blob: 898c4dd050b120ee46c206eedc22b1b0199b611d [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
Inseob Kim42882742019-07-30 17:55:33 +0900187 Sysprop struct {
188 Platform *bool
189 } `blueprint:"mutated"`
190
Colin Crosscb933592017-11-22 13:49:43 -0800191 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800192
193 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800194 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700195}
196
Colin Cross89536d42017-07-07 14:35:50 -0700197type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700198 // list of module-specific flags that will be used for dex compiles
199 Dxflags []string `android:"arch_variant"`
200
Jeongik Cha538c0d02019-07-11 15:54:27 +0900201 // if not blank, set to the version of the sdk to compile against.
202 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800203 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700204
Colin Cross83bb3162018-06-25 15:48:06 -0700205 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
206 // Defaults to sdk_version if not set.
207 Min_sdk_version *string
208
Dan Willemsen419290a2018-10-31 15:28:47 -0700209 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
210 // Defaults to sdk_version if not set.
211 Target_sdk_version *string
212
Jeongik Cha356dac42019-08-19 14:09:52 +0900213 // Whether to compile against the platform APIs instead of an SDK.
214 // If true, then sdk_version must be empty. The value of this field
215 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700216 Platform_apis *bool
217
Colin Crossebe1a512017-11-14 13:12:14 -0800218 Aidl struct {
219 // Top level directories to pass to aidl tool
220 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700221
Colin Crossebe1a512017-11-14 13:12:14 -0800222 // Directories rooted at the Android.bp file to pass to aidl tool
223 Local_include_dirs []string
224
225 // directories that should be added as include directories for any aidl sources of modules
226 // that depend on this module, as well as to aidl for this module.
227 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100228
229 // whether to generate traces (for systrace) for this interface
230 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100231
232 // whether to generate Binder#GetTransaction name method.
233 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800234 }
Colin Cross92430102017-10-09 14:59:32 -0700235
236 // If true, export a copy of the module as a -hostdex module for host testing.
237 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700238
Colin Cross7f87f4f2019-04-24 13:41:45 -0700239 Target struct {
240 Hostdex struct {
241 // Additional required dependencies to add to -hostdex modules.
242 Required []string
243 }
244 }
245
David Brazdil17ef5632018-06-27 10:27:45 +0100246 // If set to true, compile dex regardless of installable. Defaults to false.
247 Compile_dex *bool
248
Colin Cross66dbc0b2017-12-28 12:23:20 -0800249 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700250 // If false, disable all optimization. Defaults to true for android_app and android_test
251 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800252 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700253 // True if the module containing this has it set by default.
254 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800255
256 // If true, optimize for size by removing unused code. Defaults to true for apps,
257 // false for libraries and tests.
258 Shrink *bool
259
260 // If true, optimize bytecode. Defaults to false.
261 Optimize *bool
262
263 // If true, obfuscate bytecode. Defaults to false.
264 Obfuscate *bool
265
266 // If true, do not use the flag files generated by aapt that automatically keep
267 // classes referenced by the app manifest. Defaults to false.
268 No_aapt_flags *bool
269
270 // Flags to pass to proguard.
271 Proguard_flags []string
272
273 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800274 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800275 }
276
Colin Cross1369cdb2017-09-29 17:58:17 -0700277 // When targeting 1.9, override the modules to use with --system
278 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700279
280 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800281 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700282}
283
Sasha Smundak2057f822019-04-16 17:16:58 -0700284func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
285 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
286}
287
Colin Cross46c9b8b2017-06-22 16:51:17 -0700288// Module contains the properties and members used by all java module types
289type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700290 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700291 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900292 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900293 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700294
Colin Cross89536d42017-07-07 14:35:50 -0700295 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700296 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700297 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700298
Colin Cross331a1212018-08-15 20:40:52 -0700299 // jar file containing header classes including static library dependencies, suitable for
300 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700301 headerJarFile android.Path
302
Colin Cross331a1212018-08-15 20:40:52 -0700303 // jar file containing implementation classes including static library dependencies but no
304 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700305 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700306
Colin Cross331a1212018-08-15 20:40:52 -0700307 // jar file containing only resources including from static library dependencies
308 resourceJar android.Path
309
Colin Cross0c4ce212019-05-03 15:28:19 -0700310 // args and dependencies to package source files into a srcjar
311 srcJarArgs []string
312 srcJarDeps android.Paths
313
Colin Cross331a1212018-08-15 20:40:52 -0700314 // jar file containing implementation classes and resources including static library
315 // dependencies
316 implementationAndResourcesJar android.Path
317
318 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700319 dexJarFile android.Path
320
Colin Cross43f08db2018-11-12 10:13:39 -0800321 // output file that contains classes.dex if it should be in the output file
322 maybeStrippedDexJarFile android.Path
323
Colin Crosscb933592017-11-22 13:49:43 -0800324 // output file containing uninstrumented classes that will be instrumented by jacoco
325 jacocoReportClassesFile android.Path
326
Colin Cross66dbc0b2017-12-28 12:23:20 -0800327 // output file containing mapping of obfuscated names
328 proguardDictionary android.Path
329
Colin Cross331a1212018-08-15 20:40:52 -0700330 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700331 outputFile android.Path
332 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700333
Colin Cross635c3b02016-05-18 15:37:25 -0700334 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700335
Colin Cross635c3b02016-05-18 15:37:25 -0700336 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700337
Colin Cross2fe66872015-03-30 17:20:39 -0700338 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700339 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800340
341 // list of .java files and srcjars that was passed to javac
342 compiledJavaSrcs android.Paths
343 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800344
345 // list of extra progurad flag files
346 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900347
Colin Cross094054a2018-10-17 15:10:48 -0700348 // manifest file to use instead of properties.Manifest
349 overrideManifest android.OptionalPath
350
Jiyong Park1be96912018-05-28 18:02:19 +0900351 // list of SDK lib names that this java moudule is exporting
352 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700353
354 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
355 // filter out Exclude_srcs, will be used by android.IDEInfo struct
356 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800357
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800358 // expanded Jarjar_rules
359 expandJarjarRules android.Path
360
Vladimir Marko0975ee02019-04-02 10:29:55 +0100361 // list of additional targets for checkbuild
362 additionalCheckedModules android.Paths
363
Colin Cross988708c2019-05-06 14:04:11 -0700364 // Extra files generated by the module type to be added as java resources.
365 extraResources android.Paths
366
Colin Crossf24a22a2019-01-31 14:12:44 -0800367 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800368 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800369
370 // list of the xref extraction files
371 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700372}
373
Colin Cross41955e82019-05-29 14:40:35 -0700374func (j *Module) OutputFiles(tag string) (android.Paths, error) {
375 switch tag {
376 case "":
377 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700378 case ".jar":
379 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700380 case ".proguard_map":
381 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700382 default:
383 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
384 }
Colin Cross54250902017-12-05 09:28:08 -0800385}
386
Jiyong Park8fd61922018-11-08 02:50:25 +0900387func (j *Module) DexJarFile() android.Path {
388 return j.dexJarFile
389}
390
Colin Cross41955e82019-05-29 14:40:35 -0700391var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800392
Colin Crossf506d872017-07-19 15:53:04 -0700393type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700394 HeaderJars() android.Paths
395 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700396 ResourceJars() android.Paths
397 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800398 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700399 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900400 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700401 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700402 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700403}
404
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700406 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
407 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900408}
409
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800410type xref interface {
411 XrefJavaFiles() android.Paths
412}
413
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800414func (j *Module) XrefJavaFiles() android.Paths {
415 return j.kytheFiles
416}
417
Colin Cross89536d42017-07-07 14:35:50 -0700418func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
419 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
420 android.InitDefaultableModule(module)
421}
422
Colin Crossbe1da472017-07-07 15:59:46 -0700423type dependencyTag struct {
424 blueprint.BaseDependencyTag
425 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700426}
427
Colin Crossa4f08812018-10-02 22:03:40 -0700428type jniDependencyTag struct {
429 blueprint.BaseDependencyTag
430 target android.Target
431}
432
Colin Crossbe1da472017-07-07 15:59:46 -0700433var (
Colin Cross4b964c02018-10-15 16:18:06 -0700434 staticLibTag = dependencyTag{name: "staticlib"}
435 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800436 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700437 bootClasspathTag = dependencyTag{name: "bootclasspath"}
438 systemModulesTag = dependencyTag{name: "system modules"}
439 frameworkResTag = dependencyTag{name: "framework-res"}
440 frameworkApkTag = dependencyTag{name: "framework-apk"}
441 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800442 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700443 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
444 certificateTag = dependencyTag{name: "certificate"}
445 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700446 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700447)
Colin Cross2fe66872015-03-30 17:20:39 -0700448
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900449func defaultSdkVersion(ctx checkVendorModuleContext) string {
450 if ctx.SocSpecific() || ctx.DeviceSpecific() {
451 return "system_current"
452 }
453 return ""
454}
455
456type checkVendorModuleContext interface {
457 SocSpecific() bool
458 DeviceSpecific() bool
459}
460
Colin Crossfc3674a2017-09-18 17:41:52 -0700461type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700462 useModule, useFiles, useDefaultLibs, invalidVersion bool
463
Colin Cross86a60ae2018-05-29 14:44:55 -0700464 modules []string
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
503func (j *Module) minSdkVersion() string {
504 if j.deviceProperties.Min_sdk_version != nil {
505 return *j.deviceProperties.Min_sdk_version
506 }
507 return j.sdkVersion()
508}
509
Dan Willemsen419290a2018-10-31 15:28:47 -0700510func (j *Module) targetSdkVersion() string {
511 if j.deviceProperties.Target_sdk_version != nil {
512 return *j.deviceProperties.Target_sdk_version
513 }
514 return j.sdkVersion()
515}
516
Colin Crossbe1da472017-07-07 15:59:46 -0700517func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700518 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100519 sdkDep := decodeSdkDep(ctx, sdkContext(j))
520 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700521 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700522 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100523 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100524 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700525 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700526 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700527 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100528 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700529 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700530 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700531 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
532 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800533 }
Colin Crossbe1da472017-07-07 15:59:46 -0700534 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700535 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100536 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100537 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100538 } else if *j.deviceProperties.System_modules != "none" {
Paul Duffin68289b02019-09-20 13:50:52 +0100539 // Add the system modules to both the system modules and bootclasspath.
Colin Cross42d48b72018-08-29 14:10:52 -0700540 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Paul Duffin68289b02019-09-20 13:50:52 +0100541 ctx.AddVariationDependencies(nil, bootClasspathTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700542 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800543 if ctx.ModuleName() == "android_stubs_current" ||
544 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700545 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700546 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800547 }
Colin Cross2fe66872015-03-30 17:20:39 -0700548 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700549
Colin Cross42d48b72018-08-29 14:10:52 -0700550 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
551 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700552
Colin Crossbe9cdb82019-01-21 21:37:16 -0800553 ctx.AddFarVariationDependencies([]blueprint.Variation{
554 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
555 }, pluginTag, j.properties.Plugins...)
556
Colin Crossfe17f6f2019-03-28 19:30:56 -0700557 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700558 if j.hasSrcExt(".proto") {
559 protoDeps(ctx, &j.protoProperties)
560 }
Colin Cross93e85952017-08-15 13:34:18 -0700561
562 if j.hasSrcExt(".kt") {
563 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
564 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700565 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
566 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800567 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800568 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
569 }
Colin Cross93e85952017-08-15 13:34:18 -0700570 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800571
572 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700573 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800574 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700575}
576
577func hasSrcExt(srcs []string, ext string) bool {
578 for _, src := range srcs {
579 if filepath.Ext(src) == ext {
580 return true
581 }
582 }
583
584 return false
585}
586
Nan Zhang61eaedb2017-11-02 13:28:15 -0700587func shardPaths(paths android.Paths, shardSize int) []android.Paths {
588 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
589 for len(paths) > shardSize {
590 ret = append(ret, paths[0:shardSize])
591 paths = paths[shardSize:]
592 }
593 if len(paths) > 0 {
594 ret = append(ret, paths)
595 }
596 return ret
597}
598
Colin Cross6af17aa2017-09-20 12:59:05 -0700599func (j *Module) hasSrcExt(ext string) bool {
600 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700601}
602
Colin Cross46c9b8b2017-06-22 16:51:17 -0700603func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700604 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700605
Colin Crossebe1a512017-11-14 13:12:14 -0800606 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
607 aidlIncludes = append(aidlIncludes,
608 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
609 aidlIncludes = append(aidlIncludes,
610 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700611
Colin Cross3047fa22019-04-18 10:56:44 -0700612 var flags []string
613 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700614
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700615 if aidlPreprocess.Valid() {
616 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700617 deps = append(deps, aidlPreprocess.Path())
618 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700619 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700620 }
621
Colin Cross3047fa22019-04-18 10:56:44 -0700622 if len(j.exportAidlIncludeDirs) > 0 {
623 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
624 }
625
626 if len(aidlIncludes) > 0 {
627 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
628 }
629
Colin Cross635c3b02016-05-18 15:37:25 -0700630 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800631 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700632 flags = append(flags, "-I"+src.String())
633 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700634
Martijn Coeneneab15642018-03-09 09:29:59 +0100635 if Bool(j.deviceProperties.Aidl.Generate_traces) {
636 flags = append(flags, "-t")
637 }
638
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100639 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
640 flags = append(flags, "--transaction_names")
641 }
642
Colin Cross3047fa22019-04-18 10:56:44 -0700643 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700644}
645
Colin Cross32f676a2017-09-06 13:41:06 -0700646type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800647 classpath classpath
648 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700649 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800650 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700651 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700652 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700653 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700654 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800655 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700656 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700657 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700658 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700659 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800660 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800661
662 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700663}
Colin Cross2fe66872015-03-30 17:20:39 -0700664
Colin Cross54250902017-12-05 09:28:08 -0800665func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
666 for _, f := range dep.Srcs() {
667 if f.Ext() != ".jar" {
668 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
669 ctx.OtherModuleName(dep.(blueprint.Module)))
670 }
671 }
672}
673
Jiyong Park2d492942018-03-05 17:44:10 +0900674type linkType int
675
676const (
677 javaCore linkType = iota
678 javaSdk
679 javaSystem
680 javaPlatform
681)
682
Jiyong Park46f78fb2018-10-20 16:33:17 +0900683func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700684 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700685 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900686 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
687 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100688 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900689 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100690 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900691 return javaCore, false
692 case name == "android_system_stubs_current":
693 return javaSystem, true
694 case strings.HasPrefix(ver, "system_"):
695 return javaSystem, false
696 case name == "android_test_stubs_current":
697 return javaSystem, true
698 case strings.HasPrefix(ver, "test_"):
699 return javaPlatform, false
700 case name == "android_stubs_current":
701 return javaSdk, true
702 case ver == "current":
703 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100704 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900705 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700706 default:
707 if _, err := strconv.Atoi(ver); err != nil {
708 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
709 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900710 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900711 }
712}
713
Jiyong Park750e5572018-01-31 00:20:13 +0900714func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700715 if ctx.Host() {
716 return
717 }
718
Jiyong Park46f78fb2018-10-20 16:33:17 +0900719 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
720 if stubs {
721 return
722 }
723 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900724 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."
725
726 switch myLinkType {
727 case javaCore:
728 if otherLinkType != javaCore {
729 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 +0900730 ctx.OtherModuleName(to))
731 }
Jiyong Park2d492942018-03-05 17:44:10 +0900732 break
733 case javaSdk:
734 if otherLinkType != javaCore && otherLinkType != javaSdk {
735 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
736 ctx.OtherModuleName(to))
737 }
738 break
739 case javaSystem:
740 if otherLinkType == javaPlatform {
741 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
742 ctx.OtherModuleName(to))
743 }
744 break
745 case javaPlatform:
746 // no restriction on link-type
747 break
Jiyong Park750e5572018-01-31 00:20:13 +0900748 }
749}
750
Colin Cross32f676a2017-09-06 13:41:06 -0700751func (j *Module) collectDeps(ctx android.ModuleContext) deps {
752 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700753
Colin Cross300f0382018-03-06 13:11:51 -0800754 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700755 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800756 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700757 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800758 } else if sdkDep.useFiles {
759 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700760 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700761 deps.aidlPreprocess = sdkDep.aidl
762 } else {
763 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800764 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700765 }
766
Colin Crossd11fcda2017-10-23 17:59:01 -0700767 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700768 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700769 tag := ctx.OtherModuleDependencyTag(module)
770
Colin Crossa4f08812018-10-02 22:03:40 -0700771 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700772 // Handled by AndroidApp.collectAppDeps
773 return
774 }
775 if tag == certificateTag {
776 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700777 return
778 }
779
Jiyong Park750e5572018-01-31 00:20:13 +0900780 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700781 switch tag {
782 case bootClasspathTag, libTag, staticLibTag:
783 checkLinkType(ctx, j, to, tag.(dependencyTag))
784 }
Jiyong Park750e5572018-01-31 00:20:13 +0900785 }
Colin Cross54250902017-12-05 09:28:08 -0800786 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800787 case SdkLibraryDependency:
788 switch tag {
789 case libTag:
790 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
791 // names of sdk libs that are directly depended are exported
792 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700793 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800794 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
795 }
Colin Cross54250902017-12-05 09:28:08 -0800796 case Dependency:
797 switch tag {
798 case bootClasspathTag:
799 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700800 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800801 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900802 // sdk lib names from dependencies are re-exported
803 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700804 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800805 case staticLibTag:
806 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
807 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
808 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700809 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900810 // sdk lib names from dependencies are re-exported
811 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700812 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800813 case pluginTag:
814 if plugin, ok := dep.(*Plugin); ok {
815 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
816 if plugin.pluginProperties.Processor_class != nil {
817 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
818 }
819 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
820 } else {
821 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
822 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800823 case frameworkApkTag:
824 if ctx.ModuleName() == "android_stubs_current" ||
825 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700826 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800827 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
828 // resource files out of there for aapt.
829 //
830 // Normally the package rule runs aapt, which includes the resource,
831 // but we're not running that in our package rule so just copy in the
832 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700833 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800834 }
Colin Cross54250902017-12-05 09:28:08 -0800835 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700836 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800837 case kotlinAnnotationsTag:
838 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800839 }
840
Colin Cross54250902017-12-05 09:28:08 -0800841 case android.SourceFileProducer:
842 switch tag {
843 case libTag:
844 checkProducesJars(ctx, dep)
845 deps.classpath = append(deps.classpath, dep.Srcs()...)
846 case staticLibTag:
847 checkProducesJars(ctx, dep)
848 deps.classpath = append(deps.classpath, dep.Srcs()...)
849 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
850 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800851 }
852 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700853 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100854 case bootClasspathTag:
855 // If a system modules dependency has been added to the bootclasspath
856 // then add its libs to the bootclasspath.
857 sm := module.(*SystemModules)
858 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
859
Colin Cross1369cdb2017-09-29 17:58:17 -0700860 case systemModulesTag:
861 if deps.systemModules != nil {
862 panic("Found two system module dependencies")
863 }
864 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000865 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700866 panic("Missing directory for system module dependency")
867 }
Colin Crossb77043e2019-07-16 13:57:13 -0700868 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700869 }
Colin Crossec7a0422017-07-07 14:47:12 -0700870 }
Colin Cross2fe66872015-03-30 17:20:39 -0700871 })
872
Jiyong Park1be96912018-05-28 18:02:19 +0900873 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
874
Colin Cross32f676a2017-09-06 13:41:06 -0700875 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700876}
877
Colin Cross83bb3162018-06-25 15:48:06 -0700878func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700879 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800880 v := sdkContext.sdkVersion()
881 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100882 if ctx.Config().IsPdkBuild() &&
883 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700884 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800885 latestSdkVersion := 0
886 if len(sdkVersions) > 0 {
887 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
888 }
889 v = strconv.Itoa(latestSdkVersion)
890 }
891
892 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700893 if err != nil {
894 ctx.PropertyErrorf("sdk_version", "%s", err)
895 }
Nan Zhang357466b2018-04-17 17:38:36 -0700896 if javaVersion != "" {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100897 ret = normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700898 } else if ctx.Device() && sdk <= 23 {
899 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100900 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700901 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100902 } else if ctx.Device() &&
903 sdkContext.sdkVersion() != "" &&
904 sdkContext.sdkVersion() != "none" &&
905 sdkContext.sdkVersion() != "core_platform" &&
906 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700907 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
908 ret = "1.8"
909 } else {
910 ret = "1.9"
911 }
912
913 return ret
914}
915
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100916func normalizeJavaVersion(ctx android.ModuleContext, javaVersion string) string {
917 switch javaVersion {
918 case "1.6", "6":
919 return "1.6"
920 case "1.7", "7":
921 return "1.7"
922 case "1.8", "8":
923 return "1.8"
924 case "1.9", "9":
925 return "1.9"
926 case "10", "11":
927 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
928 return "unsupported"
929 default:
930 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
931 return "unrecognized"
932 }
933}
934
Nan Zhanged19fc32017-10-19 13:06:22 -0700935func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700936
Colin Crossf03c82b2015-04-13 13:53:40 -0700937 var flags javaBuilderFlags
938
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100939 // javaVersion flag.
940 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
941
Nan Zhanged19fc32017-10-19 13:06:22 -0700942 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700943 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100944 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700945 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700946 }
Colin Cross6510f912017-11-29 00:27:14 -0800947 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700948 // Override the -g flag passed globally to remove local variable debug info to reduce
949 // disk and memory usage.
950 javacFlags = append(javacFlags, "-g:source,lines")
951 }
Colin Cross64162712017-08-08 13:17:59 -0700952
Colin Cross66548102018-06-19 22:47:35 -0700953 if ctx.Config().RunErrorProne() {
954 if config.ErrorProneClasspath == nil {
955 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
956 }
957
958 errorProneFlags := []string{
959 "-Xplugin:ErrorProne",
960 "${config.ErrorProneChecks}",
961 }
962 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
963
964 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
965 "'" + strings.Join(errorProneFlags, " ") + "'"
966 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800967 }
968
Nan Zhanged19fc32017-10-19 13:06:22 -0700969 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800970 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
971 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700972 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800973
Colin Crossbe9cdb82019-01-21 21:37:16 -0800974 flags.processor = strings.Join(deps.processorClasses, ",")
975
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100976 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100977 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800978 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
979 // Give host-side tools a version of OpenJDK's standard libraries
980 // close to what they're targeting. As of Dec 2017, AOSP is only
981 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
982 //
983 // When building with OpenJDK 8, the following should have no
984 // effect since those jars would be available by default.
985 //
986 // When building with OpenJDK 9 but targeting a version < 1.8,
987 // putting them on the bootclasspath means that:
988 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
989 // b) references to existing APIs are not reinterpreted in an
990 // OpenJDK 9-specific way, eg. calls to subclasses of
991 // java.nio.Buffer as in http://b/70862583
992 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
993 flags.bootClasspath = append(flags.bootClasspath,
994 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
995 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800996 if Bool(j.properties.Use_tools_jar) {
997 flags.bootClasspath = append(flags.bootClasspath,
998 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
999 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001000 }
1001
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001002 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001003 // Manually specify build directory in case it is not under the repo root.
1004 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1005 // just adding a symlink under the root doesn't help.)
1006 patchPaths := ".:" + ctx.Config().BuildDir()
1007 classPath := flags.classpath.FormJavaClassPath("")
1008 if classPath != "" {
1009 patchPaths += ":" + classPath
1010 }
1011 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001012 }
1013
Nan Zhanged19fc32017-10-19 13:06:22 -07001014 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001015 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001016
Nan Zhanged19fc32017-10-19 13:06:22 -07001017 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001018 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001019
Colin Cross81440082018-08-15 20:21:55 -07001020 if len(javacFlags) > 0 {
1021 // optimization.
1022 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1023 flags.javacFlags = "$javacFlags"
1024 }
1025
Nan Zhanged19fc32017-10-19 13:06:22 -07001026 return flags
1027}
Colin Crossc0b06f12015-04-08 13:03:43 -07001028
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001029func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001030 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001031
1032 deps := j.collectDeps(ctx)
1033 flags := j.collectBuilderFlags(ctx, deps)
1034
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001035 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001036 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1037 }
Colin Cross8a497952019-03-05 22:25:09 -08001038 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001039 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001040 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001041 }
1042
Colin Crossaf050172017-11-15 23:01:59 -08001043 srcFiles = j.genSources(ctx, srcFiles, flags)
1044
1045 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001046 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001047 if aaptSrcJar != nil {
1048 srcJars = append(srcJars, aaptSrcJar)
1049 }
Colin Crossb7a63242015-04-16 14:09:14 -07001050
Jiyong Parkb5ddfa92019-08-23 19:12:42 +09001051 // Collect source files and filter out Exclude_srcs that IDEInfo struct will use
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001052 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1053
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001054 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001055 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001056 }
1057
Colin Cross1ee23172017-10-18 14:44:18 -07001058 jarName := ctx.ModuleName() + ".jar"
1059
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001060 javaSrcFiles := srcFiles.FilterByExt(".java")
1061 var uniqueSrcFiles android.Paths
1062 set := make(map[string]bool)
1063 for _, v := range javaSrcFiles {
1064 if _, found := set[v.String()]; !found {
1065 set[v.String()] = true
1066 uniqueSrcFiles = append(uniqueSrcFiles, v)
1067 }
1068 }
1069
Colin Cross55f63ea2018-08-27 12:37:09 -07001070 var kotlinJars android.Paths
1071
Colin Cross93e85952017-08-15 13:34:18 -07001072 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001073 // user defined kotlin flags.
1074 kotlincFlags := j.properties.Kotlincflags
1075 CheckKotlincFlags(ctx, kotlincFlags)
1076
Colin Cross93e85952017-08-15 13:34:18 -07001077 // If there are kotlin files, compile them first but pass all the kotlin and java files
1078 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1079 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001080 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001081 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001082 kotlincFlags = append(kotlincFlags, "-no-jdk")
1083 }
1084 if len(kotlincFlags) > 0 {
1085 // optimization.
1086 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1087 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001088 }
1089
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001090 var kotlinSrcFiles android.Paths
1091 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1092 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1093
Colin Crossafbb1732019-01-17 15:42:52 -08001094 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1095 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1096
1097 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1098 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1099
1100 if len(flags.processorPath) > 0 {
1101 // Use kapt for annotation processing
1102 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1103 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1104 srcJars = append(srcJars, kaptSrcJar)
1105 // Disable annotation processing in javac, it's already been handled by kapt
1106 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001107 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001108 }
Colin Cross93e85952017-08-15 13:34:18 -07001109
Colin Cross1ee23172017-10-18 14:44:18 -07001110 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001111 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001112 if ctx.Failed() {
1113 return
1114 }
1115
1116 // Make javac rule depend on the kotlinc rule
1117 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001118
Colin Cross93e85952017-08-15 13:34:18 -07001119 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001120 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001121 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001122 }
1123
Colin Cross55f63ea2018-08-27 12:37:09 -07001124 jars := append(android.Paths(nil), kotlinJars...)
1125
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001126 // Store the list of .java files that was passed to javac
1127 j.compiledJavaSrcs = uniqueSrcFiles
1128 j.compiledSrcJars = srcJars
1129
Nan Zhang61eaedb2017-11-02 13:28:15 -07001130 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001131 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001132 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1133 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001134 // Formerly, there was a check here that prevented annotation processors
1135 // from being used when sharding was enabled, as some annotation processors
1136 // do not function correctly in sharded environments. It was removed to
1137 // allow for the use of annotation processors that do function correctly
1138 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001139 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001140 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001141 if ctx.Failed() {
1142 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001143 }
1144 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001145 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001146 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001147 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001148 // If error-prone is enabled, add an additional rule to compile the java files into
1149 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001150 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001151 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1152 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001153 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001154 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001155 extraJarDeps = append(extraJarDeps, errorprone)
1156 }
1157
Nan Zhang61eaedb2017-11-02 13:28:15 -07001158 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001159 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001160 shardSize := int(*(j.properties.Javac_shard_size))
1161 var shardSrcs []android.Paths
1162 if len(uniqueSrcFiles) > 0 {
1163 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1164 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001165 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1166 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001167 jars = append(jars, classes)
1168 }
1169 }
1170 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001171 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1172 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001173 jars = append(jars, classes)
1174 }
1175 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001176 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001177 jars = append(jars, classes)
1178 }
Colin Crossd6891432017-09-27 17:39:56 -07001179 if ctx.Failed() {
1180 return
1181 }
Colin Cross2fe66872015-03-30 17:20:39 -07001182 }
1183
Colin Cross0c4ce212019-05-03 15:28:19 -07001184 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1185
1186 var includeSrcJar android.WritablePath
1187 if Bool(j.properties.Include_srcs) {
1188 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1189 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1190 }
1191
Colin Crosscedd4762018-09-13 11:26:19 -07001192 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1193 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001194 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001195 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001196
1197 var resArgs []string
1198 var resDeps android.Paths
1199
1200 resArgs = append(resArgs, dirArgs...)
1201 resDeps = append(resDeps, dirDeps...)
1202
1203 resArgs = append(resArgs, fileArgs...)
1204 resDeps = append(resDeps, fileDeps...)
1205
Colin Cross988708c2019-05-06 14:04:11 -07001206 resArgs = append(resArgs, extraArgs...)
1207 resDeps = append(resDeps, extraDeps...)
1208
Colin Cross40a36712017-09-27 17:41:35 -07001209 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001210 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001211 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001212 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001213 if ctx.Failed() {
1214 return
1215 }
1216 }
1217
Colin Cross0c4ce212019-05-03 15:28:19 -07001218 var resourceJars android.Paths
1219 if j.resourceJar != nil {
1220 resourceJars = append(resourceJars, j.resourceJar)
1221 }
1222 if Bool(j.properties.Include_srcs) {
1223 resourceJars = append(resourceJars, includeSrcJar)
1224 }
1225 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001226
Colin Cross0c4ce212019-05-03 15:28:19 -07001227 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001228 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001229 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001230 false, nil, nil)
1231 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001232 } else if len(resourceJars) == 1 {
1233 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001234 }
1235
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001236 if len(deps.staticJars) > 0 {
1237 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001238 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001239
Colin Cross094054a2018-10-17 15:10:48 -07001240 manifest := j.overrideManifest
1241 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001242 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001243 }
Colin Cross635acc92017-09-12 22:50:46 -07001244
Colin Cross8a497952019-03-05 22:25:09 -08001245 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001246 if len(services) > 0 {
1247 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1248 var zipargs []string
1249 for _, file := range services {
1250 serviceFile := file.String()
1251 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1252 }
1253 ctx.Build(pctx, android.BuildParams{
1254 Rule: zip,
1255 Output: servicesJar,
1256 Implicits: services,
1257 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001258 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001259 },
1260 })
1261 jars = append(jars, servicesJar)
1262 }
1263
Colin Cross0a6e0072017-08-30 14:24:55 -07001264 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001265 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001266 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001267
1268 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001269 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1270 // Optimization: skip the combine step if there is nothing to do
1271 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1272 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1273 // any if len(jars) == 1.
1274 outputFile = moduleOutPath
1275 } else {
1276 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1277 ctx.Build(pctx, android.BuildParams{
1278 Rule: android.Cp,
1279 Input: jars[0],
1280 Output: combinedJar,
1281 })
1282 outputFile = combinedJar
1283 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001284 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001285 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001286 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001287 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001288 outputFile = combinedJar
1289 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001290
Colin Cross331a1212018-08-15 20:40:52 -07001291 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001292 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001293 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001294 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001295 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001296 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001297
1298 // jarjar resource jar if necessary
1299 if j.resourceJar != nil {
1300 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001301 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001302 j.resourceJar = resourceJarJarFile
1303 }
1304
Colin Cross0a6e0072017-08-30 14:24:55 -07001305 if ctx.Failed() {
1306 return
1307 }
1308 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001309
1310 // Check package restrictions if necessary.
1311 if len(j.properties.Permitted_packages) > 0 {
1312 // Check packages and copy to package-checked file.
1313 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1314 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1315 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1316
1317 if ctx.Failed() {
1318 return
1319 }
1320 }
1321
Nan Zhanged19fc32017-10-19 13:06:22 -07001322 j.implementationJarFile = outputFile
1323 if j.headerJarFile == nil {
1324 j.headerJarFile = j.implementationJarFile
1325 }
Colin Cross2fe66872015-03-30 17:20:39 -07001326
Colin Cross6510f912017-11-29 00:27:14 -08001327 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001328 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1329 j.properties.Instrument = true
1330 }
1331 }
1332
Colin Cross3144dfc2018-01-03 15:06:47 -08001333 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001334 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1335 }
1336
Colin Cross331a1212018-08-15 20:40:52 -07001337 // merge implementation jar with resources if necessary
1338 implementationAndResourcesJar := outputFile
1339 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001340 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001341 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001342 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001343 false, nil, nil)
1344 implementationAndResourcesJar = combinedJar
1345 }
1346
1347 j.implementationAndResourcesJar = implementationAndResourcesJar
1348
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001349 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001350 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001351 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001352 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001353 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001354 if ctx.Failed() {
1355 return
1356 }
Colin Cross331a1212018-08-15 20:40:52 -07001357
Jiyong Park09cb6292019-07-15 15:29:23 +09001358 // Hidden API CSV generation and dex encoding
1359 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1360 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001361
Colin Cross331a1212018-08-15 20:40:52 -07001362 // merge dex jar with resources if necessary
1363 if j.resourceJar != nil {
1364 jars := android.Paths{dexOutputFile, j.resourceJar}
1365 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1366 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1367 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001368 if j.deviceProperties.UncompressDex {
1369 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1370 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1371 dexOutputFile = combinedAlignedJar
1372 } else {
1373 dexOutputFile = combinedJar
1374 }
Colin Cross331a1212018-08-15 20:40:52 -07001375 }
1376
1377 j.dexJarFile = dexOutputFile
1378
Colin Cross8faf8fc2019-01-16 15:15:52 -08001379 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001380 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1381
1382 j.maybeStrippedDexJarFile = dexOutputFile
1383
Colin Cross3063b782018-08-15 11:19:12 -07001384 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001385
1386 if ctx.Failed() {
1387 return
1388 }
Colin Cross331a1212018-08-15 20:40:52 -07001389 } else {
1390 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001391 }
Colin Cross331a1212018-08-15 20:40:52 -07001392
Colin Crossb7a63242015-04-16 14:09:14 -07001393 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001394
1395 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1396 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001397}
1398
Colin Cross3b706fd2019-09-05 16:44:18 -07001399func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1400 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1401
1402 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1403 if idx >= 0 {
1404 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1405 jarName += strconv.Itoa(idx)
1406 }
1407
1408 classes := android.PathForModuleOut(ctx, "javac", jarName)
1409 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1410
1411 if ctx.Config().EmitXrefRules() {
1412 extractionFile := android.PathForModuleOut(ctx, kzipName)
1413 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1414 j.kytheFiles = append(j.kytheFiles, extractionFile)
1415 }
1416
1417 return classes
1418}
1419
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001420// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1421// since some of these flags may be used internally.
1422func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1423 for _, flag := range flags {
1424 flag = strings.TrimSpace(flag)
1425
1426 if !strings.HasPrefix(flag, "-") {
1427 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1428 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1429 ctx.PropertyErrorf("kotlincflags",
1430 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1431 } else if inList(flag, config.KotlincIllegalFlags) {
1432 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1433 } else if flag == "-include-runtime" {
1434 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1435 } else {
1436 args := strings.Split(flag, " ")
1437 if args[0] == "-kotlin-home" {
1438 ctx.PropertyErrorf("kotlincflags",
1439 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1440 }
1441 }
1442 }
1443}
1444
Colin Cross8eadbf02017-10-24 17:46:00 -07001445func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001446 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001447
1448 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001449 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001450 // Compile java sources into turbine.jar.
1451 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1452 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1453 if ctx.Failed() {
1454 return nil
1455 }
1456 jars = append(jars, turbineJar)
1457 }
1458
Colin Cross55f63ea2018-08-27 12:37:09 -07001459 jars = append(jars, extraJars...)
1460
Nan Zhanged19fc32017-10-19 13:06:22 -07001461 // Combine any static header libraries into classes-header.jar. If there is only
1462 // one input jar this step will be skipped.
1463 var headerJar android.Path
1464 jars = append(jars, deps.staticHeaderJars...)
1465
Colin Cross5c6ecc12017-10-23 18:12:27 -07001466 // we cannot skip the combine step for now if there is only one jar
1467 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1468 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001469 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001470 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001471 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001472
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001473 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001474 // Transform classes.jar into classes-jarjar.jar
1475 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001476 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001477 headerJar = jarjarFile
1478 if ctx.Failed() {
1479 return nil
1480 }
1481 }
1482
1483 return headerJar
1484}
1485
Colin Crosscb933592017-11-22 13:49:43 -08001486func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001487 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001488
Colin Cross7a3139e2017-12-19 13:57:50 -08001489 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001490
Colin Cross84c38822018-01-03 15:59:46 -08001491 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001492 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1493
1494 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1495
1496 j.jacocoReportClassesFile = jacocoReportClassesFile
1497
1498 return instrumentedJar
1499}
1500
albaltai36ff7dc2018-12-25 14:35:23 +08001501var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001502
Nan Zhanged19fc32017-10-19 13:06:22 -07001503func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001504 if j.headerJarFile == nil {
1505 return nil
1506 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001507 return android.Paths{j.headerJarFile}
1508}
1509
1510func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001511 if j.implementationJarFile == nil {
1512 return nil
1513 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001514 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001515}
1516
Colin Crossf24a22a2019-01-31 14:12:44 -08001517func (j *Module) DexJar() android.Path {
1518 return j.dexJarFile
1519}
1520
Colin Cross331a1212018-08-15 20:40:52 -07001521func (j *Module) ResourceJars() android.Paths {
1522 if j.resourceJar == nil {
1523 return nil
1524 }
1525 return android.Paths{j.resourceJar}
1526}
1527
1528func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001529 if j.implementationAndResourcesJar == nil {
1530 return nil
1531 }
Colin Cross331a1212018-08-15 20:40:52 -07001532 return android.Paths{j.implementationAndResourcesJar}
1533}
1534
Colin Cross46c9b8b2017-06-22 16:51:17 -07001535func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001536 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001537 return j.exportAidlIncludeDirs
1538}
1539
Jiyong Park1be96912018-05-28 18:02:19 +09001540func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001541 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001542 return j.exportedSdkLibs
1543}
1544
Colin Cross0c4ce212019-05-03 15:28:19 -07001545func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1546 return j.srcJarArgs, j.srcJarDeps
1547}
1548
Colin Cross46c9b8b2017-06-22 16:51:17 -07001549var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001550
Colin Cross46c9b8b2017-06-22 16:51:17 -07001551func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001552 return j.logtagsSrcs
1553}
1554
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001555// Collect information for opening IDE project files in java/jdeps.go.
1556func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1557 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1558 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001559 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001560 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001561 if j.expandJarjarRules != nil {
1562 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001563 }
1564}
1565
1566func (j *Module) CompilerDeps() []string {
1567 jdeps := []string{}
1568 jdeps = append(jdeps, j.properties.Libs...)
1569 jdeps = append(jdeps, j.properties.Static_libs...)
1570 return jdeps
1571}
1572
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001573func (j *Module) hasCode(ctx android.ModuleContext) bool {
1574 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1575 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1576}
1577
Colin Cross2fe66872015-03-30 17:20:39 -07001578//
1579// Java libraries (.jar file)
1580//
1581
Colin Crossf506d872017-07-19 15:53:04 -07001582type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001583 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001584}
1585
Colin Cross42be7612019-02-21 18:12:14 -08001586func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001587 // Store uncompressed (and do not strip) dex files from boot class path jars.
1588 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1589 return true
1590 }
1591
1592 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001593 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001594 return true
1595 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001596 if ctx.Config().UncompressPrivAppDex() &&
1597 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1598 return true
1599 }
1600
Colin Cross2fc72f62018-12-21 12:59:54 -08001601 return false
1602}
1603
Colin Crossf506d872017-07-19 15:53:04 -07001604func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001605 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1606 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001607 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001608 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001609 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001610 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001611
Jiyong Park7f7766d2019-07-25 22:02:35 +09001612 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1613 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001614 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1615 ctx.ModuleName()+".jar", j.outputFile)
1616 }
Colin Crossb7a63242015-04-16 14:09:14 -07001617}
1618
Colin Crossf506d872017-07-19 15:53:04 -07001619func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001620 j.deps(ctx)
1621}
1622
Colin Cross1b16b0e2019-02-12 14:41:32 -08001623// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1624//
1625// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1626// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1627// as a `static_libs` dependency of another module.
1628//
1629// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1630// a device.
1631//
1632// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1633// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001634func LibraryFactory() android.Module {
1635 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001636
Colin Cross9ae1b922018-06-26 17:59:05 -07001637 module.AddProperties(
1638 &module.Module.properties,
1639 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001640 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001641 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001642
Colin Cross9ae1b922018-06-26 17:59:05 -07001643 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001644 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001645 android.InitSdkAwareModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001646 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001647}
1648
Colin Cross1b16b0e2019-02-12 14:41:32 -08001649// java_library_static is an obsolete alias for java_library.
1650func LibraryStaticFactory() android.Module {
1651 return LibraryFactory()
1652}
1653
1654// java_library_host builds and links sources into a `.jar` file for the host.
1655//
1656// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1657// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001658func LibraryHostFactory() android.Module {
1659 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001660
Colin Cross6af17aa2017-09-20 12:59:05 -07001661 module.AddProperties(
1662 &module.Module.properties,
1663 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001664
Colin Cross9ae1b922018-06-26 17:59:05 -07001665 module.Module.properties.Installable = proptools.BoolPtr(true)
1666
Colin Cross89536d42017-07-07 14:35:50 -07001667 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001668 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001669 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001670}
1671
1672//
Colin Crossb628ea52018-08-14 16:42:33 -07001673// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001674//
1675
1676type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001677 // list of compatibility suites (for example "cts", "vts") that the module should be
1678 // installed into.
1679 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001680
1681 // the name of the test configuration (for example "AndroidTest.xml") that should be
1682 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001683 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001684
Jack He33338892018-09-19 02:21:28 -07001685 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1686 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001687 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001688
Colin Crossd96ca352018-08-10 16:06:24 -07001689 // list of files or filegroup modules that provide data that should be installed alongside
1690 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001691 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001692}
1693
Paul Duffin42df1442019-03-20 12:45:53 +00001694type testHelperLibraryProperties struct {
1695 // list of compatibility suites (for example "cts", "vts") that the module should be
1696 // installed into.
1697 Test_suites []string `android:"arch_variant"`
1698}
1699
Colin Cross05638fc2018-04-09 18:40:24 -07001700type Test struct {
1701 Library
1702
1703 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001704
1705 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001706 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001707}
1708
Paul Duffin42df1442019-03-20 12:45:53 +00001709type TestHelperLibrary struct {
1710 Library
1711
1712 testHelperLibraryProperties testHelperLibraryProperties
1713}
1714
Colin Cross303e21f2018-08-07 16:49:25 -07001715func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001716 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, j.testProperties.Test_suites)
Colin Cross8a497952019-03-05 22:25:09 -08001717 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001718
1719 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001720}
1721
Paul Duffin42df1442019-03-20 12:45:53 +00001722func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1723 j.Library.GenerateAndroidBuildActions(ctx)
1724}
1725
Colin Cross1b16b0e2019-02-12 14:41:32 -08001726// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1727// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1728//
1729// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1730// compiled against the device bootclasspath.
1731//
1732// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1733// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001734func TestFactory() android.Module {
1735 module := &Test{}
1736
1737 module.AddProperties(
1738 &module.Module.properties,
1739 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001740 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001741 &module.Module.protoProperties,
1742 &module.testProperties)
1743
Colin Cross9ae1b922018-06-26 17:59:05 -07001744 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001745 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001746
Colin Cross05638fc2018-04-09 18:40:24 -07001747 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001748 return module
1749}
1750
Paul Duffin42df1442019-03-20 12:45:53 +00001751// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1752func TestHelperLibraryFactory() android.Module {
1753 module := &TestHelperLibrary{}
1754
1755 module.AddProperties(
1756 &module.Module.properties,
1757 &module.Module.deviceProperties,
1758 &module.Module.dexpreoptProperties,
1759 &module.Module.protoProperties,
1760 &module.testHelperLibraryProperties)
1761
Colin Cross9a4abed2019-04-24 13:19:28 -07001762 module.Module.properties.Installable = proptools.BoolPtr(true)
1763 module.Module.dexpreopter.isTest = true
1764
Paul Duffin42df1442019-03-20 12:45:53 +00001765 InitJavaModule(module, android.HostAndDeviceSupported)
1766 return module
1767}
1768
Colin Cross1b16b0e2019-02-12 14:41:32 -08001769// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1770// allow running the test with `atest` or a `TEST_MAPPING` file.
1771//
1772// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1773// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001774func TestHostFactory() android.Module {
1775 module := &Test{}
1776
1777 module.AddProperties(
1778 &module.Module.properties,
1779 &module.Module.protoProperties,
1780 &module.testProperties)
1781
Colin Cross9ae1b922018-06-26 17:59:05 -07001782 module.Module.properties.Installable = proptools.BoolPtr(true)
1783
Colin Cross05638fc2018-04-09 18:40:24 -07001784 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001785 return module
1786}
1787
1788//
Colin Cross2fe66872015-03-30 17:20:39 -07001789// Java Binaries (.jar file plus wrapper script)
1790//
1791
Colin Crossf506d872017-07-19 15:53:04 -07001792type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001793 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001794 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001795
1796 // Name of the class containing main to be inserted into the manifest as Main-Class.
1797 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001798}
1799
Colin Crossf506d872017-07-19 15:53:04 -07001800type Binary struct {
1801 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001802
Colin Crossf506d872017-07-19 15:53:04 -07001803 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001804
Colin Cross6b4a32d2017-12-05 13:42:45 -08001805 isWrapperVariant bool
1806
Colin Crossc3315992017-12-08 19:12:36 -08001807 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001808 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001809}
1810
Alex Light24237172017-10-26 09:46:21 -07001811func (j *Binary) HostToolPath() android.OptionalPath {
1812 return android.OptionalPathForPath(j.binaryFile)
1813}
1814
Colin Crossf506d872017-07-19 15:53:04 -07001815func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001816 if ctx.Arch().ArchType == android.Common {
1817 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001818 if j.binaryProperties.Main_class != nil {
1819 if j.properties.Manifest != nil {
1820 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1821 }
1822 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1823 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1824 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1825 }
1826
Colin Cross6b4a32d2017-12-05 13:42:45 -08001827 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001828 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001829 // Handle the binary wrapper
1830 j.isWrapperVariant = true
1831
Colin Cross366938f2017-12-11 16:29:02 -08001832 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001833 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001834 } else {
1835 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1836 }
1837
1838 // Depend on the installed jar so that the wrapper doesn't get executed by
1839 // another build rule before the jar has been installed.
1840 jarFile := ctx.PrimaryModule().(*Binary).installFile
1841
1842 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1843 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001844 }
Colin Cross2fe66872015-03-30 17:20:39 -07001845}
1846
Colin Crossf506d872017-07-19 15:53:04 -07001847func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001848 if ctx.Arch().ArchType == android.Common {
1849 j.deps(ctx)
1850 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001851}
1852
Colin Cross1b16b0e2019-02-12 14:41:32 -08001853// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1854// as well.
1855//
1856// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1857// compiled against the device bootclasspath.
1858//
1859// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1860// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001861func BinaryFactory() android.Module {
1862 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001863
Colin Cross36242852017-06-23 15:06:31 -07001864 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001865 &module.Module.properties,
1866 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001867 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001868 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001869 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001870
Colin Cross9ae1b922018-06-26 17:59:05 -07001871 module.Module.properties.Installable = proptools.BoolPtr(true)
1872
Colin Cross6b4a32d2017-12-05 13:42:45 -08001873 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1874 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001875 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001876}
1877
Colin Cross1b16b0e2019-02-12 14:41:32 -08001878// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1879//
1880// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1881// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001882func BinaryHostFactory() android.Module {
1883 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001884
Colin Cross36242852017-06-23 15:06:31 -07001885 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001886 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001887 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001888 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001889
Colin Cross9ae1b922018-06-26 17:59:05 -07001890 module.Module.properties.Installable = proptools.BoolPtr(true)
1891
Colin Cross6b4a32d2017-12-05 13:42:45 -08001892 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1893 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001894 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001895}
1896
1897//
1898// Java prebuilts
1899//
1900
Colin Cross74d73e22017-08-02 11:05:49 -07001901type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001902 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001903
Nan Zhangea568a42017-11-08 21:20:04 -08001904 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001905
1906 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001907
1908 // List of shared java libs that this module has dependencies to
1909 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001910
1911 // List of files to remove from the jar file(s)
1912 Exclude_files []string
1913
1914 // List of directories to remove from the jar file(s)
1915 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001916
1917 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001918 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001919}
1920
1921type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001922 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001923 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001924 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001925 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001926 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001927
Colin Cross74d73e22017-08-02 11:05:49 -07001928 properties ImportProperties
1929
Colin Cross0a6e0072017-08-30 14:24:55 -07001930 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001931 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001932}
1933
Colin Cross83bb3162018-06-25 15:48:06 -07001934func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001935 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001936}
1937
1938func (j *Import) minSdkVersion() string {
1939 return j.sdkVersion()
1940}
1941
Colin Cross74d73e22017-08-02 11:05:49 -07001942func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001943 return &j.prebuilt
1944}
1945
Colin Cross74d73e22017-08-02 11:05:49 -07001946func (j *Import) PrebuiltSrcs() []string {
1947 return j.properties.Jars
1948}
1949
1950func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001951 return j.prebuilt.Name(j.ModuleBase.Name())
1952}
1953
Colin Cross74d73e22017-08-02 11:05:49 -07001954func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001955 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001956}
1957
Colin Cross74d73e22017-08-02 11:05:49 -07001958func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001959 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001960
Nan Zhang4c819fb2018-08-27 18:31:46 -07001961 jarName := ctx.ModuleName() + ".jar"
1962 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001963 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1964 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001965 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001966 inputFile := outputFile
1967 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1968 TransformJetifier(ctx, outputFile, inputFile)
1969 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001970 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001971
1972 ctx.VisitDirectDeps(func(module android.Module) {
1973 otherName := ctx.OtherModuleName(module)
1974 tag := ctx.OtherModuleDependencyTag(module)
1975
1976 switch dep := module.(type) {
1977 case Dependency:
1978 switch tag {
1979 case libTag, staticLibTag:
1980 // sdk lib names from dependencies are re-exported
1981 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1982 }
1983 case SdkLibraryDependency:
1984 switch tag {
1985 case libTag:
1986 // names of sdk libs that are directly depended are exported
1987 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1988 }
1989 }
1990 })
1991
1992 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001993 if Bool(j.properties.Installable) {
1994 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1995 ctx.ModuleName()+".jar", outputFile)
1996 }
Colin Cross2fe66872015-03-30 17:20:39 -07001997}
1998
Colin Cross74d73e22017-08-02 11:05:49 -07001999var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002000
Nan Zhanged19fc32017-10-19 13:06:22 -07002001func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002002 if j.combinedClasspathFile == nil {
2003 return nil
2004 }
Colin Cross37f6d792018-07-12 12:28:41 -07002005 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002006}
2007
2008func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002009 if j.combinedClasspathFile == nil {
2010 return nil
2011 }
Colin Cross37f6d792018-07-12 12:28:41 -07002012 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002013}
2014
Colin Cross331a1212018-08-15 20:40:52 -07002015func (j *Import) ResourceJars() android.Paths {
2016 return nil
2017}
2018
2019func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002020 if j.combinedClasspathFile == nil {
2021 return nil
2022 }
Colin Cross331a1212018-08-15 20:40:52 -07002023 return android.Paths{j.combinedClasspathFile}
2024}
2025
Colin Crossf24a22a2019-01-31 14:12:44 -08002026func (j *Import) DexJar() android.Path {
2027 return nil
2028}
2029
Colin Cross74d73e22017-08-02 11:05:49 -07002030func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002031 return nil
2032}
2033
Jiyong Park1be96912018-05-28 18:02:19 +09002034func (j *Import) ExportedSdkLibs() []string {
2035 return j.exportedSdkLibs
2036}
2037
Colin Cross0c4ce212019-05-03 15:28:19 -07002038func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2039 return nil, nil
2040}
2041
albaltai36ff7dc2018-12-25 14:35:23 +08002042// Add compile time check for interface implementation
2043var _ android.IDEInfo = (*Import)(nil)
2044var _ android.IDECustomizedModuleName = (*Import)(nil)
2045
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002046// Collect information for opening IDE project files in java/jdeps.go.
2047const (
2048 removedPrefix = "prebuilt_"
2049)
2050
2051func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2052 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2053}
2054
2055func (j *Import) IDECustomizedModuleName() string {
2056 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2057 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2058 // solution to get the Import name.
2059 name := j.Name()
2060 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002061 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002062 }
2063 return name
2064}
2065
Colin Cross74d73e22017-08-02 11:05:49 -07002066var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002067
Colin Cross1b16b0e2019-02-12 14:41:32 -08002068// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2069//
2070// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2071// compiled against an Android classpath.
2072//
2073// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2074// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002075func ImportFactory() android.Module {
2076 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002077
Colin Cross74d73e22017-08-02 11:05:49 -07002078 module.AddProperties(&module.properties)
2079
2080 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002081 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002082 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002083 android.InitSdkAwareModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002084 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002085}
2086
Colin Cross1b16b0e2019-02-12 14:41:32 -08002087// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2088// module.
2089//
2090// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2091// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002092func ImportFactoryHost() android.Module {
2093 module := &Import{}
2094
2095 module.AddProperties(&module.properties)
2096
2097 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002098 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002099 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002100 return module
2101}
2102
Colin Cross42be7612019-02-21 18:12:14 -08002103// dex_import module
2104
2105type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002106 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002107}
2108
2109type DexImport struct {
2110 android.ModuleBase
2111 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002112 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002113 prebuilt android.Prebuilt
2114
2115 properties DexImportProperties
2116
2117 dexJarFile android.Path
2118 maybeStrippedDexJarFile android.Path
2119
2120 dexpreopter
2121}
2122
2123func (j *DexImport) Prebuilt() *android.Prebuilt {
2124 return &j.prebuilt
2125}
2126
2127func (j *DexImport) PrebuiltSrcs() []string {
2128 return j.properties.Jars
2129}
2130
2131func (j *DexImport) Name() string {
2132 return j.prebuilt.Name(j.ModuleBase.Name())
2133}
2134
Colin Cross42be7612019-02-21 18:12:14 -08002135func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2136 if len(j.properties.Jars) != 1 {
2137 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2138 }
2139
2140 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2141 j.dexpreopter.isInstallable = true
2142 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2143
2144 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2145 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2146
2147 if j.dexpreopter.uncompressedDex {
2148 rule := android.NewRuleBuilder()
2149
2150 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2151 rule.Temporary(temporary)
2152
2153 // use zip2zip to uncompress classes*.dex files
2154 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002155 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002156 FlagWithInput("-i ", inputJar).
2157 FlagWithOutput("-o ", temporary).
2158 FlagWithArg("-0 ", "'classes*.dex'")
2159
2160 // use zipalign to align uncompressed classes*.dex files
2161 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002162 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002163 Flag("-f").
2164 Text("4").
2165 Input(temporary).
2166 Output(dexOutputFile)
2167
2168 rule.DeleteTemporaryFiles()
2169
2170 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2171 } else {
2172 ctx.Build(pctx, android.BuildParams{
2173 Rule: android.Cp,
2174 Input: inputJar,
2175 Output: dexOutputFile,
2176 })
2177 }
2178
2179 j.dexJarFile = dexOutputFile
2180
2181 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2182
2183 j.maybeStrippedDexJarFile = dexOutputFile
2184
2185 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2186 ctx.ModuleName()+".jar", dexOutputFile)
2187}
2188
2189func (j *DexImport) DexJar() android.Path {
2190 return j.dexJarFile
2191}
2192
2193// dex_import imports a `.jar` file containing classes.dex files.
2194//
2195// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2196// to the device.
2197func DexImportFactory() android.Module {
2198 module := &DexImport{}
2199
2200 module.AddProperties(&module.properties)
2201
2202 android.InitPrebuiltModule(module, &module.properties.Jars)
2203 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002204 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002205 return module
2206}
2207
Colin Cross89536d42017-07-07 14:35:50 -07002208//
2209// Defaults
2210//
2211type Defaults struct {
2212 android.ModuleBase
2213 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002214 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002215}
2216
Colin Cross1b16b0e2019-02-12 14:41:32 -08002217// java_defaults provides a set of properties that can be inherited by other java or android modules.
2218//
2219// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2220// property in the defaults module that exists in the depending module will be prepended to the depending module's
2221// value for that property.
2222//
2223// Example:
2224//
2225// java_defaults {
2226// name: "example_defaults",
2227// srcs: ["common/**/*.java"],
2228// javacflags: ["-Xlint:all"],
2229// aaptflags: ["--auto-add-overlay"],
2230// }
2231//
2232// java_library {
2233// name: "example",
2234// defaults: ["example_defaults"],
2235// srcs: ["example/**/*.java"],
2236// }
2237//
2238// is functionally identical to:
2239//
2240// java_library {
2241// name: "example",
2242// srcs: [
2243// "common/**/*.java",
2244// "example/**/*.java",
2245// ],
2246// javacflags: ["-Xlint:all"],
2247// }
Colin Cross89536d42017-07-07 14:35:50 -07002248func defaultsFactory() android.Module {
2249 return DefaultsFactory()
2250}
2251
2252func DefaultsFactory(props ...interface{}) android.Module {
2253 module := &Defaults{}
2254
2255 module.AddProperties(props...)
2256 module.AddProperties(
2257 &CompilerProperties{},
2258 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002259 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002260 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002261 &aaptProperties{},
2262 &androidLibraryProperties{},
2263 &appProperties{},
2264 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002265 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002266 &ImportProperties{},
2267 &AARImportProperties{},
2268 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002269 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002270 )
2271
2272 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002273 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002274 return module
2275}
Nan Zhangea568a42017-11-08 21:20:04 -08002276
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002277func kytheExtractJavaFactory() android.Singleton {
2278 return &kytheExtractJavaSingleton{}
2279}
2280
2281type kytheExtractJavaSingleton struct {
2282}
2283
2284func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2285 var xrefTargets android.Paths
2286 ctx.VisitAllModules(func(module android.Module) {
2287 if javaModule, ok := module.(xref); ok {
2288 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2289 }
2290 })
2291 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2292 if len(xrefTargets) > 0 {
2293 ctx.Build(pctx, android.BuildParams{
2294 Rule: blueprint.Phony,
2295 Output: android.PathForPhony(ctx, "xref_java"),
2296 Inputs: xrefTargets,
2297 })
2298 }
2299}
2300
Nan Zhangea568a42017-11-08 21:20:04 -08002301var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002302var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002303var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002304var inList = android.InList