blob: 71d0098809acebed85209b622d2feeea74bdd1e4 [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() {
Colin Cross89536d42017-07-07 14:35:50 -070037 android.RegisterModuleType("java_defaults", defaultsFactory)
38
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)
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Jeongik Cha538c0d02019-07-11 15:54:27 +090057func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
58 if sc, ok := ctx.Module().(sdkContext); ok {
59 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
60 if usePlatformAPI != (sc.sdkVersion() == "") {
61 if usePlatformAPI {
62 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
63 } else {
64 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
65 }
66 }
67
68 }
69}
70
Colin Cross2fe66872015-03-30 17:20:39 -070071// TODO:
72// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070073// Renderscript
74// Post-jar passes:
75// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070076// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070077// DroidDoc
78// Findbugs
79
Colin Cross89536d42017-07-07 14:35:50 -070080type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070081 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
82 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080083 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070084
85 // list of source files that should not be used to build the Java module.
86 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080087 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070088
89 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070090 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070091
Colin Cross86a63ff2017-09-27 17:33:10 -070092 // list of directories that should be excluded from java_resource_dirs
93 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070094
Colin Cross0f37af02017-09-27 17:42:05 -070095 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080096 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070097
Colin Crosscedd4762018-09-13 11:26:19 -070098 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080099 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700100
Colin Cross7d5136f2015-05-11 13:39:40 -0700101 // list of module-specific flags that will be used for javac compiles
102 Javacflags []string `android:"arch_variant"`
103
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200104 // list of module-specific flags that will be used for kotlinc compiles
105 Kotlincflags []string `android:"arch_variant"`
106
Colin Cross7d5136f2015-05-11 13:39:40 -0700107 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700108 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700109
110 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700111 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700112
113 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800114 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700115
Colin Cross540eff82017-06-22 17:01:52 -0700116 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800117 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700118
119 // If not blank, set the java version passed to javac as -source and -target
120 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700121
Colin Cross9ae1b922018-06-26 17:59:05 -0700122 // If set to true, allow this module to be dexed and installed on devices. Has no
123 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700124 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700125
Colin Cross0f37af02017-09-27 17:42:05 -0700126 // If set to true, include sources used to compile the module in to the final jar
127 Include_srcs *bool
128
Vladimir Marko0975ee02019-04-02 10:29:55 +0100129 // If not empty, classes are restricted to the specified packages and their sub-packages.
130 // This restriction is checked after applying jarjar rules and including static libs.
131 Permitted_packages []string
132
Colin Crossbe9cdb82019-01-21 21:37:16 -0800133 // List of modules to use as annotation processors
134 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700135
Nan Zhang61eaedb2017-11-02 13:28:15 -0700136 // The number of Java source entries each Javac instance can process
137 Javac_shard_size *int64
138
Nan Zhang5f8cb422018-02-06 10:34:32 -0800139 // Add host jdk tools.jar to bootclasspath
140 Use_tools_jar *bool
141
Colin Cross1369cdb2017-09-29 17:58:17 -0700142 Openjdk9 struct {
143 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800144 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700145
146 // List of javac flags that should only be used when passing -source 1.9
147 Javacflags []string
148 }
Colin Crosscb933592017-11-22 13:49:43 -0800149
Colin Cross81440082018-08-15 20:21:55 -0700150 // When compiling language level 9+ .java code in packages that are part of
151 // a system module, patch_module names the module that your sources and
152 // dependencies should be patched into. The Android runtime currently
153 // doesn't implement the JEP 261 module system so this option is only
154 // supported at compile time. It should only be needed to compile tests in
155 // packages that exist in libcore and which are inconvenient to move
156 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100157 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700158
Colin Crosscb933592017-11-22 13:49:43 -0800159 Jacoco struct {
160 // List of classes to include for instrumentation with jacoco to collect coverage
161 // information at runtime when building with coverage enabled. If unset defaults to all
162 // classes.
163 // Supports '*' as the last character of an entry in the list as a wildcard match.
164 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
165 // it matches classes in the package that have the class name as a prefix.
166 Include_filter []string
167
168 // List of classes to exclude from instrumentation with jacoco to collect coverage
169 // information at runtime when building with coverage enabled. Overrides classes selected
170 // by the include_filter property.
171 // Supports '*' as the last character of an entry in the list as a wildcard match.
172 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
173 // it matches classes in the package that have the class name as a prefix.
174 Exclude_filter []string
175 }
176
Andreas Gampef3e5b552018-01-22 21:27:21 -0800177 Errorprone struct {
178 // List of javac flags that should only be used when running errorprone.
179 Javacflags []string
180 }
181
Colin Cross0f2ee152017-12-14 15:22:43 -0800182 Proto struct {
183 // List of extra options that will be passed to the proto generator.
184 Output_params []string
185 }
186
Colin Crosscb933592017-11-22 13:49:43 -0800187 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800188
189 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800190 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700191}
192
Colin Cross89536d42017-07-07 14:35:50 -0700193type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700194 // list of module-specific flags that will be used for dex compiles
195 Dxflags []string `android:"arch_variant"`
196
Jeongik Cha538c0d02019-07-11 15:54:27 +0900197 // if not blank, set to the version of the sdk to compile against.
198 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800199 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700200
Colin Cross83bb3162018-06-25 15:48:06 -0700201 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
202 // Defaults to sdk_version if not set.
203 Min_sdk_version *string
204
Dan Willemsen419290a2018-10-31 15:28:47 -0700205 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
206 // Defaults to sdk_version if not set.
207 Target_sdk_version *string
208
Jeongik Cha356dac42019-08-19 14:09:52 +0900209 // Whether to compile against the platform APIs instead of an SDK.
210 // If true, then sdk_version must be empty. The value of this field
211 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700212 Platform_apis *bool
213
Colin Crossebe1a512017-11-14 13:12:14 -0800214 Aidl struct {
215 // Top level directories to pass to aidl tool
216 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700217
Colin Crossebe1a512017-11-14 13:12:14 -0800218 // Directories rooted at the Android.bp file to pass to aidl tool
219 Local_include_dirs []string
220
221 // directories that should be added as include directories for any aidl sources of modules
222 // that depend on this module, as well as to aidl for this module.
223 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100224
225 // whether to generate traces (for systrace) for this interface
226 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100227
228 // whether to generate Binder#GetTransaction name method.
229 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800230 }
Colin Cross92430102017-10-09 14:59:32 -0700231
232 // If true, export a copy of the module as a -hostdex module for host testing.
233 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700234
Colin Cross7f87f4f2019-04-24 13:41:45 -0700235 Target struct {
236 Hostdex struct {
237 // Additional required dependencies to add to -hostdex modules.
238 Required []string
239 }
240 }
241
David Brazdil17ef5632018-06-27 10:27:45 +0100242 // If set to true, compile dex regardless of installable. Defaults to false.
243 Compile_dex *bool
244
Colin Cross66dbc0b2017-12-28 12:23:20 -0800245 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700246 // If false, disable all optimization. Defaults to true for android_app and android_test
247 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800248 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700249 // True if the module containing this has it set by default.
250 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800251
252 // If true, optimize for size by removing unused code. Defaults to true for apps,
253 // false for libraries and tests.
254 Shrink *bool
255
256 // If true, optimize bytecode. Defaults to false.
257 Optimize *bool
258
259 // If true, obfuscate bytecode. Defaults to false.
260 Obfuscate *bool
261
262 // If true, do not use the flag files generated by aapt that automatically keep
263 // classes referenced by the app manifest. Defaults to false.
264 No_aapt_flags *bool
265
266 // Flags to pass to proguard.
267 Proguard_flags []string
268
269 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800270 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800271 }
272
Paul Duffine25c6442019-10-11 13:50:28 +0100273 // When targeting 1.9 and above, override the modules to use with --system,
274 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700275 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700276
277 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800278 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700279}
280
Sasha Smundak2057f822019-04-16 17:16:58 -0700281func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
282 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
283}
284
Colin Cross46c9b8b2017-06-22 16:51:17 -0700285// Module contains the properties and members used by all java module types
286type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700287 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700288 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900289 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900290 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700291
Colin Cross89536d42017-07-07 14:35:50 -0700292 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700293 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700294 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700295
Colin Cross331a1212018-08-15 20:40:52 -0700296 // jar file containing header classes including static library dependencies, suitable for
297 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700298 headerJarFile android.Path
299
Colin Cross331a1212018-08-15 20:40:52 -0700300 // jar file containing implementation classes including static library dependencies but no
301 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700302 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700303
Colin Cross331a1212018-08-15 20:40:52 -0700304 // jar file containing only resources including from static library dependencies
305 resourceJar android.Path
306
Colin Cross0c4ce212019-05-03 15:28:19 -0700307 // args and dependencies to package source files into a srcjar
308 srcJarArgs []string
309 srcJarDeps android.Paths
310
Colin Cross331a1212018-08-15 20:40:52 -0700311 // jar file containing implementation classes and resources including static library
312 // dependencies
313 implementationAndResourcesJar android.Path
314
315 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700316 dexJarFile android.Path
317
Colin Cross43f08db2018-11-12 10:13:39 -0800318 // output file that contains classes.dex if it should be in the output file
319 maybeStrippedDexJarFile android.Path
320
Colin Crosscb933592017-11-22 13:49:43 -0800321 // output file containing uninstrumented classes that will be instrumented by jacoco
322 jacocoReportClassesFile android.Path
323
Colin Cross66dbc0b2017-12-28 12:23:20 -0800324 // output file containing mapping of obfuscated names
325 proguardDictionary android.Path
326
Colin Cross331a1212018-08-15 20:40:52 -0700327 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700328 outputFile android.Path
329 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700330
Colin Cross635c3b02016-05-18 15:37:25 -0700331 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700332
Colin Cross635c3b02016-05-18 15:37:25 -0700333 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700334
Colin Cross2fe66872015-03-30 17:20:39 -0700335 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700336 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800337
338 // list of .java files and srcjars that was passed to javac
339 compiledJavaSrcs android.Paths
340 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800341
342 // list of extra progurad flag files
343 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900344
Colin Cross094054a2018-10-17 15:10:48 -0700345 // manifest file to use instead of properties.Manifest
346 overrideManifest android.OptionalPath
347
Jiyong Park1be96912018-05-28 18:02:19 +0900348 // list of SDK lib names that this java moudule is exporting
349 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700350
patricktu242faad2019-09-24 15:41:30 +0800351 // list of source files, collected from srcFiles with uniqie java and all kt files,
352 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700353 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800354
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800355 // expanded Jarjar_rules
356 expandJarjarRules android.Path
357
Vladimir Marko0975ee02019-04-02 10:29:55 +0100358 // list of additional targets for checkbuild
359 additionalCheckedModules android.Paths
360
Colin Cross988708c2019-05-06 14:04:11 -0700361 // Extra files generated by the module type to be added as java resources.
362 extraResources android.Paths
363
Colin Crossf24a22a2019-01-31 14:12:44 -0800364 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800365 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800366
367 // list of the xref extraction files
368 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700369}
370
Colin Cross41955e82019-05-29 14:40:35 -0700371func (j *Module) OutputFiles(tag string) (android.Paths, error) {
372 switch tag {
373 case "":
374 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700375 case ".jar":
376 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700377 case ".proguard_map":
378 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700379 default:
380 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
381 }
Colin Cross54250902017-12-05 09:28:08 -0800382}
383
Jiyong Park8fd61922018-11-08 02:50:25 +0900384func (j *Module) DexJarFile() android.Path {
385 return j.dexJarFile
386}
387
Colin Cross41955e82019-05-29 14:40:35 -0700388var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800389
Colin Crossf506d872017-07-19 15:53:04 -0700390type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700391 HeaderJars() android.Paths
392 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700393 ResourceJars() android.Paths
394 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800395 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700396 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900397 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700398 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700399 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700400}
401
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700403 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
404 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405}
406
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800407type xref interface {
408 XrefJavaFiles() android.Paths
409}
410
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800411func (j *Module) XrefJavaFiles() android.Paths {
412 return j.kytheFiles
413}
414
Colin Cross89536d42017-07-07 14:35:50 -0700415func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
416 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
417 android.InitDefaultableModule(module)
418}
419
Colin Crossbe1da472017-07-07 15:59:46 -0700420type dependencyTag struct {
421 blueprint.BaseDependencyTag
422 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700423}
424
Colin Crossa4f08812018-10-02 22:03:40 -0700425type jniDependencyTag struct {
426 blueprint.BaseDependencyTag
427 target android.Target
428}
429
Colin Crossbe1da472017-07-07 15:59:46 -0700430var (
Colin Cross4b964c02018-10-15 16:18:06 -0700431 staticLibTag = dependencyTag{name: "staticlib"}
432 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800433 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700434 bootClasspathTag = dependencyTag{name: "bootclasspath"}
435 systemModulesTag = dependencyTag{name: "system modules"}
436 frameworkResTag = dependencyTag{name: "framework-res"}
437 frameworkApkTag = dependencyTag{name: "framework-apk"}
438 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800439 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700440 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
441 certificateTag = dependencyTag{name: "certificate"}
442 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700443 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700444)
Colin Cross2fe66872015-03-30 17:20:39 -0700445
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900446func defaultSdkVersion(ctx checkVendorModuleContext) string {
447 if ctx.SocSpecific() || ctx.DeviceSpecific() {
448 return "system_current"
449 }
450 return ""
451}
452
453type checkVendorModuleContext interface {
454 SocSpecific() bool
455 DeviceSpecific() bool
456}
457
Colin Crossfc3674a2017-09-18 17:41:52 -0700458type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700459 useModule, useFiles, useDefaultLibs, invalidVersion bool
460
Paul Duffine25c6442019-10-11 13:50:28 +0100461 modules []string
462
463 // The default system modules to use. Will be an empty string if no system
464 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700465 systemModules string
466
Colin Crossa97c5d32018-03-28 14:58:31 -0700467 frameworkResModule string
468
Colin Cross86a60ae2018-05-29 14:44:55 -0700469 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700470 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100471
472 noStandardLibs, noFrameworksLibs bool
473}
474
475func (s sdkDep) hasStandardLibs() bool {
476 return !s.noStandardLibs
477}
478
479func (s sdkDep) hasFrameworkLibs() bool {
480 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700481}
482
Colin Crossa4f08812018-10-02 22:03:40 -0700483type jniLib struct {
484 name string
485 path android.Path
486 target android.Target
487}
488
Colin Cross0ea8ba82019-06-06 14:33:29 -0700489func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800490 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
491}
492
Colin Cross0ea8ba82019-06-06 14:33:29 -0700493func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800494 return j.shouldInstrument(ctx) &&
495 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
496 ctx.Config().UnbundledBuild())
497}
498
Colin Cross83bb3162018-06-25 15:48:06 -0700499func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900500 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700501}
502
Paul Duffine25c6442019-10-11 13:50:28 +0100503func (j *Module) systemModules() string {
504 return proptools.String(j.deviceProperties.System_modules)
505}
506
Colin Cross83bb3162018-06-25 15:48:06 -0700507func (j *Module) minSdkVersion() string {
508 if j.deviceProperties.Min_sdk_version != nil {
509 return *j.deviceProperties.Min_sdk_version
510 }
511 return j.sdkVersion()
512}
513
Dan Willemsen419290a2018-10-31 15:28:47 -0700514func (j *Module) targetSdkVersion() string {
515 if j.deviceProperties.Target_sdk_version != nil {
516 return *j.deviceProperties.Target_sdk_version
517 }
518 return j.sdkVersion()
519}
520
Colin Crossbe1da472017-07-07 15:59:46 -0700521func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700522 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100523 sdkDep := decodeSdkDep(ctx, sdkContext(j))
524 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700525 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700526 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100527 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100528 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700529 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700530 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700531 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100532 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700533 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700534 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700535 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
536 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800537 }
Colin Crossbe1da472017-07-07 15:59:46 -0700538 }
Paul Duffine25c6442019-10-11 13:50:28 +0100539 } else if sdkDep.systemModules != "" {
Paul Duffin68289b02019-09-20 13:50:52 +0100540 // Add the system modules to both the system modules and bootclasspath.
Paul Duffine25c6442019-10-11 13:50:28 +0100541 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
542 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.systemModules)
Colin Cross2fe66872015-03-30 17:20:39 -0700543 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800544 if ctx.ModuleName() == "android_stubs_current" ||
545 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700546 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700547 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800548 }
Colin Cross2fe66872015-03-30 17:20:39 -0700549 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700550
Colin Cross42d48b72018-08-29 14:10:52 -0700551 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
552 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700553
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700554 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800555
Colin Crossfe17f6f2019-03-28 19:30:56 -0700556 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700557 if j.hasSrcExt(".proto") {
558 protoDeps(ctx, &j.protoProperties)
559 }
Colin Cross93e85952017-08-15 13:34:18 -0700560
561 if j.hasSrcExt(".kt") {
562 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
563 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700564 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
565 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800566 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800567 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
568 }
Colin Cross93e85952017-08-15 13:34:18 -0700569 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800570
571 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700572 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800573 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700574}
575
576func hasSrcExt(srcs []string, ext string) bool {
577 for _, src := range srcs {
578 if filepath.Ext(src) == ext {
579 return true
580 }
581 }
582
583 return false
584}
585
586func (j *Module) hasSrcExt(ext string) bool {
587 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700588}
589
Colin Cross46c9b8b2017-06-22 16:51:17 -0700590func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700591 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700592
Colin Crossebe1a512017-11-14 13:12:14 -0800593 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
594 aidlIncludes = append(aidlIncludes,
595 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
596 aidlIncludes = append(aidlIncludes,
597 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700598
Colin Cross3047fa22019-04-18 10:56:44 -0700599 var flags []string
600 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700601
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700602 if aidlPreprocess.Valid() {
603 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700604 deps = append(deps, aidlPreprocess.Path())
605 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700606 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700607 }
608
Colin Cross3047fa22019-04-18 10:56:44 -0700609 if len(j.exportAidlIncludeDirs) > 0 {
610 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
611 }
612
613 if len(aidlIncludes) > 0 {
614 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
615 }
616
Colin Cross635c3b02016-05-18 15:37:25 -0700617 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800618 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700619 flags = append(flags, "-I"+src.String())
620 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700621
Martijn Coeneneab15642018-03-09 09:29:59 +0100622 if Bool(j.deviceProperties.Aidl.Generate_traces) {
623 flags = append(flags, "-t")
624 }
625
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100626 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
627 flags = append(flags, "--transaction_names")
628 }
629
Colin Cross3047fa22019-04-18 10:56:44 -0700630 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700631}
632
Colin Cross32f676a2017-09-06 13:41:06 -0700633type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800634 classpath classpath
635 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700636 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800637 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700638 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700639 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700640 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700641 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800642 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700643 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700644 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700645 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700646 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800647 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800648
649 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700650}
Colin Cross2fe66872015-03-30 17:20:39 -0700651
Colin Cross54250902017-12-05 09:28:08 -0800652func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
653 for _, f := range dep.Srcs() {
654 if f.Ext() != ".jar" {
655 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
656 ctx.OtherModuleName(dep.(blueprint.Module)))
657 }
658 }
659}
660
Jiyong Park2d492942018-03-05 17:44:10 +0900661type linkType int
662
663const (
664 javaCore linkType = iota
665 javaSdk
666 javaSystem
667 javaPlatform
668)
669
Jiyong Park46f78fb2018-10-20 16:33:17 +0900670func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700671 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700672 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900673 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
674 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100675 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900676 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100677 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900678 return javaCore, false
679 case name == "android_system_stubs_current":
680 return javaSystem, true
681 case strings.HasPrefix(ver, "system_"):
682 return javaSystem, false
683 case name == "android_test_stubs_current":
684 return javaSystem, true
685 case strings.HasPrefix(ver, "test_"):
686 return javaPlatform, false
687 case name == "android_stubs_current":
688 return javaSdk, true
689 case ver == "current":
690 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100691 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900692 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700693 default:
694 if _, err := strconv.Atoi(ver); err != nil {
695 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
696 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900697 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900698 }
699}
700
Jiyong Park750e5572018-01-31 00:20:13 +0900701func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700702 if ctx.Host() {
703 return
704 }
705
Jiyong Park46f78fb2018-10-20 16:33:17 +0900706 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
707 if stubs {
708 return
709 }
710 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900711 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."
712
713 switch myLinkType {
714 case javaCore:
715 if otherLinkType != javaCore {
716 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 +0900717 ctx.OtherModuleName(to))
718 }
Jiyong Park2d492942018-03-05 17:44:10 +0900719 break
720 case javaSdk:
721 if otherLinkType != javaCore && otherLinkType != javaSdk {
722 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
723 ctx.OtherModuleName(to))
724 }
725 break
726 case javaSystem:
727 if otherLinkType == javaPlatform {
728 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
729 ctx.OtherModuleName(to))
730 }
731 break
732 case javaPlatform:
733 // no restriction on link-type
734 break
Jiyong Park750e5572018-01-31 00:20:13 +0900735 }
736}
737
Colin Cross32f676a2017-09-06 13:41:06 -0700738func (j *Module) collectDeps(ctx android.ModuleContext) deps {
739 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700740
Colin Cross300f0382018-03-06 13:11:51 -0800741 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700742 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800743 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700744 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800745 } else if sdkDep.useFiles {
746 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700747 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700748 deps.aidlPreprocess = sdkDep.aidl
749 } else {
750 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800751 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700752 }
753
Colin Crossd11fcda2017-10-23 17:59:01 -0700754 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700755 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700756 tag := ctx.OtherModuleDependencyTag(module)
757
Colin Crossa4f08812018-10-02 22:03:40 -0700758 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700759 // Handled by AndroidApp.collectAppDeps
760 return
761 }
762 if tag == certificateTag {
763 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700764 return
765 }
766
Jiyong Park750e5572018-01-31 00:20:13 +0900767 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700768 switch tag {
769 case bootClasspathTag, libTag, staticLibTag:
770 checkLinkType(ctx, j, to, tag.(dependencyTag))
771 }
Jiyong Park750e5572018-01-31 00:20:13 +0900772 }
Colin Cross54250902017-12-05 09:28:08 -0800773 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800774 case SdkLibraryDependency:
775 switch tag {
776 case libTag:
777 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
778 // names of sdk libs that are directly depended are exported
779 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700780 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800781 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
782 }
Colin Cross54250902017-12-05 09:28:08 -0800783 case Dependency:
784 switch tag {
785 case bootClasspathTag:
786 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700787 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800788 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900789 // sdk lib names from dependencies are re-exported
790 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700791 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800792 case staticLibTag:
793 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
794 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
795 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700796 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900797 // sdk lib names from dependencies are re-exported
798 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700799 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800800 case pluginTag:
801 if plugin, ok := dep.(*Plugin); ok {
802 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
803 if plugin.pluginProperties.Processor_class != nil {
804 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
805 }
806 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
807 } else {
808 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
809 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800810 case frameworkApkTag:
811 if ctx.ModuleName() == "android_stubs_current" ||
812 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700813 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800814 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
815 // resource files out of there for aapt.
816 //
817 // Normally the package rule runs aapt, which includes the resource,
818 // but we're not running that in our package rule so just copy in the
819 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700820 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800821 }
Colin Cross54250902017-12-05 09:28:08 -0800822 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700823 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800824 case kotlinAnnotationsTag:
825 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800826 }
827
Colin Cross54250902017-12-05 09:28:08 -0800828 case android.SourceFileProducer:
829 switch tag {
830 case libTag:
831 checkProducesJars(ctx, dep)
832 deps.classpath = append(deps.classpath, dep.Srcs()...)
833 case staticLibTag:
834 checkProducesJars(ctx, dep)
835 deps.classpath = append(deps.classpath, dep.Srcs()...)
836 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
837 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800838 }
839 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700840 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100841 case bootClasspathTag:
842 // If a system modules dependency has been added to the bootclasspath
843 // then add its libs to the bootclasspath.
844 sm := module.(*SystemModules)
845 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
846
Colin Cross1369cdb2017-09-29 17:58:17 -0700847 case systemModulesTag:
848 if deps.systemModules != nil {
849 panic("Found two system module dependencies")
850 }
851 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000852 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700853 panic("Missing directory for system module dependency")
854 }
Colin Crossb77043e2019-07-16 13:57:13 -0700855 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700856 }
Colin Crossec7a0422017-07-07 14:47:12 -0700857 }
Colin Cross2fe66872015-03-30 17:20:39 -0700858 })
859
Jiyong Park1be96912018-05-28 18:02:19 +0900860 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
861
Colin Cross32f676a2017-09-06 13:41:06 -0700862 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700863}
864
Colin Cross83bb3162018-06-25 15:48:06 -0700865func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700866 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800867 v := sdkContext.sdkVersion()
868 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100869 if ctx.Config().IsPdkBuild() &&
870 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700871 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800872 latestSdkVersion := 0
873 if len(sdkVersions) > 0 {
874 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
875 }
876 v = strconv.Itoa(latestSdkVersion)
877 }
878
879 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700880 if err != nil {
881 ctx.PropertyErrorf("sdk_version", "%s", err)
882 }
Nan Zhang357466b2018-04-17 17:38:36 -0700883 if javaVersion != "" {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100884 ret = normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700885 } else if ctx.Device() && sdk <= 23 {
886 ret = "1.7"
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100887 } else if ctx.Device() && sdk <= 29 {
Nan Zhang357466b2018-04-17 17:38:36 -0700888 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100889 } else if ctx.Device() &&
890 sdkContext.sdkVersion() != "" &&
891 sdkContext.sdkVersion() != "none" &&
892 sdkContext.sdkVersion() != "core_platform" &&
893 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700894 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
895 ret = "1.8"
896 } else {
897 ret = "1.9"
898 }
899
900 return ret
901}
902
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100903func normalizeJavaVersion(ctx android.ModuleContext, javaVersion string) string {
904 switch javaVersion {
905 case "1.6", "6":
906 return "1.6"
907 case "1.7", "7":
908 return "1.7"
909 case "1.8", "8":
910 return "1.8"
911 case "1.9", "9":
912 return "1.9"
913 case "10", "11":
914 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
915 return "unsupported"
916 default:
917 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
918 return "unrecognized"
919 }
920}
921
Nan Zhanged19fc32017-10-19 13:06:22 -0700922func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700923
Colin Crossf03c82b2015-04-13 13:53:40 -0700924 var flags javaBuilderFlags
925
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100926 // javaVersion flag.
927 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
928
Nan Zhanged19fc32017-10-19 13:06:22 -0700929 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700930 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100931 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700932 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 }
Colin Cross6510f912017-11-29 00:27:14 -0800934 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700935 // Override the -g flag passed globally to remove local variable debug info to reduce
936 // disk and memory usage.
937 javacFlags = append(javacFlags, "-g:source,lines")
938 }
Colin Cross64162712017-08-08 13:17:59 -0700939
Colin Cross66548102018-06-19 22:47:35 -0700940 if ctx.Config().RunErrorProne() {
941 if config.ErrorProneClasspath == nil {
942 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
943 }
944
945 errorProneFlags := []string{
946 "-Xplugin:ErrorProne",
947 "${config.ErrorProneChecks}",
948 }
949 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
950
951 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
952 "'" + strings.Join(errorProneFlags, " ") + "'"
953 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800954 }
955
Nan Zhanged19fc32017-10-19 13:06:22 -0700956 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800957 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
958 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700959 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800960
Colin Crossbe9cdb82019-01-21 21:37:16 -0800961 flags.processor = strings.Join(deps.processorClasses, ",")
962
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100963 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100964 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800965 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
966 // Give host-side tools a version of OpenJDK's standard libraries
967 // close to what they're targeting. As of Dec 2017, AOSP is only
968 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
969 //
970 // When building with OpenJDK 8, the following should have no
971 // effect since those jars would be available by default.
972 //
973 // When building with OpenJDK 9 but targeting a version < 1.8,
974 // putting them on the bootclasspath means that:
975 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
976 // b) references to existing APIs are not reinterpreted in an
977 // OpenJDK 9-specific way, eg. calls to subclasses of
978 // java.nio.Buffer as in http://b/70862583
979 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
980 flags.bootClasspath = append(flags.bootClasspath,
981 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
982 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800983 if Bool(j.properties.Use_tools_jar) {
984 flags.bootClasspath = append(flags.bootClasspath,
985 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
986 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800987 }
988
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100989 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800990 // Manually specify build directory in case it is not under the repo root.
991 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
992 // just adding a symlink under the root doesn't help.)
993 patchPaths := ".:" + ctx.Config().BuildDir()
994 classPath := flags.classpath.FormJavaClassPath("")
995 if classPath != "" {
996 patchPaths += ":" + classPath
997 }
998 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700999 }
1000
Nan Zhanged19fc32017-10-19 13:06:22 -07001001 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001002 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001003
Nan Zhanged19fc32017-10-19 13:06:22 -07001004 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001005 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001006
Colin Cross81440082018-08-15 20:21:55 -07001007 if len(javacFlags) > 0 {
1008 // optimization.
1009 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1010 flags.javacFlags = "$javacFlags"
1011 }
1012
Nan Zhanged19fc32017-10-19 13:06:22 -07001013 return flags
1014}
Colin Crossc0b06f12015-04-08 13:03:43 -07001015
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001016func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001017 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001018
1019 deps := j.collectDeps(ctx)
1020 flags := j.collectBuilderFlags(ctx, deps)
1021
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001022 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001023 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1024 }
Colin Cross8a497952019-03-05 22:25:09 -08001025 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001026 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001027 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001028 }
1029
Colin Crossaf050172017-11-15 23:01:59 -08001030 srcFiles = j.genSources(ctx, srcFiles, flags)
1031
1032 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001033 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001034 if aaptSrcJar != nil {
1035 srcJars = append(srcJars, aaptSrcJar)
1036 }
Colin Crossb7a63242015-04-16 14:09:14 -07001037
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001038 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001039 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001040 }
1041
Colin Cross1ee23172017-10-18 14:44:18 -07001042 jarName := ctx.ModuleName() + ".jar"
1043
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001044 javaSrcFiles := srcFiles.FilterByExt(".java")
1045 var uniqueSrcFiles android.Paths
1046 set := make(map[string]bool)
1047 for _, v := range javaSrcFiles {
1048 if _, found := set[v.String()]; !found {
1049 set[v.String()] = true
1050 uniqueSrcFiles = append(uniqueSrcFiles, v)
1051 }
1052 }
1053
patricktu242faad2019-09-24 15:41:30 +08001054 // Collect .java files for AIDEGen
1055 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1056
Colin Cross55f63ea2018-08-27 12:37:09 -07001057 var kotlinJars android.Paths
1058
Colin Cross93e85952017-08-15 13:34:18 -07001059 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001060 // user defined kotlin flags.
1061 kotlincFlags := j.properties.Kotlincflags
1062 CheckKotlincFlags(ctx, kotlincFlags)
1063
Colin Cross93e85952017-08-15 13:34:18 -07001064 // If there are kotlin files, compile them first but pass all the kotlin and java files
1065 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1066 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001067 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001068 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001069 kotlincFlags = append(kotlincFlags, "-no-jdk")
1070 }
1071 if len(kotlincFlags) > 0 {
1072 // optimization.
1073 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1074 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001075 }
1076
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001077 var kotlinSrcFiles android.Paths
1078 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1079 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1080
patricktu242faad2019-09-24 15:41:30 +08001081 // Collect .kt files for AIDEGen
1082 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1083
Colin Crossafbb1732019-01-17 15:42:52 -08001084 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1085 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1086
1087 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1088 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1089
1090 if len(flags.processorPath) > 0 {
1091 // Use kapt for annotation processing
1092 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1093 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1094 srcJars = append(srcJars, kaptSrcJar)
1095 // Disable annotation processing in javac, it's already been handled by kapt
1096 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001097 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001098 }
Colin Cross93e85952017-08-15 13:34:18 -07001099
Colin Cross1ee23172017-10-18 14:44:18 -07001100 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001101 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001102 if ctx.Failed() {
1103 return
1104 }
1105
1106 // Make javac rule depend on the kotlinc rule
1107 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001108
Colin Cross93e85952017-08-15 13:34:18 -07001109 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001110 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001111 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001112 }
1113
Colin Cross55f63ea2018-08-27 12:37:09 -07001114 jars := append(android.Paths(nil), kotlinJars...)
1115
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001116 // Store the list of .java files that was passed to javac
1117 j.compiledJavaSrcs = uniqueSrcFiles
1118 j.compiledSrcJars = srcJars
1119
Nan Zhang61eaedb2017-11-02 13:28:15 -07001120 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001121 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001122 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1123 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001124 // Formerly, there was a check here that prevented annotation processors
1125 // from being used when sharding was enabled, as some annotation processors
1126 // do not function correctly in sharded environments. It was removed to
1127 // allow for the use of annotation processors that do function correctly
1128 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001129 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001130 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001131 if ctx.Failed() {
1132 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001133 }
1134 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001135 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001136 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001137 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001138 // If error-prone is enabled, add an additional rule to compile the java files into
1139 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001140 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001141 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1142 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001143 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001144 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001145 extraJarDeps = append(extraJarDeps, errorprone)
1146 }
1147
Nan Zhang61eaedb2017-11-02 13:28:15 -07001148 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001149 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001150 shardSize := int(*(j.properties.Javac_shard_size))
1151 var shardSrcs []android.Paths
1152 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001153 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001154 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001155 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1156 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001157 jars = append(jars, classes)
1158 }
1159 }
1160 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001161 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1162 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001163 jars = append(jars, classes)
1164 }
1165 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001166 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001167 jars = append(jars, classes)
1168 }
Colin Crossd6891432017-09-27 17:39:56 -07001169 if ctx.Failed() {
1170 return
1171 }
Colin Cross2fe66872015-03-30 17:20:39 -07001172 }
1173
Colin Cross0c4ce212019-05-03 15:28:19 -07001174 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1175
1176 var includeSrcJar android.WritablePath
1177 if Bool(j.properties.Include_srcs) {
1178 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1179 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1180 }
1181
Colin Crosscedd4762018-09-13 11:26:19 -07001182 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1183 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001184 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001185 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001186
1187 var resArgs []string
1188 var resDeps android.Paths
1189
1190 resArgs = append(resArgs, dirArgs...)
1191 resDeps = append(resDeps, dirDeps...)
1192
1193 resArgs = append(resArgs, fileArgs...)
1194 resDeps = append(resDeps, fileDeps...)
1195
Colin Cross988708c2019-05-06 14:04:11 -07001196 resArgs = append(resArgs, extraArgs...)
1197 resDeps = append(resDeps, extraDeps...)
1198
Colin Cross40a36712017-09-27 17:41:35 -07001199 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001200 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001201 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001202 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001203 if ctx.Failed() {
1204 return
1205 }
1206 }
1207
Colin Cross0c4ce212019-05-03 15:28:19 -07001208 var resourceJars android.Paths
1209 if j.resourceJar != nil {
1210 resourceJars = append(resourceJars, j.resourceJar)
1211 }
1212 if Bool(j.properties.Include_srcs) {
1213 resourceJars = append(resourceJars, includeSrcJar)
1214 }
1215 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001216
Colin Cross0c4ce212019-05-03 15:28:19 -07001217 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001218 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001219 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001220 false, nil, nil)
1221 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001222 } else if len(resourceJars) == 1 {
1223 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001224 }
1225
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001226 if len(deps.staticJars) > 0 {
1227 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001228 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001229
Colin Cross094054a2018-10-17 15:10:48 -07001230 manifest := j.overrideManifest
1231 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001232 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001233 }
Colin Cross635acc92017-09-12 22:50:46 -07001234
Colin Cross8a497952019-03-05 22:25:09 -08001235 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001236 if len(services) > 0 {
1237 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1238 var zipargs []string
1239 for _, file := range services {
1240 serviceFile := file.String()
1241 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1242 }
1243 ctx.Build(pctx, android.BuildParams{
1244 Rule: zip,
1245 Output: servicesJar,
1246 Implicits: services,
1247 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001248 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001249 },
1250 })
1251 jars = append(jars, servicesJar)
1252 }
1253
Colin Cross0a6e0072017-08-30 14:24:55 -07001254 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001255 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001256 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001257
1258 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001259 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1260 // Optimization: skip the combine step if there is nothing to do
1261 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1262 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1263 // any if len(jars) == 1.
1264 outputFile = moduleOutPath
1265 } else {
1266 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1267 ctx.Build(pctx, android.BuildParams{
1268 Rule: android.Cp,
1269 Input: jars[0],
1270 Output: combinedJar,
1271 })
1272 outputFile = combinedJar
1273 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001274 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001275 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001276 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001277 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001278 outputFile = combinedJar
1279 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001280
Colin Cross331a1212018-08-15 20:40:52 -07001281 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001282 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001283 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001284 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001285 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001286 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001287
1288 // jarjar resource jar if necessary
1289 if j.resourceJar != nil {
1290 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001291 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001292 j.resourceJar = resourceJarJarFile
1293 }
1294
Colin Cross0a6e0072017-08-30 14:24:55 -07001295 if ctx.Failed() {
1296 return
1297 }
1298 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001299
1300 // Check package restrictions if necessary.
1301 if len(j.properties.Permitted_packages) > 0 {
1302 // Check packages and copy to package-checked file.
1303 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1304 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1305 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1306
1307 if ctx.Failed() {
1308 return
1309 }
1310 }
1311
Nan Zhanged19fc32017-10-19 13:06:22 -07001312 j.implementationJarFile = outputFile
1313 if j.headerJarFile == nil {
1314 j.headerJarFile = j.implementationJarFile
1315 }
Colin Cross2fe66872015-03-30 17:20:39 -07001316
Colin Cross6510f912017-11-29 00:27:14 -08001317 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001318 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1319 j.properties.Instrument = true
1320 }
1321 }
1322
Colin Cross3144dfc2018-01-03 15:06:47 -08001323 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001324 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1325 }
1326
Colin Cross331a1212018-08-15 20:40:52 -07001327 // merge implementation jar with resources if necessary
1328 implementationAndResourcesJar := outputFile
1329 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001330 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001331 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001332 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001333 false, nil, nil)
1334 implementationAndResourcesJar = combinedJar
1335 }
1336
1337 j.implementationAndResourcesJar = implementationAndResourcesJar
1338
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001339 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001340 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001341 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001342 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001343 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001344 if ctx.Failed() {
1345 return
1346 }
Colin Cross331a1212018-08-15 20:40:52 -07001347
Jiyong Park09cb6292019-07-15 15:29:23 +09001348 // Hidden API CSV generation and dex encoding
1349 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1350 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001351
Colin Cross331a1212018-08-15 20:40:52 -07001352 // merge dex jar with resources if necessary
1353 if j.resourceJar != nil {
1354 jars := android.Paths{dexOutputFile, j.resourceJar}
1355 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1356 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1357 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001358 if j.deviceProperties.UncompressDex {
1359 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1360 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1361 dexOutputFile = combinedAlignedJar
1362 } else {
1363 dexOutputFile = combinedJar
1364 }
Colin Cross331a1212018-08-15 20:40:52 -07001365 }
1366
1367 j.dexJarFile = dexOutputFile
1368
Colin Cross8faf8fc2019-01-16 15:15:52 -08001369 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001370 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1371
1372 j.maybeStrippedDexJarFile = dexOutputFile
1373
Colin Cross3063b782018-08-15 11:19:12 -07001374 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001375
1376 if ctx.Failed() {
1377 return
1378 }
Colin Cross331a1212018-08-15 20:40:52 -07001379 } else {
1380 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001381 }
Colin Cross331a1212018-08-15 20:40:52 -07001382
Colin Crossb7a63242015-04-16 14:09:14 -07001383 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001384
1385 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1386 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001387}
1388
Colin Cross3b706fd2019-09-05 16:44:18 -07001389func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1390 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1391
1392 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1393 if idx >= 0 {
1394 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1395 jarName += strconv.Itoa(idx)
1396 }
1397
1398 classes := android.PathForModuleOut(ctx, "javac", jarName)
1399 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1400
1401 if ctx.Config().EmitXrefRules() {
1402 extractionFile := android.PathForModuleOut(ctx, kzipName)
1403 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1404 j.kytheFiles = append(j.kytheFiles, extractionFile)
1405 }
1406
1407 return classes
1408}
1409
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001410// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1411// since some of these flags may be used internally.
1412func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1413 for _, flag := range flags {
1414 flag = strings.TrimSpace(flag)
1415
1416 if !strings.HasPrefix(flag, "-") {
1417 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1418 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1419 ctx.PropertyErrorf("kotlincflags",
1420 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1421 } else if inList(flag, config.KotlincIllegalFlags) {
1422 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1423 } else if flag == "-include-runtime" {
1424 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1425 } else {
1426 args := strings.Split(flag, " ")
1427 if args[0] == "-kotlin-home" {
1428 ctx.PropertyErrorf("kotlincflags",
1429 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1430 }
1431 }
1432 }
1433}
1434
Colin Cross8eadbf02017-10-24 17:46:00 -07001435func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001436 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001437
1438 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001439 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001440 // Compile java sources into turbine.jar.
1441 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1442 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1443 if ctx.Failed() {
1444 return nil
1445 }
1446 jars = append(jars, turbineJar)
1447 }
1448
Colin Cross55f63ea2018-08-27 12:37:09 -07001449 jars = append(jars, extraJars...)
1450
Nan Zhanged19fc32017-10-19 13:06:22 -07001451 // Combine any static header libraries into classes-header.jar. If there is only
1452 // one input jar this step will be skipped.
1453 var headerJar android.Path
1454 jars = append(jars, deps.staticHeaderJars...)
1455
Colin Cross5c6ecc12017-10-23 18:12:27 -07001456 // we cannot skip the combine step for now if there is only one jar
1457 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1458 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001459 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001460 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001461 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001462
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001463 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001464 // Transform classes.jar into classes-jarjar.jar
1465 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001466 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001467 headerJar = jarjarFile
1468 if ctx.Failed() {
1469 return nil
1470 }
1471 }
1472
1473 return headerJar
1474}
1475
Colin Crosscb933592017-11-22 13:49:43 -08001476func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001477 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001478
Colin Cross7a3139e2017-12-19 13:57:50 -08001479 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001480
Colin Cross84c38822018-01-03 15:59:46 -08001481 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001482 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1483
1484 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1485
1486 j.jacocoReportClassesFile = jacocoReportClassesFile
1487
1488 return instrumentedJar
1489}
1490
albaltai36ff7dc2018-12-25 14:35:23 +08001491var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001492
Nan Zhanged19fc32017-10-19 13:06:22 -07001493func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001494 if j.headerJarFile == nil {
1495 return nil
1496 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001497 return android.Paths{j.headerJarFile}
1498}
1499
1500func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001501 if j.implementationJarFile == nil {
1502 return nil
1503 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001504 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001505}
1506
Colin Crossf24a22a2019-01-31 14:12:44 -08001507func (j *Module) DexJar() android.Path {
1508 return j.dexJarFile
1509}
1510
Colin Cross331a1212018-08-15 20:40:52 -07001511func (j *Module) ResourceJars() android.Paths {
1512 if j.resourceJar == nil {
1513 return nil
1514 }
1515 return android.Paths{j.resourceJar}
1516}
1517
1518func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001519 if j.implementationAndResourcesJar == nil {
1520 return nil
1521 }
Colin Cross331a1212018-08-15 20:40:52 -07001522 return android.Paths{j.implementationAndResourcesJar}
1523}
1524
Colin Cross46c9b8b2017-06-22 16:51:17 -07001525func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001526 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001527 return j.exportAidlIncludeDirs
1528}
1529
Jiyong Park1be96912018-05-28 18:02:19 +09001530func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001531 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001532 return j.exportedSdkLibs
1533}
1534
Colin Cross0c4ce212019-05-03 15:28:19 -07001535func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1536 return j.srcJarArgs, j.srcJarDeps
1537}
1538
Colin Cross46c9b8b2017-06-22 16:51:17 -07001539var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001540
Colin Cross46c9b8b2017-06-22 16:51:17 -07001541func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001542 return j.logtagsSrcs
1543}
1544
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001545// Collect information for opening IDE project files in java/jdeps.go.
1546func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1547 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1548 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001549 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001550 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001551 if j.expandJarjarRules != nil {
1552 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001553 }
1554}
1555
1556func (j *Module) CompilerDeps() []string {
1557 jdeps := []string{}
1558 jdeps = append(jdeps, j.properties.Libs...)
1559 jdeps = append(jdeps, j.properties.Static_libs...)
1560 return jdeps
1561}
1562
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001563func (j *Module) hasCode(ctx android.ModuleContext) bool {
1564 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1565 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1566}
1567
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001568func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1569 depTag := ctx.OtherModuleDependencyTag(dep)
1570 // dependencies other than the static linkage are all considered crossing APEX boundary
1571 return depTag == staticLibTag
1572}
1573
Colin Cross2fe66872015-03-30 17:20:39 -07001574//
1575// Java libraries (.jar file)
1576//
1577
Colin Crossf506d872017-07-19 15:53:04 -07001578type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001579 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001580
1581 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001582}
1583
Colin Cross42be7612019-02-21 18:12:14 -08001584func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001585 // Store uncompressed (and do not strip) dex files from boot class path jars.
1586 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1587 return true
1588 }
1589
1590 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001591 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001592 return true
1593 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001594 if ctx.Config().UncompressPrivAppDex() &&
1595 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1596 return true
1597 }
1598
Colin Cross2fc72f62018-12-21 12:59:54 -08001599 return false
1600}
1601
Colin Crossf506d872017-07-19 15:53:04 -07001602func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001603 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1604 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001605 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001606 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001607 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001608 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001609
Jiyong Park7f7766d2019-07-25 22:02:35 +09001610 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1611 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001612 var extraInstallDeps android.Paths
1613 if j.InstallMixin != nil {
1614 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1615 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001616 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001617 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001618 }
Colin Crossb7a63242015-04-16 14:09:14 -07001619}
1620
Colin Crossf506d872017-07-19 15:53:04 -07001621func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001622 j.deps(ctx)
1623}
1624
Colin Cross1b16b0e2019-02-12 14:41:32 -08001625// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1626//
1627// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1628// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1629// as a `static_libs` dependency of another module.
1630//
1631// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1632// a device.
1633//
1634// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1635// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001636func LibraryFactory() android.Module {
1637 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001638
Colin Cross9ae1b922018-06-26 17:59:05 -07001639 module.AddProperties(
1640 &module.Module.properties,
1641 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001642 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001643 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001644
Colin Cross9ae1b922018-06-26 17:59:05 -07001645 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001646 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001647 android.InitSdkAwareModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001648 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001649}
1650
Colin Cross1b16b0e2019-02-12 14:41:32 -08001651// java_library_static is an obsolete alias for java_library.
1652func LibraryStaticFactory() android.Module {
1653 return LibraryFactory()
1654}
1655
1656// java_library_host builds and links sources into a `.jar` file for the host.
1657//
1658// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1659// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001660func LibraryHostFactory() android.Module {
1661 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001662
Colin Cross6af17aa2017-09-20 12:59:05 -07001663 module.AddProperties(
1664 &module.Module.properties,
1665 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001666
Colin Cross9ae1b922018-06-26 17:59:05 -07001667 module.Module.properties.Installable = proptools.BoolPtr(true)
1668
Colin Cross89536d42017-07-07 14:35:50 -07001669 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001670 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001671 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001672}
1673
1674//
Colin Crossb628ea52018-08-14 16:42:33 -07001675// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001676//
1677
1678type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001679 // list of compatibility suites (for example "cts", "vts") that the module should be
1680 // installed into.
1681 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001682
1683 // the name of the test configuration (for example "AndroidTest.xml") that should be
1684 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001685 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001686
Jack He33338892018-09-19 02:21:28 -07001687 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1688 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001689 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001690
Colin Crossd96ca352018-08-10 16:06:24 -07001691 // list of files or filegroup modules that provide data that should be installed alongside
1692 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001693 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001694
1695 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1696 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1697 // explicitly.
1698 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001699}
1700
Paul Duffin42df1442019-03-20 12:45:53 +00001701type testHelperLibraryProperties struct {
1702 // list of compatibility suites (for example "cts", "vts") that the module should be
1703 // installed into.
1704 Test_suites []string `android:"arch_variant"`
1705}
1706
Colin Cross05638fc2018-04-09 18:40:24 -07001707type Test struct {
1708 Library
1709
1710 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001711
1712 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001713 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001714}
1715
Paul Duffin42df1442019-03-20 12:45:53 +00001716type TestHelperLibrary struct {
1717 Library
1718
1719 testHelperLibraryProperties testHelperLibraryProperties
1720}
1721
Colin Cross303e21f2018-08-07 16:49:25 -07001722func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001723 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1724 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001725 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001726
1727 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001728}
1729
Paul Duffin42df1442019-03-20 12:45:53 +00001730func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1731 j.Library.GenerateAndroidBuildActions(ctx)
1732}
1733
Colin Cross1b16b0e2019-02-12 14:41:32 -08001734// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1735// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1736//
1737// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1738// compiled against the device bootclasspath.
1739//
1740// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1741// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001742func TestFactory() android.Module {
1743 module := &Test{}
1744
1745 module.AddProperties(
1746 &module.Module.properties,
1747 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001748 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001749 &module.Module.protoProperties,
1750 &module.testProperties)
1751
Colin Cross9ae1b922018-06-26 17:59:05 -07001752 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001753 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001754
Colin Cross05638fc2018-04-09 18:40:24 -07001755 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001756 return module
1757}
1758
Paul Duffin42df1442019-03-20 12:45:53 +00001759// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1760func TestHelperLibraryFactory() android.Module {
1761 module := &TestHelperLibrary{}
1762
1763 module.AddProperties(
1764 &module.Module.properties,
1765 &module.Module.deviceProperties,
1766 &module.Module.dexpreoptProperties,
1767 &module.Module.protoProperties,
1768 &module.testHelperLibraryProperties)
1769
Colin Cross9a4abed2019-04-24 13:19:28 -07001770 module.Module.properties.Installable = proptools.BoolPtr(true)
1771 module.Module.dexpreopter.isTest = true
1772
Paul Duffin42df1442019-03-20 12:45:53 +00001773 InitJavaModule(module, android.HostAndDeviceSupported)
1774 return module
1775}
1776
Colin Cross1b16b0e2019-02-12 14:41:32 -08001777// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1778// allow running the test with `atest` or a `TEST_MAPPING` file.
1779//
1780// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1781// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001782func TestHostFactory() android.Module {
1783 module := &Test{}
1784
1785 module.AddProperties(
1786 &module.Module.properties,
1787 &module.Module.protoProperties,
1788 &module.testProperties)
1789
Colin Cross9ae1b922018-06-26 17:59:05 -07001790 module.Module.properties.Installable = proptools.BoolPtr(true)
1791
Colin Cross05638fc2018-04-09 18:40:24 -07001792 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001793 return module
1794}
1795
1796//
Colin Cross2fe66872015-03-30 17:20:39 -07001797// Java Binaries (.jar file plus wrapper script)
1798//
1799
Colin Crossf506d872017-07-19 15:53:04 -07001800type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001801 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001802 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001803
1804 // Name of the class containing main to be inserted into the manifest as Main-Class.
1805 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001806}
1807
Colin Crossf506d872017-07-19 15:53:04 -07001808type Binary struct {
1809 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001810
Colin Crossf506d872017-07-19 15:53:04 -07001811 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001812
Colin Cross6b4a32d2017-12-05 13:42:45 -08001813 isWrapperVariant bool
1814
Colin Crossc3315992017-12-08 19:12:36 -08001815 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001816 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001817}
1818
Alex Light24237172017-10-26 09:46:21 -07001819func (j *Binary) HostToolPath() android.OptionalPath {
1820 return android.OptionalPathForPath(j.binaryFile)
1821}
1822
Colin Crossf506d872017-07-19 15:53:04 -07001823func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001824 if ctx.Arch().ArchType == android.Common {
1825 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001826 if j.binaryProperties.Main_class != nil {
1827 if j.properties.Manifest != nil {
1828 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1829 }
1830 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1831 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1832 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1833 }
1834
Colin Cross6b4a32d2017-12-05 13:42:45 -08001835 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001836 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001837 // Handle the binary wrapper
1838 j.isWrapperVariant = true
1839
Colin Cross366938f2017-12-11 16:29:02 -08001840 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001841 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001842 } else {
1843 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1844 }
1845
1846 // Depend on the installed jar so that the wrapper doesn't get executed by
1847 // another build rule before the jar has been installed.
1848 jarFile := ctx.PrimaryModule().(*Binary).installFile
1849
1850 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1851 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001852 }
Colin Cross2fe66872015-03-30 17:20:39 -07001853}
1854
Colin Crossf506d872017-07-19 15:53:04 -07001855func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001856 if ctx.Arch().ArchType == android.Common {
1857 j.deps(ctx)
1858 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001859}
1860
Colin Cross1b16b0e2019-02-12 14:41:32 -08001861// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1862// as well.
1863//
1864// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1865// compiled against the device bootclasspath.
1866//
1867// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1868// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001869func BinaryFactory() android.Module {
1870 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001871
Colin Cross36242852017-06-23 15:06:31 -07001872 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001873 &module.Module.properties,
1874 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001875 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001876 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001877 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001878
Colin Cross9ae1b922018-06-26 17:59:05 -07001879 module.Module.properties.Installable = proptools.BoolPtr(true)
1880
Colin Cross6b4a32d2017-12-05 13:42:45 -08001881 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1882 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001883 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001884}
1885
Colin Cross1b16b0e2019-02-12 14:41:32 -08001886// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1887//
1888// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1889// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001890func BinaryHostFactory() android.Module {
1891 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001892
Colin Cross36242852017-06-23 15:06:31 -07001893 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001894 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001895 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001896 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001897
Colin Cross9ae1b922018-06-26 17:59:05 -07001898 module.Module.properties.Installable = proptools.BoolPtr(true)
1899
Colin Cross6b4a32d2017-12-05 13:42:45 -08001900 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1901 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001902 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001903}
1904
1905//
1906// Java prebuilts
1907//
1908
Colin Cross74d73e22017-08-02 11:05:49 -07001909type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001910 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001911
Nan Zhangea568a42017-11-08 21:20:04 -08001912 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001913
1914 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001915
1916 // List of shared java libs that this module has dependencies to
1917 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001918
1919 // List of files to remove from the jar file(s)
1920 Exclude_files []string
1921
1922 // List of directories to remove from the jar file(s)
1923 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001924
1925 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001926 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001927}
1928
1929type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001930 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001931 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001932 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001933 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001934 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001935
Colin Cross74d73e22017-08-02 11:05:49 -07001936 properties ImportProperties
1937
Colin Cross0a6e0072017-08-30 14:24:55 -07001938 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001939 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001940}
1941
Colin Cross83bb3162018-06-25 15:48:06 -07001942func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001943 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001944}
1945
1946func (j *Import) minSdkVersion() string {
1947 return j.sdkVersion()
1948}
1949
Colin Cross74d73e22017-08-02 11:05:49 -07001950func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001951 return &j.prebuilt
1952}
1953
Colin Cross74d73e22017-08-02 11:05:49 -07001954func (j *Import) PrebuiltSrcs() []string {
1955 return j.properties.Jars
1956}
1957
1958func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001959 return j.prebuilt.Name(j.ModuleBase.Name())
1960}
1961
Colin Cross74d73e22017-08-02 11:05:49 -07001962func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001963 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001964}
1965
Colin Cross74d73e22017-08-02 11:05:49 -07001966func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001967 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001968
Nan Zhang4c819fb2018-08-27 18:31:46 -07001969 jarName := ctx.ModuleName() + ".jar"
1970 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001971 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1972 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001973 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001974 inputFile := outputFile
1975 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1976 TransformJetifier(ctx, outputFile, inputFile)
1977 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001978 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001979
1980 ctx.VisitDirectDeps(func(module android.Module) {
1981 otherName := ctx.OtherModuleName(module)
1982 tag := ctx.OtherModuleDependencyTag(module)
1983
1984 switch dep := module.(type) {
1985 case Dependency:
1986 switch tag {
1987 case libTag, staticLibTag:
1988 // sdk lib names from dependencies are re-exported
1989 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1990 }
1991 case SdkLibraryDependency:
1992 switch tag {
1993 case libTag:
1994 // names of sdk libs that are directly depended are exported
1995 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1996 }
1997 }
1998 })
1999
2000 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002001 if Bool(j.properties.Installable) {
2002 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2003 ctx.ModuleName()+".jar", outputFile)
2004 }
Colin Cross2fe66872015-03-30 17:20:39 -07002005}
2006
Colin Cross74d73e22017-08-02 11:05:49 -07002007var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002008
Nan Zhanged19fc32017-10-19 13:06:22 -07002009func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002010 if j.combinedClasspathFile == nil {
2011 return nil
2012 }
Colin Cross37f6d792018-07-12 12:28:41 -07002013 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002014}
2015
2016func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002017 if j.combinedClasspathFile == nil {
2018 return nil
2019 }
Colin Cross37f6d792018-07-12 12:28:41 -07002020 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002021}
2022
Colin Cross331a1212018-08-15 20:40:52 -07002023func (j *Import) ResourceJars() android.Paths {
2024 return nil
2025}
2026
2027func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002028 if j.combinedClasspathFile == nil {
2029 return nil
2030 }
Colin Cross331a1212018-08-15 20:40:52 -07002031 return android.Paths{j.combinedClasspathFile}
2032}
2033
Colin Crossf24a22a2019-01-31 14:12:44 -08002034func (j *Import) DexJar() android.Path {
2035 return nil
2036}
2037
Colin Cross74d73e22017-08-02 11:05:49 -07002038func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002039 return nil
2040}
2041
Jiyong Park1be96912018-05-28 18:02:19 +09002042func (j *Import) ExportedSdkLibs() []string {
2043 return j.exportedSdkLibs
2044}
2045
Colin Cross0c4ce212019-05-03 15:28:19 -07002046func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2047 return nil, nil
2048}
2049
albaltai36ff7dc2018-12-25 14:35:23 +08002050// Add compile time check for interface implementation
2051var _ android.IDEInfo = (*Import)(nil)
2052var _ android.IDECustomizedModuleName = (*Import)(nil)
2053
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002054// Collect information for opening IDE project files in java/jdeps.go.
2055const (
2056 removedPrefix = "prebuilt_"
2057)
2058
2059func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2060 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2061}
2062
2063func (j *Import) IDECustomizedModuleName() string {
2064 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2065 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2066 // solution to get the Import name.
2067 name := j.Name()
2068 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002069 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002070 }
2071 return name
2072}
2073
Colin Cross74d73e22017-08-02 11:05:49 -07002074var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002075
Colin Cross1b16b0e2019-02-12 14:41:32 -08002076// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2077//
2078// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2079// compiled against an Android classpath.
2080//
2081// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2082// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002083func ImportFactory() android.Module {
2084 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002085
Colin Cross74d73e22017-08-02 11:05:49 -07002086 module.AddProperties(&module.properties)
2087
2088 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002089 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002090 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002091 android.InitSdkAwareModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002092 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002093}
2094
Colin Cross1b16b0e2019-02-12 14:41:32 -08002095// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2096// module.
2097//
2098// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2099// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002100func ImportFactoryHost() android.Module {
2101 module := &Import{}
2102
2103 module.AddProperties(&module.properties)
2104
2105 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002106 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002107 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002108 return module
2109}
2110
Colin Cross42be7612019-02-21 18:12:14 -08002111// dex_import module
2112
2113type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002114 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002115}
2116
2117type DexImport struct {
2118 android.ModuleBase
2119 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002120 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002121 prebuilt android.Prebuilt
2122
2123 properties DexImportProperties
2124
2125 dexJarFile android.Path
2126 maybeStrippedDexJarFile android.Path
2127
2128 dexpreopter
2129}
2130
2131func (j *DexImport) Prebuilt() *android.Prebuilt {
2132 return &j.prebuilt
2133}
2134
2135func (j *DexImport) PrebuiltSrcs() []string {
2136 return j.properties.Jars
2137}
2138
2139func (j *DexImport) Name() string {
2140 return j.prebuilt.Name(j.ModuleBase.Name())
2141}
2142
Colin Cross42be7612019-02-21 18:12:14 -08002143func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2144 if len(j.properties.Jars) != 1 {
2145 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2146 }
2147
2148 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2149 j.dexpreopter.isInstallable = true
2150 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2151
2152 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2153 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2154
2155 if j.dexpreopter.uncompressedDex {
2156 rule := android.NewRuleBuilder()
2157
2158 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2159 rule.Temporary(temporary)
2160
2161 // use zip2zip to uncompress classes*.dex files
2162 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002163 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002164 FlagWithInput("-i ", inputJar).
2165 FlagWithOutput("-o ", temporary).
2166 FlagWithArg("-0 ", "'classes*.dex'")
2167
2168 // use zipalign to align uncompressed classes*.dex files
2169 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002170 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002171 Flag("-f").
2172 Text("4").
2173 Input(temporary).
2174 Output(dexOutputFile)
2175
2176 rule.DeleteTemporaryFiles()
2177
2178 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2179 } else {
2180 ctx.Build(pctx, android.BuildParams{
2181 Rule: android.Cp,
2182 Input: inputJar,
2183 Output: dexOutputFile,
2184 })
2185 }
2186
2187 j.dexJarFile = dexOutputFile
2188
2189 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2190
2191 j.maybeStrippedDexJarFile = dexOutputFile
2192
2193 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2194 ctx.ModuleName()+".jar", dexOutputFile)
2195}
2196
2197func (j *DexImport) DexJar() android.Path {
2198 return j.dexJarFile
2199}
2200
2201// dex_import imports a `.jar` file containing classes.dex files.
2202//
2203// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2204// to the device.
2205func DexImportFactory() android.Module {
2206 module := &DexImport{}
2207
2208 module.AddProperties(&module.properties)
2209
2210 android.InitPrebuiltModule(module, &module.properties.Jars)
2211 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002212 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002213 return module
2214}
2215
Colin Cross89536d42017-07-07 14:35:50 -07002216//
2217// Defaults
2218//
2219type Defaults struct {
2220 android.ModuleBase
2221 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002222 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002223}
2224
Colin Cross1b16b0e2019-02-12 14:41:32 -08002225// java_defaults provides a set of properties that can be inherited by other java or android modules.
2226//
2227// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2228// property in the defaults module that exists in the depending module will be prepended to the depending module's
2229// value for that property.
2230//
2231// Example:
2232//
2233// java_defaults {
2234// name: "example_defaults",
2235// srcs: ["common/**/*.java"],
2236// javacflags: ["-Xlint:all"],
2237// aaptflags: ["--auto-add-overlay"],
2238// }
2239//
2240// java_library {
2241// name: "example",
2242// defaults: ["example_defaults"],
2243// srcs: ["example/**/*.java"],
2244// }
2245//
2246// is functionally identical to:
2247//
2248// java_library {
2249// name: "example",
2250// srcs: [
2251// "common/**/*.java",
2252// "example/**/*.java",
2253// ],
2254// javacflags: ["-Xlint:all"],
2255// }
Colin Cross89536d42017-07-07 14:35:50 -07002256func defaultsFactory() android.Module {
2257 return DefaultsFactory()
2258}
2259
2260func DefaultsFactory(props ...interface{}) android.Module {
2261 module := &Defaults{}
2262
2263 module.AddProperties(props...)
2264 module.AddProperties(
2265 &CompilerProperties{},
2266 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002267 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002268 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002269 &aaptProperties{},
2270 &androidLibraryProperties{},
2271 &appProperties{},
2272 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002273 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002274 &ImportProperties{},
2275 &AARImportProperties{},
2276 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002277 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002278 )
2279
2280 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002281 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002282 return module
2283}
Nan Zhangea568a42017-11-08 21:20:04 -08002284
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002285func kytheExtractJavaFactory() android.Singleton {
2286 return &kytheExtractJavaSingleton{}
2287}
2288
2289type kytheExtractJavaSingleton struct {
2290}
2291
2292func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2293 var xrefTargets android.Paths
2294 ctx.VisitAllModules(func(module android.Module) {
2295 if javaModule, ok := module.(xref); ok {
2296 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2297 }
2298 })
2299 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2300 if len(xrefTargets) > 0 {
2301 ctx.Build(pctx, android.BuildParams{
2302 Rule: blueprint.Phony,
2303 Output: android.PathForPhony(ctx, "xref_java"),
2304 Inputs: xrefTargets,
2305 })
2306 }
2307}
2308
Nan Zhangea568a42017-11-08 21:20:04 -08002309var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002310var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002311var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002312var inList = android.InList