blob: ecafa1e22ce49af95b7307986624eebb5ad5e3b4 [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 Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Jeongik Cha538c0d02019-07-11 15:54:27 +090055func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
56 if sc, ok := ctx.Module().(sdkContext); ok {
57 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
58 if usePlatformAPI != (sc.sdkVersion() == "") {
59 if usePlatformAPI {
60 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
61 } else {
62 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
63 }
64 }
65
66 }
67}
68
Colin Cross2fe66872015-03-30 17:20:39 -070069// TODO:
70// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070071// Renderscript
72// Post-jar passes:
73// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070074// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070075// DroidDoc
76// Findbugs
77
Colin Cross89536d42017-07-07 14:35:50 -070078type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
80 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080081 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070082
83 // list of source files that should not be used to build the Java module.
84 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080085 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070086
87 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070088 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070089
Colin Cross86a63ff2017-09-27 17:33:10 -070090 // list of directories that should be excluded from java_resource_dirs
91 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070092
Colin Cross0f37af02017-09-27 17:42:05 -070093 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080094 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070095
Colin Crosscedd4762018-09-13 11:26:19 -070096 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080097 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070098
Colin Cross7d5136f2015-05-11 13:39:40 -070099 // list of module-specific flags that will be used for javac compiles
100 Javacflags []string `android:"arch_variant"`
101
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200102 // list of module-specific flags that will be used for kotlinc compiles
103 Kotlincflags []string `android:"arch_variant"`
104
Colin Cross7d5136f2015-05-11 13:39:40 -0700105 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700106 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700107
108 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700109 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700110
111 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700113
Colin Cross540eff82017-06-22 17:01:52 -0700114 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800115 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700116
117 // If not blank, set the java version passed to javac as -source and -target
118 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700119
Colin Cross9ae1b922018-06-26 17:59:05 -0700120 // If set to true, allow this module to be dexed and installed on devices. Has no
121 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700122 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700123
Colin Cross0f37af02017-09-27 17:42:05 -0700124 // If set to true, include sources used to compile the module in to the final jar
125 Include_srcs *bool
126
Vladimir Marko0975ee02019-04-02 10:29:55 +0100127 // If not empty, classes are restricted to the specified packages and their sub-packages.
128 // This restriction is checked after applying jarjar rules and including static libs.
129 Permitted_packages []string
130
Colin Crossbe9cdb82019-01-21 21:37:16 -0800131 // List of modules to use as annotation processors
132 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700133
Nan Zhang61eaedb2017-11-02 13:28:15 -0700134 // The number of Java source entries each Javac instance can process
135 Javac_shard_size *int64
136
Nan Zhang5f8cb422018-02-06 10:34:32 -0800137 // Add host jdk tools.jar to bootclasspath
138 Use_tools_jar *bool
139
Colin Cross1369cdb2017-09-29 17:58:17 -0700140 Openjdk9 struct {
141 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800142 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700143
144 // List of javac flags that should only be used when passing -source 1.9
145 Javacflags []string
146 }
Colin Crosscb933592017-11-22 13:49:43 -0800147
Colin Cross81440082018-08-15 20:21:55 -0700148 // When compiling language level 9+ .java code in packages that are part of
149 // a system module, patch_module names the module that your sources and
150 // dependencies should be patched into. The Android runtime currently
151 // doesn't implement the JEP 261 module system so this option is only
152 // supported at compile time. It should only be needed to compile tests in
153 // packages that exist in libcore and which are inconvenient to move
154 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100155 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700156
Colin Crosscb933592017-11-22 13:49:43 -0800157 Jacoco struct {
158 // List of classes to include for instrumentation with jacoco to collect coverage
159 // information at runtime when building with coverage enabled. If unset defaults to all
160 // classes.
161 // Supports '*' as the last character of an entry in the list as a wildcard match.
162 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
163 // it matches classes in the package that have the class name as a prefix.
164 Include_filter []string
165
166 // List of classes to exclude from instrumentation with jacoco to collect coverage
167 // information at runtime when building with coverage enabled. Overrides classes selected
168 // by the include_filter property.
169 // Supports '*' as the last character of an entry in the list as a wildcard match.
170 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
171 // it matches classes in the package that have the class name as a prefix.
172 Exclude_filter []string
173 }
174
Andreas Gampef3e5b552018-01-22 21:27:21 -0800175 Errorprone struct {
176 // List of javac flags that should only be used when running errorprone.
177 Javacflags []string
178 }
179
Colin Cross0f2ee152017-12-14 15:22:43 -0800180 Proto struct {
181 // List of extra options that will be passed to the proto generator.
182 Output_params []string
183 }
184
Colin Crosscb933592017-11-22 13:49:43 -0800185 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800186
187 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800188 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700189}
190
Colin Cross89536d42017-07-07 14:35:50 -0700191type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700192 // list of module-specific flags that will be used for dex compiles
193 Dxflags []string `android:"arch_variant"`
194
Jeongik Cha538c0d02019-07-11 15:54:27 +0900195 // if not blank, set to the version of the sdk to compile against.
196 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800197 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700198
Colin Cross83bb3162018-06-25 15:48:06 -0700199 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
200 // Defaults to sdk_version if not set.
201 Min_sdk_version *string
202
Dan Willemsen419290a2018-10-31 15:28:47 -0700203 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
204 // Defaults to sdk_version if not set.
205 Target_sdk_version *string
206
Jeongik Cha538c0d02019-07-11 15:54:27 +0900207 // It must be true only if sdk_version is empty.
208 // This field works in only android_app, otherwise nothing happens.
Colin Cross6af2e492018-05-22 11:12:33 -0700209 Platform_apis *bool
210
Colin Crossebe1a512017-11-14 13:12:14 -0800211 Aidl struct {
212 // Top level directories to pass to aidl tool
213 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700214
Colin Crossebe1a512017-11-14 13:12:14 -0800215 // Directories rooted at the Android.bp file to pass to aidl tool
216 Local_include_dirs []string
217
218 // directories that should be added as include directories for any aidl sources of modules
219 // that depend on this module, as well as to aidl for this module.
220 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100221
222 // whether to generate traces (for systrace) for this interface
223 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100224
225 // whether to generate Binder#GetTransaction name method.
226 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800227 }
Colin Cross92430102017-10-09 14:59:32 -0700228
229 // If true, export a copy of the module as a -hostdex module for host testing.
230 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700231
Colin Cross7f87f4f2019-04-24 13:41:45 -0700232 Target struct {
233 Hostdex struct {
234 // Additional required dependencies to add to -hostdex modules.
235 Required []string
236 }
237 }
238
David Brazdil17ef5632018-06-27 10:27:45 +0100239 // If set to true, compile dex regardless of installable. Defaults to false.
240 Compile_dex *bool
241
Colin Cross66dbc0b2017-12-28 12:23:20 -0800242 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700243 // If false, disable all optimization. Defaults to true for android_app and android_test
244 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800245 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700246 // True if the module containing this has it set by default.
247 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800248
249 // If true, optimize for size by removing unused code. Defaults to true for apps,
250 // false for libraries and tests.
251 Shrink *bool
252
253 // If true, optimize bytecode. Defaults to false.
254 Optimize *bool
255
256 // If true, obfuscate bytecode. Defaults to false.
257 Obfuscate *bool
258
259 // If true, do not use the flag files generated by aapt that automatically keep
260 // classes referenced by the app manifest. Defaults to false.
261 No_aapt_flags *bool
262
263 // Flags to pass to proguard.
264 Proguard_flags []string
265
266 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800267 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800268 }
269
Colin Cross1369cdb2017-09-29 17:58:17 -0700270 // When targeting 1.9, override the modules to use with --system
271 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700272
273 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800274 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700275}
276
Sasha Smundak2057f822019-04-16 17:16:58 -0700277func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
278 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
279}
280
Colin Cross46c9b8b2017-06-22 16:51:17 -0700281// Module contains the properties and members used by all java module types
282type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700283 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700284 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900285 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700286
Colin Cross89536d42017-07-07 14:35:50 -0700287 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700288 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700289 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700290
Colin Cross331a1212018-08-15 20:40:52 -0700291 // jar file containing header classes including static library dependencies, suitable for
292 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 headerJarFile android.Path
294
Colin Cross331a1212018-08-15 20:40:52 -0700295 // jar file containing implementation classes including static library dependencies but no
296 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700298
Colin Cross331a1212018-08-15 20:40:52 -0700299 // jar file containing only resources including from static library dependencies
300 resourceJar android.Path
301
Colin Cross0c4ce212019-05-03 15:28:19 -0700302 // args and dependencies to package source files into a srcjar
303 srcJarArgs []string
304 srcJarDeps android.Paths
305
Colin Cross331a1212018-08-15 20:40:52 -0700306 // jar file containing implementation classes and resources including static library
307 // dependencies
308 implementationAndResourcesJar android.Path
309
310 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700311 dexJarFile android.Path
312
Colin Cross43f08db2018-11-12 10:13:39 -0800313 // output file that contains classes.dex if it should be in the output file
314 maybeStrippedDexJarFile android.Path
315
Colin Crosscb933592017-11-22 13:49:43 -0800316 // output file containing uninstrumented classes that will be instrumented by jacoco
317 jacocoReportClassesFile android.Path
318
Colin Cross66dbc0b2017-12-28 12:23:20 -0800319 // output file containing mapping of obfuscated names
320 proguardDictionary android.Path
321
Colin Cross331a1212018-08-15 20:40:52 -0700322 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700323 outputFile android.Path
324 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700325
Colin Cross635c3b02016-05-18 15:37:25 -0700326 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700327
Colin Cross635c3b02016-05-18 15:37:25 -0700328 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700329
Colin Cross2fe66872015-03-30 17:20:39 -0700330 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700331 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800332
333 // list of .java files and srcjars that was passed to javac
334 compiledJavaSrcs android.Paths
335 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800336
337 // list of extra progurad flag files
338 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900339
Colin Cross094054a2018-10-17 15:10:48 -0700340 // manifest file to use instead of properties.Manifest
341 overrideManifest android.OptionalPath
342
Jiyong Park1be96912018-05-28 18:02:19 +0900343 // list of SDK lib names that this java moudule is exporting
344 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700345
346 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
347 // filter out Exclude_srcs, will be used by android.IDEInfo struct
348 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800349
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800350 // expanded Jarjar_rules
351 expandJarjarRules android.Path
352
Vladimir Marko0975ee02019-04-02 10:29:55 +0100353 // list of additional targets for checkbuild
354 additionalCheckedModules android.Paths
355
Colin Cross988708c2019-05-06 14:04:11 -0700356 // Extra files generated by the module type to be added as java resources.
357 extraResources android.Paths
358
Colin Crossf24a22a2019-01-31 14:12:44 -0800359 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800360 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700361}
362
Colin Cross41955e82019-05-29 14:40:35 -0700363func (j *Module) OutputFiles(tag string) (android.Paths, error) {
364 switch tag {
365 case "":
366 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700367 case ".jar":
368 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700369 default:
370 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
371 }
Colin Cross54250902017-12-05 09:28:08 -0800372}
373
Jiyong Park8fd61922018-11-08 02:50:25 +0900374func (j *Module) DexJarFile() android.Path {
375 return j.dexJarFile
376}
377
Colin Cross41955e82019-05-29 14:40:35 -0700378var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800379
Colin Crossf506d872017-07-19 15:53:04 -0700380type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700381 HeaderJars() android.Paths
382 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700383 ResourceJars() android.Paths
384 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800385 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700386 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900387 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700388 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700389}
390
Jiyong Parkc678ad32018-04-10 13:07:10 +0900391type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700392 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
393 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900394}
395
Nan Zhangb2b33de2018-02-23 11:18:47 -0800396type SrcDependency interface {
397 CompiledSrcs() android.Paths
398 CompiledSrcJars() android.Paths
399}
400
401func (j *Module) CompiledSrcs() android.Paths {
402 return j.compiledJavaSrcs
403}
404
405func (j *Module) CompiledSrcJars() android.Paths {
406 return j.compiledSrcJars
407}
408
409var _ SrcDependency = (*Module)(nil)
410
Colin Cross89536d42017-07-07 14:35:50 -0700411func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
412 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
413 android.InitDefaultableModule(module)
414}
415
Colin Crossbe1da472017-07-07 15:59:46 -0700416type dependencyTag struct {
417 blueprint.BaseDependencyTag
418 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700419}
420
Colin Crossa4f08812018-10-02 22:03:40 -0700421type jniDependencyTag struct {
422 blueprint.BaseDependencyTag
423 target android.Target
424}
425
Colin Crossbe1da472017-07-07 15:59:46 -0700426var (
Colin Cross4b964c02018-10-15 16:18:06 -0700427 staticLibTag = dependencyTag{name: "staticlib"}
428 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800429 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700430 bootClasspathTag = dependencyTag{name: "bootclasspath"}
431 systemModulesTag = dependencyTag{name: "system modules"}
432 frameworkResTag = dependencyTag{name: "framework-res"}
433 frameworkApkTag = dependencyTag{name: "framework-apk"}
434 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800435 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700436 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
437 certificateTag = dependencyTag{name: "certificate"}
438 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700439 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700440)
Colin Cross2fe66872015-03-30 17:20:39 -0700441
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900442func defaultSdkVersion(ctx checkVendorModuleContext) string {
443 if ctx.SocSpecific() || ctx.DeviceSpecific() {
444 return "system_current"
445 }
446 return ""
447}
448
449type checkVendorModuleContext interface {
450 SocSpecific() bool
451 DeviceSpecific() bool
452}
453
Colin Crossfc3674a2017-09-18 17:41:52 -0700454type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700455 useModule, useFiles, useDefaultLibs, invalidVersion bool
456
Colin Cross86a60ae2018-05-29 14:44:55 -0700457 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700458 systemModules string
459
Colin Crossa97c5d32018-03-28 14:58:31 -0700460 frameworkResModule string
461
Colin Cross86a60ae2018-05-29 14:44:55 -0700462 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700463 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100464
465 noStandardLibs, noFrameworksLibs bool
466}
467
468func (s sdkDep) hasStandardLibs() bool {
469 return !s.noStandardLibs
470}
471
472func (s sdkDep) hasFrameworkLibs() bool {
473 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700474}
475
Colin Crossa4f08812018-10-02 22:03:40 -0700476type jniLib struct {
477 name string
478 path android.Path
479 target android.Target
480}
481
Colin Cross0ea8ba82019-06-06 14:33:29 -0700482func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800483 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
484}
485
Colin Cross0ea8ba82019-06-06 14:33:29 -0700486func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800487 return j.shouldInstrument(ctx) &&
488 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
489 ctx.Config().UnbundledBuild())
490}
491
Colin Cross83bb3162018-06-25 15:48:06 -0700492func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900493 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700494}
495
496func (j *Module) minSdkVersion() string {
497 if j.deviceProperties.Min_sdk_version != nil {
498 return *j.deviceProperties.Min_sdk_version
499 }
500 return j.sdkVersion()
501}
502
Dan Willemsen419290a2018-10-31 15:28:47 -0700503func (j *Module) targetSdkVersion() string {
504 if j.deviceProperties.Target_sdk_version != nil {
505 return *j.deviceProperties.Target_sdk_version
506 }
507 return j.sdkVersion()
508}
509
Colin Crossbe1da472017-07-07 15:59:46 -0700510func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700511 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100512 sdkDep := decodeSdkDep(ctx, sdkContext(j))
513 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700514 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700515 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100516 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100517 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700518 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700519 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700520 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100521 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700522 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700523 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700524 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
525 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800526 }
Colin Crossbe1da472017-07-07 15:59:46 -0700527 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700528 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100529 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100530 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100531 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700532 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700533 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100534 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700535 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800536 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800537 if ctx.ModuleName() == "android_stubs_current" ||
538 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700539 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700540 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800541 }
Colin Cross2fe66872015-03-30 17:20:39 -0700542 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700543
Colin Cross42d48b72018-08-29 14:10:52 -0700544 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
545 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700546
Colin Crossbe9cdb82019-01-21 21:37:16 -0800547 ctx.AddFarVariationDependencies([]blueprint.Variation{
548 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
549 }, pluginTag, j.properties.Plugins...)
550
Colin Crossfe17f6f2019-03-28 19:30:56 -0700551 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700552 if j.hasSrcExt(".proto") {
553 protoDeps(ctx, &j.protoProperties)
554 }
Colin Cross93e85952017-08-15 13:34:18 -0700555
556 if j.hasSrcExt(".kt") {
557 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
558 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700559 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
560 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800561 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800562 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
563 }
Colin Cross93e85952017-08-15 13:34:18 -0700564 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800565
566 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700567 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800568 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700569}
570
571func hasSrcExt(srcs []string, ext string) bool {
572 for _, src := range srcs {
573 if filepath.Ext(src) == ext {
574 return true
575 }
576 }
577
578 return false
579}
580
Nan Zhang61eaedb2017-11-02 13:28:15 -0700581func shardPaths(paths android.Paths, shardSize int) []android.Paths {
582 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
583 for len(paths) > shardSize {
584 ret = append(ret, paths[0:shardSize])
585 paths = paths[shardSize:]
586 }
587 if len(paths) > 0 {
588 ret = append(ret, paths)
589 }
590 return ret
591}
592
Colin Cross6af17aa2017-09-20 12:59:05 -0700593func (j *Module) hasSrcExt(ext string) bool {
594 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700595}
596
Colin Cross46c9b8b2017-06-22 16:51:17 -0700597func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700598 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700599
Colin Crossebe1a512017-11-14 13:12:14 -0800600 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
601 aidlIncludes = append(aidlIncludes,
602 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
603 aidlIncludes = append(aidlIncludes,
604 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700605
Colin Cross3047fa22019-04-18 10:56:44 -0700606 var flags []string
607 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700608
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700609 if aidlPreprocess.Valid() {
610 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700611 deps = append(deps, aidlPreprocess.Path())
612 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700613 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700614 }
615
Colin Cross3047fa22019-04-18 10:56:44 -0700616 if len(j.exportAidlIncludeDirs) > 0 {
617 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
618 }
619
620 if len(aidlIncludes) > 0 {
621 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
622 }
623
Colin Cross635c3b02016-05-18 15:37:25 -0700624 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800625 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700626 flags = append(flags, "-I"+src.String())
627 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700628
Martijn Coeneneab15642018-03-09 09:29:59 +0100629 if Bool(j.deviceProperties.Aidl.Generate_traces) {
630 flags = append(flags, "-t")
631 }
632
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100633 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
634 flags = append(flags, "--transaction_names")
635 }
636
Colin Cross3047fa22019-04-18 10:56:44 -0700637 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700638}
639
Colin Cross32f676a2017-09-06 13:41:06 -0700640type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800641 classpath classpath
642 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700643 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800644 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700645 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700646 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700647 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700648 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800649 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700650 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700651 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700652 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700653 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800654 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800655
656 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700657}
Colin Cross2fe66872015-03-30 17:20:39 -0700658
Colin Cross54250902017-12-05 09:28:08 -0800659func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
660 for _, f := range dep.Srcs() {
661 if f.Ext() != ".jar" {
662 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
663 ctx.OtherModuleName(dep.(blueprint.Module)))
664 }
665 }
666}
667
Jiyong Park2d492942018-03-05 17:44:10 +0900668type linkType int
669
670const (
671 javaCore linkType = iota
672 javaSdk
673 javaSystem
674 javaPlatform
675)
676
Jiyong Park46f78fb2018-10-20 16:33:17 +0900677func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700678 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700679 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900680 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
681 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100682 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900683 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100684 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900685 return javaCore, false
686 case name == "android_system_stubs_current":
687 return javaSystem, true
688 case strings.HasPrefix(ver, "system_"):
689 return javaSystem, false
690 case name == "android_test_stubs_current":
691 return javaSystem, true
692 case strings.HasPrefix(ver, "test_"):
693 return javaPlatform, false
694 case name == "android_stubs_current":
695 return javaSdk, true
696 case ver == "current":
697 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100698 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900699 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700700 default:
701 if _, err := strconv.Atoi(ver); err != nil {
702 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
703 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900704 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900705 }
706}
707
Jiyong Park750e5572018-01-31 00:20:13 +0900708func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700709 if ctx.Host() {
710 return
711 }
712
Jiyong Park46f78fb2018-10-20 16:33:17 +0900713 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
714 if stubs {
715 return
716 }
717 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900718 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."
719
720 switch myLinkType {
721 case javaCore:
722 if otherLinkType != javaCore {
723 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 +0900724 ctx.OtherModuleName(to))
725 }
Jiyong Park2d492942018-03-05 17:44:10 +0900726 break
727 case javaSdk:
728 if otherLinkType != javaCore && otherLinkType != javaSdk {
729 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
730 ctx.OtherModuleName(to))
731 }
732 break
733 case javaSystem:
734 if otherLinkType == javaPlatform {
735 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
736 ctx.OtherModuleName(to))
737 }
738 break
739 case javaPlatform:
740 // no restriction on link-type
741 break
Jiyong Park750e5572018-01-31 00:20:13 +0900742 }
743}
744
Colin Cross32f676a2017-09-06 13:41:06 -0700745func (j *Module) collectDeps(ctx android.ModuleContext) deps {
746 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700747
Colin Cross300f0382018-03-06 13:11:51 -0800748 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700749 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800750 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700751 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800752 } else if sdkDep.useFiles {
753 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700754 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700755 deps.aidlPreprocess = sdkDep.aidl
756 } else {
757 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800758 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700759 }
760
Colin Crossd11fcda2017-10-23 17:59:01 -0700761 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700762 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700763 tag := ctx.OtherModuleDependencyTag(module)
764
Colin Crossa4f08812018-10-02 22:03:40 -0700765 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700766 // Handled by AndroidApp.collectAppDeps
767 return
768 }
769 if tag == certificateTag {
770 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700771 return
772 }
773
Jiyong Park750e5572018-01-31 00:20:13 +0900774 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700775 switch tag {
776 case bootClasspathTag, libTag, staticLibTag:
777 checkLinkType(ctx, j, to, tag.(dependencyTag))
778 }
Jiyong Park750e5572018-01-31 00:20:13 +0900779 }
Colin Cross54250902017-12-05 09:28:08 -0800780 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800781 case SdkLibraryDependency:
782 switch tag {
783 case libTag:
784 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
785 // names of sdk libs that are directly depended are exported
786 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700787 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800788 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
789 }
Colin Cross54250902017-12-05 09:28:08 -0800790 case Dependency:
791 switch tag {
792 case bootClasspathTag:
793 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700794 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800795 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900796 // sdk lib names from dependencies are re-exported
797 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700798 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800799 case staticLibTag:
800 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
801 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
802 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700803 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900804 // sdk lib names from dependencies are re-exported
805 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700806 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800807 case pluginTag:
808 if plugin, ok := dep.(*Plugin); ok {
809 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
810 if plugin.pluginProperties.Processor_class != nil {
811 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
812 }
813 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
814 } else {
815 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
816 }
Colin Cross54250902017-12-05 09:28:08 -0800817 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100818 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800819 // framework.jar has a one-off dependency on the R.java and Manifest.java files
820 // generated by framework-res.apk
821 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
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 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700854 case systemModulesTag:
855 if deps.systemModules != nil {
856 panic("Found two system module dependencies")
857 }
858 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000859 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700860 panic("Missing directory for system module dependency")
861 }
Colin Crossb77043e2019-07-16 13:57:13 -0700862 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700863 }
Colin Crossec7a0422017-07-07 14:47:12 -0700864 }
Colin Cross2fe66872015-03-30 17:20:39 -0700865 })
866
Jiyong Park1be96912018-05-28 18:02:19 +0900867 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
868
Colin Cross32f676a2017-09-06 13:41:06 -0700869 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700870}
871
Colin Cross83bb3162018-06-25 15:48:06 -0700872func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700873 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800874 v := sdkContext.sdkVersion()
875 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100876 if ctx.Config().IsPdkBuild() &&
877 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700878 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800879 latestSdkVersion := 0
880 if len(sdkVersions) > 0 {
881 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
882 }
883 v = strconv.Itoa(latestSdkVersion)
884 }
885
886 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700887 if err != nil {
888 ctx.PropertyErrorf("sdk_version", "%s", err)
889 }
Nan Zhang357466b2018-04-17 17:38:36 -0700890 if javaVersion != "" {
891 ret = javaVersion
892 } else if ctx.Device() && sdk <= 23 {
893 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100894 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700895 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100896 } else if ctx.Device() &&
897 sdkContext.sdkVersion() != "" &&
898 sdkContext.sdkVersion() != "none" &&
899 sdkContext.sdkVersion() != "core_platform" &&
900 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700901 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
902 ret = "1.8"
903 } else {
904 ret = "1.9"
905 }
906
907 return ret
908}
909
Nan Zhanged19fc32017-10-19 13:06:22 -0700910func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700911
Colin Crossf03c82b2015-04-13 13:53:40 -0700912 var flags javaBuilderFlags
913
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100914 // javaVersion flag.
915 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
916
Nan Zhanged19fc32017-10-19 13:06:22 -0700917 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700918 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100919 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700920 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700921 }
Colin Cross6510f912017-11-29 00:27:14 -0800922 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700923 // Override the -g flag passed globally to remove local variable debug info to reduce
924 // disk and memory usage.
925 javacFlags = append(javacFlags, "-g:source,lines")
926 }
Colin Cross64162712017-08-08 13:17:59 -0700927
Colin Cross66548102018-06-19 22:47:35 -0700928 if ctx.Config().RunErrorProne() {
929 if config.ErrorProneClasspath == nil {
930 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
931 }
932
933 errorProneFlags := []string{
934 "-Xplugin:ErrorProne",
935 "${config.ErrorProneChecks}",
936 }
937 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
938
939 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
940 "'" + strings.Join(errorProneFlags, " ") + "'"
941 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800942 }
943
Nan Zhanged19fc32017-10-19 13:06:22 -0700944 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800945 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
946 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700947 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800948
Colin Crossbe9cdb82019-01-21 21:37:16 -0800949 flags.processor = strings.Join(deps.processorClasses, ",")
950
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100951 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100952 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800953 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
954 // Give host-side tools a version of OpenJDK's standard libraries
955 // close to what they're targeting. As of Dec 2017, AOSP is only
956 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
957 //
958 // When building with OpenJDK 8, the following should have no
959 // effect since those jars would be available by default.
960 //
961 // When building with OpenJDK 9 but targeting a version < 1.8,
962 // putting them on the bootclasspath means that:
963 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
964 // b) references to existing APIs are not reinterpreted in an
965 // OpenJDK 9-specific way, eg. calls to subclasses of
966 // java.nio.Buffer as in http://b/70862583
967 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
968 flags.bootClasspath = append(flags.bootClasspath,
969 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
970 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800971 if Bool(j.properties.Use_tools_jar) {
972 flags.bootClasspath = append(flags.bootClasspath,
973 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
974 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800975 }
976
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100977 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800978 // Manually specify build directory in case it is not under the repo root.
979 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
980 // just adding a symlink under the root doesn't help.)
981 patchPaths := ".:" + ctx.Config().BuildDir()
982 classPath := flags.classpath.FormJavaClassPath("")
983 if classPath != "" {
984 patchPaths += ":" + classPath
985 }
986 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700987 }
988
Nan Zhanged19fc32017-10-19 13:06:22 -0700989 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -0700990 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700991
Nan Zhanged19fc32017-10-19 13:06:22 -0700992 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700993 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700994
Colin Cross81440082018-08-15 20:21:55 -0700995 if len(javacFlags) > 0 {
996 // optimization.
997 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
998 flags.javacFlags = "$javacFlags"
999 }
1000
Nan Zhanged19fc32017-10-19 13:06:22 -07001001 return flags
1002}
Colin Crossc0b06f12015-04-08 13:03:43 -07001003
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001004func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
1005
Colin Crossebe1a512017-11-14 13:12:14 -08001006 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001007
1008 deps := j.collectDeps(ctx)
1009 flags := j.collectBuilderFlags(ctx, deps)
1010
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001011 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001012 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1013 }
Colin Cross8a497952019-03-05 22:25:09 -08001014 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001015 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001016 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001017 }
1018
Colin Crossaf050172017-11-15 23:01:59 -08001019 srcFiles = j.genSources(ctx, srcFiles, flags)
1020
1021 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001022 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001023 if aaptSrcJar != nil {
1024 srcJars = append(srcJars, aaptSrcJar)
1025 }
Colin Crossb7a63242015-04-16 14:09:14 -07001026
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001027 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1028 // that IDEInfo struct will use
1029 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1030
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001031 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001032 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001033 }
1034
Colin Cross1ee23172017-10-18 14:44:18 -07001035 jarName := ctx.ModuleName() + ".jar"
1036
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001037 javaSrcFiles := srcFiles.FilterByExt(".java")
1038 var uniqueSrcFiles android.Paths
1039 set := make(map[string]bool)
1040 for _, v := range javaSrcFiles {
1041 if _, found := set[v.String()]; !found {
1042 set[v.String()] = true
1043 uniqueSrcFiles = append(uniqueSrcFiles, v)
1044 }
1045 }
1046
Colin Cross55f63ea2018-08-27 12:37:09 -07001047 var kotlinJars android.Paths
1048
Colin Cross93e85952017-08-15 13:34:18 -07001049 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001050 // user defined kotlin flags.
1051 kotlincFlags := j.properties.Kotlincflags
1052 CheckKotlincFlags(ctx, kotlincFlags)
1053
Colin Cross93e85952017-08-15 13:34:18 -07001054 // If there are kotlin files, compile them first but pass all the kotlin and java files
1055 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1056 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001057 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001058 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001059 kotlincFlags = append(kotlincFlags, "-no-jdk")
1060 }
1061 if len(kotlincFlags) > 0 {
1062 // optimization.
1063 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1064 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001065 }
1066
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001067 var kotlinSrcFiles android.Paths
1068 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1069 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1070
Colin Crossafbb1732019-01-17 15:42:52 -08001071 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1072 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1073
1074 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1075 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1076
1077 if len(flags.processorPath) > 0 {
1078 // Use kapt for annotation processing
1079 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1080 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1081 srcJars = append(srcJars, kaptSrcJar)
1082 // Disable annotation processing in javac, it's already been handled by kapt
1083 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001084 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001085 }
Colin Cross93e85952017-08-15 13:34:18 -07001086
Colin Cross1ee23172017-10-18 14:44:18 -07001087 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001088 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001089 if ctx.Failed() {
1090 return
1091 }
1092
1093 // Make javac rule depend on the kotlinc rule
1094 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001095
Colin Cross93e85952017-08-15 13:34:18 -07001096 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001097 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001098 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001099 }
1100
Colin Cross55f63ea2018-08-27 12:37:09 -07001101 jars := append(android.Paths(nil), kotlinJars...)
1102
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001103 // Store the list of .java files that was passed to javac
1104 j.compiledJavaSrcs = uniqueSrcFiles
1105 j.compiledSrcJars = srcJars
1106
Nan Zhang61eaedb2017-11-02 13:28:15 -07001107 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001108 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001109 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1110 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001111 // Formerly, there was a check here that prevented annotation processors
1112 // from being used when sharding was enabled, as some annotation processors
1113 // do not function correctly in sharded environments. It was removed to
1114 // allow for the use of annotation processors that do function correctly
1115 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001116 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001117 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001118 if ctx.Failed() {
1119 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001120 }
1121 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001122 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001123 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001124 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001125 // If error-prone is enabled, add an additional rule to compile the java files into
1126 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001127 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001128 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1129 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001130 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001131 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001132 extraJarDeps = append(extraJarDeps, errorprone)
1133 }
1134
Nan Zhang61eaedb2017-11-02 13:28:15 -07001135 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001136 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001137 shardSize := int(*(j.properties.Javac_shard_size))
1138 var shardSrcs []android.Paths
1139 if len(uniqueSrcFiles) > 0 {
1140 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1141 for idx, shardSrc := range shardSrcs {
1142 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1143 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1144 jars = append(jars, classes)
1145 }
1146 }
1147 if len(srcJars) > 0 {
1148 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1149 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1150 jars = append(jars, classes)
1151 }
1152 } else {
1153 classes := android.PathForModuleOut(ctx, "javac", jarName)
1154 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1155 jars = append(jars, classes)
1156 }
Colin Crossd6891432017-09-27 17:39:56 -07001157 if ctx.Failed() {
1158 return
1159 }
Colin Cross2fe66872015-03-30 17:20:39 -07001160 }
1161
Colin Cross0c4ce212019-05-03 15:28:19 -07001162 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1163
1164 var includeSrcJar android.WritablePath
1165 if Bool(j.properties.Include_srcs) {
1166 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1167 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1168 }
1169
Colin Crosscedd4762018-09-13 11:26:19 -07001170 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1171 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001172 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001173 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001174
1175 var resArgs []string
1176 var resDeps android.Paths
1177
1178 resArgs = append(resArgs, dirArgs...)
1179 resDeps = append(resDeps, dirDeps...)
1180
1181 resArgs = append(resArgs, fileArgs...)
1182 resDeps = append(resDeps, fileDeps...)
1183
Colin Cross988708c2019-05-06 14:04:11 -07001184 resArgs = append(resArgs, extraArgs...)
1185 resDeps = append(resDeps, extraDeps...)
1186
Colin Cross40a36712017-09-27 17:41:35 -07001187 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001188 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001189 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001190 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001191 if ctx.Failed() {
1192 return
1193 }
1194 }
1195
Colin Cross0c4ce212019-05-03 15:28:19 -07001196 var resourceJars android.Paths
1197 if j.resourceJar != nil {
1198 resourceJars = append(resourceJars, j.resourceJar)
1199 }
1200 if Bool(j.properties.Include_srcs) {
1201 resourceJars = append(resourceJars, includeSrcJar)
1202 }
1203 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001204
Colin Cross0c4ce212019-05-03 15:28:19 -07001205 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001206 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001207 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001208 false, nil, nil)
1209 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001210 } else if len(resourceJars) == 1 {
1211 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001212 }
1213
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001214 if len(deps.staticJars) > 0 {
1215 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001216 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001217
Colin Cross094054a2018-10-17 15:10:48 -07001218 manifest := j.overrideManifest
1219 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001220 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001221 }
Colin Cross635acc92017-09-12 22:50:46 -07001222
Colin Cross8a497952019-03-05 22:25:09 -08001223 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001224 if len(services) > 0 {
1225 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1226 var zipargs []string
1227 for _, file := range services {
1228 serviceFile := file.String()
1229 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1230 }
1231 ctx.Build(pctx, android.BuildParams{
1232 Rule: zip,
1233 Output: servicesJar,
1234 Implicits: services,
1235 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001236 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001237 },
1238 })
1239 jars = append(jars, servicesJar)
1240 }
1241
Colin Cross0a6e0072017-08-30 14:24:55 -07001242 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001243 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001244 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001245
1246 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001247 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1248 // Optimization: skip the combine step if there is nothing to do
1249 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1250 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1251 // any if len(jars) == 1.
1252 outputFile = moduleOutPath
1253 } else {
1254 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1255 ctx.Build(pctx, android.BuildParams{
1256 Rule: android.Cp,
1257 Input: jars[0],
1258 Output: combinedJar,
1259 })
1260 outputFile = combinedJar
1261 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001262 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001263 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001264 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001265 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001266 outputFile = combinedJar
1267 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001268
Colin Cross331a1212018-08-15 20:40:52 -07001269 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001270 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001271 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001272 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001273 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001274 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001275
1276 // jarjar resource jar if necessary
1277 if j.resourceJar != nil {
1278 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001279 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001280 j.resourceJar = resourceJarJarFile
1281 }
1282
Colin Cross0a6e0072017-08-30 14:24:55 -07001283 if ctx.Failed() {
1284 return
1285 }
1286 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001287
1288 // Check package restrictions if necessary.
1289 if len(j.properties.Permitted_packages) > 0 {
1290 // Check packages and copy to package-checked file.
1291 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1292 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1293 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1294
1295 if ctx.Failed() {
1296 return
1297 }
1298 }
1299
Nan Zhanged19fc32017-10-19 13:06:22 -07001300 j.implementationJarFile = outputFile
1301 if j.headerJarFile == nil {
1302 j.headerJarFile = j.implementationJarFile
1303 }
Colin Cross2fe66872015-03-30 17:20:39 -07001304
Colin Cross6510f912017-11-29 00:27:14 -08001305 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001306 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1307 j.properties.Instrument = true
1308 }
1309 }
1310
Colin Cross3144dfc2018-01-03 15:06:47 -08001311 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001312 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1313 }
1314
Colin Cross331a1212018-08-15 20:40:52 -07001315 // merge implementation jar with resources if necessary
1316 implementationAndResourcesJar := outputFile
1317 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001318 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001319 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001320 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001321 false, nil, nil)
1322 implementationAndResourcesJar = combinedJar
1323 }
1324
1325 j.implementationAndResourcesJar = implementationAndResourcesJar
1326
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001327 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001328 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001329 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001330 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001331 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001332 if ctx.Failed() {
1333 return
1334 }
Colin Cross331a1212018-08-15 20:40:52 -07001335
Jiyong Park09cb6292019-07-15 15:29:23 +09001336 // Hidden API CSV generation and dex encoding
1337 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1338 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001339
Colin Cross331a1212018-08-15 20:40:52 -07001340 // merge dex jar with resources if necessary
1341 if j.resourceJar != nil {
1342 jars := android.Paths{dexOutputFile, j.resourceJar}
1343 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1344 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1345 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001346 if j.deviceProperties.UncompressDex {
1347 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1348 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1349 dexOutputFile = combinedAlignedJar
1350 } else {
1351 dexOutputFile = combinedJar
1352 }
Colin Cross331a1212018-08-15 20:40:52 -07001353 }
1354
1355 j.dexJarFile = dexOutputFile
1356
Colin Cross8faf8fc2019-01-16 15:15:52 -08001357 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001358 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1359
1360 j.maybeStrippedDexJarFile = dexOutputFile
1361
Colin Cross3063b782018-08-15 11:19:12 -07001362 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001363
1364 if ctx.Failed() {
1365 return
1366 }
Colin Cross331a1212018-08-15 20:40:52 -07001367 } else {
1368 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001369 }
Colin Cross331a1212018-08-15 20:40:52 -07001370
Colin Crossb7a63242015-04-16 14:09:14 -07001371 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001372
1373 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1374 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001375}
1376
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001377// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1378// since some of these flags may be used internally.
1379func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1380 for _, flag := range flags {
1381 flag = strings.TrimSpace(flag)
1382
1383 if !strings.HasPrefix(flag, "-") {
1384 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1385 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1386 ctx.PropertyErrorf("kotlincflags",
1387 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1388 } else if inList(flag, config.KotlincIllegalFlags) {
1389 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1390 } else if flag == "-include-runtime" {
1391 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1392 } else {
1393 args := strings.Split(flag, " ")
1394 if args[0] == "-kotlin-home" {
1395 ctx.PropertyErrorf("kotlincflags",
1396 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1397 }
1398 }
1399 }
1400}
1401
Colin Cross8eadbf02017-10-24 17:46:00 -07001402func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001403 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001404
1405 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001406 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001407 // Compile java sources into turbine.jar.
1408 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1409 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1410 if ctx.Failed() {
1411 return nil
1412 }
1413 jars = append(jars, turbineJar)
1414 }
1415
Colin Cross55f63ea2018-08-27 12:37:09 -07001416 jars = append(jars, extraJars...)
1417
Nan Zhanged19fc32017-10-19 13:06:22 -07001418 // Combine any static header libraries into classes-header.jar. If there is only
1419 // one input jar this step will be skipped.
1420 var headerJar android.Path
1421 jars = append(jars, deps.staticHeaderJars...)
1422
Colin Cross5c6ecc12017-10-23 18:12:27 -07001423 // we cannot skip the combine step for now if there is only one jar
1424 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1425 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001426 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001427 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001428 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001429
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001430 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001431 // Transform classes.jar into classes-jarjar.jar
1432 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001433 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001434 headerJar = jarjarFile
1435 if ctx.Failed() {
1436 return nil
1437 }
1438 }
1439
1440 return headerJar
1441}
1442
Colin Crosscb933592017-11-22 13:49:43 -08001443func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001444 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001445
Colin Cross7a3139e2017-12-19 13:57:50 -08001446 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001447
Colin Cross84c38822018-01-03 15:59:46 -08001448 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001449 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1450
1451 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1452
1453 j.jacocoReportClassesFile = jacocoReportClassesFile
1454
1455 return instrumentedJar
1456}
1457
albaltai36ff7dc2018-12-25 14:35:23 +08001458var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001459
Nan Zhanged19fc32017-10-19 13:06:22 -07001460func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001461 if j.headerJarFile == nil {
1462 return nil
1463 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001464 return android.Paths{j.headerJarFile}
1465}
1466
1467func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001468 if j.implementationJarFile == nil {
1469 return nil
1470 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001471 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001472}
1473
Colin Crossf24a22a2019-01-31 14:12:44 -08001474func (j *Module) DexJar() android.Path {
1475 return j.dexJarFile
1476}
1477
Colin Cross331a1212018-08-15 20:40:52 -07001478func (j *Module) ResourceJars() android.Paths {
1479 if j.resourceJar == nil {
1480 return nil
1481 }
1482 return android.Paths{j.resourceJar}
1483}
1484
1485func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001486 if j.implementationAndResourcesJar == nil {
1487 return nil
1488 }
Colin Cross331a1212018-08-15 20:40:52 -07001489 return android.Paths{j.implementationAndResourcesJar}
1490}
1491
Colin Cross46c9b8b2017-06-22 16:51:17 -07001492func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001493 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001494 return j.exportAidlIncludeDirs
1495}
1496
Jiyong Park1be96912018-05-28 18:02:19 +09001497func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001498 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001499 return j.exportedSdkLibs
1500}
1501
Colin Cross0c4ce212019-05-03 15:28:19 -07001502func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1503 return j.srcJarArgs, j.srcJarDeps
1504}
1505
Colin Cross46c9b8b2017-06-22 16:51:17 -07001506var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001507
Colin Cross46c9b8b2017-06-22 16:51:17 -07001508func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001509 return j.logtagsSrcs
1510}
1511
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001512// Collect information for opening IDE project files in java/jdeps.go.
1513func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1514 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1515 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001516 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001517 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001518 if j.expandJarjarRules != nil {
1519 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001520 }
1521}
1522
1523func (j *Module) CompilerDeps() []string {
1524 jdeps := []string{}
1525 jdeps = append(jdeps, j.properties.Libs...)
1526 jdeps = append(jdeps, j.properties.Static_libs...)
1527 return jdeps
1528}
1529
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001530func (j *Module) hasCode(ctx android.ModuleContext) bool {
1531 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1532 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1533}
1534
Colin Cross2fe66872015-03-30 17:20:39 -07001535//
1536// Java libraries (.jar file)
1537//
1538
Colin Crossf506d872017-07-19 15:53:04 -07001539type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001540 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001541}
1542
Colin Cross42be7612019-02-21 18:12:14 -08001543func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001544 // Store uncompressed (and do not strip) dex files from boot class path jars.
1545 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1546 return true
1547 }
1548
1549 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001550 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001551 return true
1552 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001553 if ctx.Config().UncompressPrivAppDex() &&
1554 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1555 return true
1556 }
1557
Colin Cross2fc72f62018-12-21 12:59:54 -08001558 return false
1559}
1560
Colin Crossf506d872017-07-19 15:53:04 -07001561func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001562 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1563 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001564 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001565 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001566 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001567 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001568
Jiyong Park7f7766d2019-07-25 22:02:35 +09001569 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1570 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001571 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1572 ctx.ModuleName()+".jar", j.outputFile)
1573 }
Colin Crossb7a63242015-04-16 14:09:14 -07001574}
1575
Colin Crossf506d872017-07-19 15:53:04 -07001576func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001577 j.deps(ctx)
1578}
1579
Colin Cross1b16b0e2019-02-12 14:41:32 -08001580// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1581//
1582// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1583// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1584// as a `static_libs` dependency of another module.
1585//
1586// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1587// a device.
1588//
1589// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1590// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001591func LibraryFactory() android.Module {
1592 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001593
Colin Cross9ae1b922018-06-26 17:59:05 -07001594 module.AddProperties(
1595 &module.Module.properties,
1596 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001597 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001598 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001599
Colin Cross9ae1b922018-06-26 17:59:05 -07001600 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001601 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001602 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001603}
1604
Colin Cross1b16b0e2019-02-12 14:41:32 -08001605// java_library_static is an obsolete alias for java_library.
1606func LibraryStaticFactory() android.Module {
1607 return LibraryFactory()
1608}
1609
1610// java_library_host builds and links sources into a `.jar` file for the host.
1611//
1612// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1613// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001614func LibraryHostFactory() android.Module {
1615 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001616
Colin Cross6af17aa2017-09-20 12:59:05 -07001617 module.AddProperties(
1618 &module.Module.properties,
1619 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001620
Colin Cross9ae1b922018-06-26 17:59:05 -07001621 module.Module.properties.Installable = proptools.BoolPtr(true)
1622
Colin Cross89536d42017-07-07 14:35:50 -07001623 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001624 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001625 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001626}
1627
1628//
Colin Crossb628ea52018-08-14 16:42:33 -07001629// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001630//
1631
1632type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001633 // list of compatibility suites (for example "cts", "vts") that the module should be
1634 // installed into.
1635 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001636
1637 // the name of the test configuration (for example "AndroidTest.xml") that should be
1638 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001639 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001640
Jack He33338892018-09-19 02:21:28 -07001641 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1642 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001643 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001644
Colin Crossd96ca352018-08-10 16:06:24 -07001645 // list of files or filegroup modules that provide data that should be installed alongside
1646 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001647 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001648}
1649
Paul Duffin42df1442019-03-20 12:45:53 +00001650type testHelperLibraryProperties struct {
1651 // list of compatibility suites (for example "cts", "vts") that the module should be
1652 // installed into.
1653 Test_suites []string `android:"arch_variant"`
1654}
1655
Colin Cross05638fc2018-04-09 18:40:24 -07001656type Test struct {
1657 Library
1658
1659 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001660
1661 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001662 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001663}
1664
Paul Duffin42df1442019-03-20 12:45:53 +00001665type TestHelperLibrary struct {
1666 Library
1667
1668 testHelperLibraryProperties testHelperLibraryProperties
1669}
1670
Colin Cross303e21f2018-08-07 16:49:25 -07001671func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001672 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 -08001673 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001674
1675 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001676}
1677
Paul Duffin42df1442019-03-20 12:45:53 +00001678func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1679 j.Library.GenerateAndroidBuildActions(ctx)
1680}
1681
Colin Cross1b16b0e2019-02-12 14:41:32 -08001682// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1683// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1684//
1685// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1686// compiled against the device bootclasspath.
1687//
1688// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1689// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001690func TestFactory() android.Module {
1691 module := &Test{}
1692
1693 module.AddProperties(
1694 &module.Module.properties,
1695 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001696 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001697 &module.Module.protoProperties,
1698 &module.testProperties)
1699
Colin Cross9ae1b922018-06-26 17:59:05 -07001700 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001701 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001702
Colin Cross05638fc2018-04-09 18:40:24 -07001703 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001704 return module
1705}
1706
Paul Duffin42df1442019-03-20 12:45:53 +00001707// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1708func TestHelperLibraryFactory() android.Module {
1709 module := &TestHelperLibrary{}
1710
1711 module.AddProperties(
1712 &module.Module.properties,
1713 &module.Module.deviceProperties,
1714 &module.Module.dexpreoptProperties,
1715 &module.Module.protoProperties,
1716 &module.testHelperLibraryProperties)
1717
Colin Cross9a4abed2019-04-24 13:19:28 -07001718 module.Module.properties.Installable = proptools.BoolPtr(true)
1719 module.Module.dexpreopter.isTest = true
1720
Paul Duffin42df1442019-03-20 12:45:53 +00001721 InitJavaModule(module, android.HostAndDeviceSupported)
1722 return module
1723}
1724
Colin Cross1b16b0e2019-02-12 14:41:32 -08001725// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1726// allow running the test with `atest` or a `TEST_MAPPING` file.
1727//
1728// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1729// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001730func TestHostFactory() android.Module {
1731 module := &Test{}
1732
1733 module.AddProperties(
1734 &module.Module.properties,
1735 &module.Module.protoProperties,
1736 &module.testProperties)
1737
Colin Cross9ae1b922018-06-26 17:59:05 -07001738 module.Module.properties.Installable = proptools.BoolPtr(true)
1739
Colin Cross05638fc2018-04-09 18:40:24 -07001740 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001741 return module
1742}
1743
1744//
Colin Cross2fe66872015-03-30 17:20:39 -07001745// Java Binaries (.jar file plus wrapper script)
1746//
1747
Colin Crossf506d872017-07-19 15:53:04 -07001748type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001749 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001750 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001751
1752 // Name of the class containing main to be inserted into the manifest as Main-Class.
1753 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001754}
1755
Colin Crossf506d872017-07-19 15:53:04 -07001756type Binary struct {
1757 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001758
Colin Crossf506d872017-07-19 15:53:04 -07001759 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001760
Colin Cross6b4a32d2017-12-05 13:42:45 -08001761 isWrapperVariant bool
1762
Colin Crossc3315992017-12-08 19:12:36 -08001763 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001764 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001765}
1766
Alex Light24237172017-10-26 09:46:21 -07001767func (j *Binary) HostToolPath() android.OptionalPath {
1768 return android.OptionalPathForPath(j.binaryFile)
1769}
1770
Colin Crossf506d872017-07-19 15:53:04 -07001771func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001772 if ctx.Arch().ArchType == android.Common {
1773 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001774 if j.binaryProperties.Main_class != nil {
1775 if j.properties.Manifest != nil {
1776 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1777 }
1778 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1779 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1780 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1781 }
1782
Colin Cross6b4a32d2017-12-05 13:42:45 -08001783 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001784 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001785 // Handle the binary wrapper
1786 j.isWrapperVariant = true
1787
Colin Cross366938f2017-12-11 16:29:02 -08001788 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001789 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001790 } else {
1791 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1792 }
1793
1794 // Depend on the installed jar so that the wrapper doesn't get executed by
1795 // another build rule before the jar has been installed.
1796 jarFile := ctx.PrimaryModule().(*Binary).installFile
1797
1798 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1799 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001800 }
Colin Cross2fe66872015-03-30 17:20:39 -07001801}
1802
Colin Crossf506d872017-07-19 15:53:04 -07001803func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001804 if ctx.Arch().ArchType == android.Common {
1805 j.deps(ctx)
1806 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001807}
1808
Colin Cross1b16b0e2019-02-12 14:41:32 -08001809// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1810// as well.
1811//
1812// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1813// compiled against the device bootclasspath.
1814//
1815// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1816// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001817func BinaryFactory() android.Module {
1818 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001819
Colin Cross36242852017-06-23 15:06:31 -07001820 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001821 &module.Module.properties,
1822 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001823 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001824 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001825 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001826
Colin Cross9ae1b922018-06-26 17:59:05 -07001827 module.Module.properties.Installable = proptools.BoolPtr(true)
1828
Colin Cross6b4a32d2017-12-05 13:42:45 -08001829 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1830 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001831 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001832}
1833
Colin Cross1b16b0e2019-02-12 14:41:32 -08001834// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1835//
1836// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1837// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001838func BinaryHostFactory() android.Module {
1839 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001840
Colin Cross36242852017-06-23 15:06:31 -07001841 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001842 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001843 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001844 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001845
Colin Cross9ae1b922018-06-26 17:59:05 -07001846 module.Module.properties.Installable = proptools.BoolPtr(true)
1847
Colin Cross6b4a32d2017-12-05 13:42:45 -08001848 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1849 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001850 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001851}
1852
1853//
1854// Java prebuilts
1855//
1856
Colin Cross74d73e22017-08-02 11:05:49 -07001857type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001858 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001859
Nan Zhangea568a42017-11-08 21:20:04 -08001860 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001861
1862 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001863
1864 // List of shared java libs that this module has dependencies to
1865 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001866
1867 // List of files to remove from the jar file(s)
1868 Exclude_files []string
1869
1870 // List of directories to remove from the jar file(s)
1871 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001872
1873 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001874 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001875}
1876
1877type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001878 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001879 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001880 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001881 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001882
Colin Cross74d73e22017-08-02 11:05:49 -07001883 properties ImportProperties
1884
Colin Cross0a6e0072017-08-30 14:24:55 -07001885 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001886 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001887}
1888
Colin Cross83bb3162018-06-25 15:48:06 -07001889func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001890 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001891}
1892
1893func (j *Import) minSdkVersion() string {
1894 return j.sdkVersion()
1895}
1896
Colin Cross74d73e22017-08-02 11:05:49 -07001897func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001898 return &j.prebuilt
1899}
1900
Colin Cross74d73e22017-08-02 11:05:49 -07001901func (j *Import) PrebuiltSrcs() []string {
1902 return j.properties.Jars
1903}
1904
1905func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001906 return j.prebuilt.Name(j.ModuleBase.Name())
1907}
1908
Colin Cross74d73e22017-08-02 11:05:49 -07001909func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001910 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001911}
1912
Colin Cross74d73e22017-08-02 11:05:49 -07001913func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001914 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001915
Nan Zhang4c819fb2018-08-27 18:31:46 -07001916 jarName := ctx.ModuleName() + ".jar"
1917 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001918 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1919 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001920 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001921 inputFile := outputFile
1922 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1923 TransformJetifier(ctx, outputFile, inputFile)
1924 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001925 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001926
1927 ctx.VisitDirectDeps(func(module android.Module) {
1928 otherName := ctx.OtherModuleName(module)
1929 tag := ctx.OtherModuleDependencyTag(module)
1930
1931 switch dep := module.(type) {
1932 case Dependency:
1933 switch tag {
1934 case libTag, staticLibTag:
1935 // sdk lib names from dependencies are re-exported
1936 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1937 }
1938 case SdkLibraryDependency:
1939 switch tag {
1940 case libTag:
1941 // names of sdk libs that are directly depended are exported
1942 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1943 }
1944 }
1945 })
1946
1947 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001948 if Bool(j.properties.Installable) {
1949 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1950 ctx.ModuleName()+".jar", outputFile)
1951 }
Colin Cross2fe66872015-03-30 17:20:39 -07001952}
1953
Colin Cross74d73e22017-08-02 11:05:49 -07001954var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001955
Nan Zhanged19fc32017-10-19 13:06:22 -07001956func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001957 if j.combinedClasspathFile == nil {
1958 return nil
1959 }
Colin Cross37f6d792018-07-12 12:28:41 -07001960 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001961}
1962
1963func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001964 if j.combinedClasspathFile == nil {
1965 return nil
1966 }
Colin Cross37f6d792018-07-12 12:28:41 -07001967 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001968}
1969
Colin Cross331a1212018-08-15 20:40:52 -07001970func (j *Import) ResourceJars() android.Paths {
1971 return nil
1972}
1973
1974func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001975 if j.combinedClasspathFile == nil {
1976 return nil
1977 }
Colin Cross331a1212018-08-15 20:40:52 -07001978 return android.Paths{j.combinedClasspathFile}
1979}
1980
Colin Crossf24a22a2019-01-31 14:12:44 -08001981func (j *Import) DexJar() android.Path {
1982 return nil
1983}
1984
Colin Cross74d73e22017-08-02 11:05:49 -07001985func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001986 return nil
1987}
1988
Jiyong Park1be96912018-05-28 18:02:19 +09001989func (j *Import) ExportedSdkLibs() []string {
1990 return j.exportedSdkLibs
1991}
1992
Colin Cross0c4ce212019-05-03 15:28:19 -07001993func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1994 return nil, nil
1995}
1996
albaltai36ff7dc2018-12-25 14:35:23 +08001997// Add compile time check for interface implementation
1998var _ android.IDEInfo = (*Import)(nil)
1999var _ android.IDECustomizedModuleName = (*Import)(nil)
2000
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002001// Collect information for opening IDE project files in java/jdeps.go.
2002const (
2003 removedPrefix = "prebuilt_"
2004)
2005
2006func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2007 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2008}
2009
2010func (j *Import) IDECustomizedModuleName() string {
2011 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2012 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2013 // solution to get the Import name.
2014 name := j.Name()
2015 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002016 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002017 }
2018 return name
2019}
2020
Colin Cross74d73e22017-08-02 11:05:49 -07002021var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002022
Colin Cross1b16b0e2019-02-12 14:41:32 -08002023// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2024//
2025// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2026// compiled against an Android classpath.
2027//
2028// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2029// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002030func ImportFactory() android.Module {
2031 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002032
Colin Cross74d73e22017-08-02 11:05:49 -07002033 module.AddProperties(&module.properties)
2034
2035 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002036 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002037 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002038 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002039}
2040
Colin Cross1b16b0e2019-02-12 14:41:32 -08002041// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2042// module.
2043//
2044// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2045// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002046func ImportFactoryHost() android.Module {
2047 module := &Import{}
2048
2049 module.AddProperties(&module.properties)
2050
2051 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002052 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002053 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002054 return module
2055}
2056
Colin Cross42be7612019-02-21 18:12:14 -08002057// dex_import module
2058
2059type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002060 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002061}
2062
2063type DexImport struct {
2064 android.ModuleBase
2065 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002066 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002067 prebuilt android.Prebuilt
2068
2069 properties DexImportProperties
2070
2071 dexJarFile android.Path
2072 maybeStrippedDexJarFile android.Path
2073
2074 dexpreopter
2075}
2076
2077func (j *DexImport) Prebuilt() *android.Prebuilt {
2078 return &j.prebuilt
2079}
2080
2081func (j *DexImport) PrebuiltSrcs() []string {
2082 return j.properties.Jars
2083}
2084
2085func (j *DexImport) Name() string {
2086 return j.prebuilt.Name(j.ModuleBase.Name())
2087}
2088
Colin Cross42be7612019-02-21 18:12:14 -08002089func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2090 if len(j.properties.Jars) != 1 {
2091 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2092 }
2093
2094 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2095 j.dexpreopter.isInstallable = true
2096 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2097
2098 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2099 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2100
2101 if j.dexpreopter.uncompressedDex {
2102 rule := android.NewRuleBuilder()
2103
2104 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2105 rule.Temporary(temporary)
2106
2107 // use zip2zip to uncompress classes*.dex files
2108 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002109 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002110 FlagWithInput("-i ", inputJar).
2111 FlagWithOutput("-o ", temporary).
2112 FlagWithArg("-0 ", "'classes*.dex'")
2113
2114 // use zipalign to align uncompressed classes*.dex files
2115 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002116 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002117 Flag("-f").
2118 Text("4").
2119 Input(temporary).
2120 Output(dexOutputFile)
2121
2122 rule.DeleteTemporaryFiles()
2123
2124 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2125 } else {
2126 ctx.Build(pctx, android.BuildParams{
2127 Rule: android.Cp,
2128 Input: inputJar,
2129 Output: dexOutputFile,
2130 })
2131 }
2132
2133 j.dexJarFile = dexOutputFile
2134
2135 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2136
2137 j.maybeStrippedDexJarFile = dexOutputFile
2138
2139 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2140 ctx.ModuleName()+".jar", dexOutputFile)
2141}
2142
2143func (j *DexImport) DexJar() android.Path {
2144 return j.dexJarFile
2145}
2146
2147// dex_import imports a `.jar` file containing classes.dex files.
2148//
2149// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2150// to the device.
2151func DexImportFactory() android.Module {
2152 module := &DexImport{}
2153
2154 module.AddProperties(&module.properties)
2155
2156 android.InitPrebuiltModule(module, &module.properties.Jars)
2157 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002158 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002159 return module
2160}
2161
Colin Cross89536d42017-07-07 14:35:50 -07002162//
2163// Defaults
2164//
2165type Defaults struct {
2166 android.ModuleBase
2167 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002168 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002169}
2170
Colin Cross1b16b0e2019-02-12 14:41:32 -08002171// java_defaults provides a set of properties that can be inherited by other java or android modules.
2172//
2173// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2174// property in the defaults module that exists in the depending module will be prepended to the depending module's
2175// value for that property.
2176//
2177// Example:
2178//
2179// java_defaults {
2180// name: "example_defaults",
2181// srcs: ["common/**/*.java"],
2182// javacflags: ["-Xlint:all"],
2183// aaptflags: ["--auto-add-overlay"],
2184// }
2185//
2186// java_library {
2187// name: "example",
2188// defaults: ["example_defaults"],
2189// srcs: ["example/**/*.java"],
2190// }
2191//
2192// is functionally identical to:
2193//
2194// java_library {
2195// name: "example",
2196// srcs: [
2197// "common/**/*.java",
2198// "example/**/*.java",
2199// ],
2200// javacflags: ["-Xlint:all"],
2201// }
Colin Cross89536d42017-07-07 14:35:50 -07002202func defaultsFactory() android.Module {
2203 return DefaultsFactory()
2204}
2205
2206func DefaultsFactory(props ...interface{}) android.Module {
2207 module := &Defaults{}
2208
2209 module.AddProperties(props...)
2210 module.AddProperties(
2211 &CompilerProperties{},
2212 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002213 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002214 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002215 &aaptProperties{},
2216 &androidLibraryProperties{},
2217 &appProperties{},
2218 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002219 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002220 &ImportProperties{},
2221 &AARImportProperties{},
2222 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002223 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002224 )
2225
2226 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002227 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002228 return module
2229}
Nan Zhangea568a42017-11-08 21:20:04 -08002230
2231var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002232var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002233var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002234var inList = android.InList