blob: a76cde54f2e6dff9d50c5a957b9f708624b6a20b [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)
39 android.RegisterModuleType("java_library_static", LibraryFactory)
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)
44 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070045 android.RegisterModuleType("java_import", ImportFactory)
46 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070047
Colin Cross798bfce2016-10-12 14:28:16 -070048 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070049}
50
Colin Cross2fe66872015-03-30 17:20:39 -070051// TODO:
52// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070053// Renderscript
54// Post-jar passes:
55// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070056// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070057// DroidDoc
58// Findbugs
59
Colin Cross89536d42017-07-07 14:35:50 -070060type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070061 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
62 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070063 Srcs []string `android:"arch_variant"`
64
65 // list of source files that should not be used to build the Java module.
66 // This is most useful in the arch/multilib variants to remove non-common files
67 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
69 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070070 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
Colin Cross86a63ff2017-09-27 17:33:10 -070072 // list of directories that should be excluded from java_resource_dirs
73 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070074
Colin Cross0f37af02017-09-27 17:42:05 -070075 // list of files to use as Java resources
76 Java_resources []string `android:"arch_variant"`
77
Colin Crosscedd4762018-09-13 11:26:19 -070078 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross0f37af02017-09-27 17:42:05 -070079 Exclude_java_resources []string `android:"arch_variant"`
80
Colin Crossfa5eb232017-10-01 20:33:03 -070081 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070082 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070083 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070084
Colin Crossfa5eb232017-10-01 20:33:03 -070085 // don't build against the framework libraries (legacy-test, core-junit,
86 // ext, and framework for device targets)
87 No_framework_libs *bool
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of module-specific flags that will be used for javac compiles
90 Javacflags []string `android:"arch_variant"`
91
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020092 // list of module-specific flags that will be used for kotlinc compiles
93 Kotlincflags []string `android:"arch_variant"`
94
Colin Cross7d5136f2015-05-11 13:39:40 -070095 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross540eff82017-06-22 17:01:52 -0700104 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700105 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700106
107 // If not blank, set the java version passed to javac as -source and -target
108 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700109
Colin Cross9ae1b922018-06-26 17:59:05 -0700110 // If set to true, allow this module to be dexed and installed on devices. Has no
111 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700112 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700113
Colin Cross0f37af02017-09-27 17:42:05 -0700114 // If set to true, include sources used to compile the module in to the final jar
115 Include_srcs *bool
116
Colin Crossbe9cdb82019-01-21 21:37:16 -0800117 // List of modules to use as annotation processors. Deprecated, use plugins instead.
Colin Cross32f676a2017-09-06 13:41:06 -0700118 Annotation_processors []string
119
Colin Crossbe9cdb82019-01-21 21:37:16 -0800120 // List of modules to use as annotation processors
121 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700122
Nan Zhang61eaedb2017-11-02 13:28:15 -0700123 // The number of Java source entries each Javac instance can process
124 Javac_shard_size *int64
125
Nan Zhang5f8cb422018-02-06 10:34:32 -0800126 // Add host jdk tools.jar to bootclasspath
127 Use_tools_jar *bool
128
Colin Cross1369cdb2017-09-29 17:58:17 -0700129 Openjdk9 struct {
130 // List of source files that should only be used when passing -source 1.9
131 Srcs []string
132
133 // List of javac flags that should only be used when passing -source 1.9
134 Javacflags []string
135 }
Colin Crosscb933592017-11-22 13:49:43 -0800136
Colin Cross81440082018-08-15 20:21:55 -0700137 // When compiling language level 9+ .java code in packages that are part of
138 // a system module, patch_module names the module that your sources and
139 // dependencies should be patched into. The Android runtime currently
140 // doesn't implement the JEP 261 module system so this option is only
141 // supported at compile time. It should only be needed to compile tests in
142 // packages that exist in libcore and which are inconvenient to move
143 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100144 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700145
Colin Crosscb933592017-11-22 13:49:43 -0800146 Jacoco struct {
147 // List of classes to include for instrumentation with jacoco to collect coverage
148 // information at runtime when building with coverage enabled. If unset defaults to all
149 // classes.
150 // Supports '*' as the last character of an entry in the list as a wildcard match.
151 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
152 // it matches classes in the package that have the class name as a prefix.
153 Include_filter []string
154
155 // List of classes to exclude from instrumentation with jacoco to collect coverage
156 // information at runtime when building with coverage enabled. Overrides classes selected
157 // by the include_filter property.
158 // Supports '*' as the last character of an entry in the list as a wildcard match.
159 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
160 // it matches classes in the package that have the class name as a prefix.
161 Exclude_filter []string
162 }
163
Andreas Gampef3e5b552018-01-22 21:27:21 -0800164 Errorprone struct {
165 // List of javac flags that should only be used when running errorprone.
166 Javacflags []string
167 }
168
Colin Cross0f2ee152017-12-14 15:22:43 -0800169 Proto struct {
170 // List of extra options that will be passed to the proto generator.
171 Output_params []string
172 }
173
Colin Crosscb933592017-11-22 13:49:43 -0800174 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700175}
176
Colin Cross89536d42017-07-07 14:35:50 -0700177type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700178 // list of module-specific flags that will be used for dex compiles
179 Dxflags []string `android:"arch_variant"`
180
Colin Cross83bb3162018-06-25 15:48:06 -0700181 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
182 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800183 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700184
Colin Cross83bb3162018-06-25 15:48:06 -0700185 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
186 // Defaults to sdk_version if not set.
187 Min_sdk_version *string
188
Dan Willemsen419290a2018-10-31 15:28:47 -0700189 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
190 // Defaults to sdk_version if not set.
191 Target_sdk_version *string
192
Colin Cross6af2e492018-05-22 11:12:33 -0700193 // if true, compile against the platform APIs instead of an SDK.
194 Platform_apis *bool
195
Colin Crossebe1a512017-11-14 13:12:14 -0800196 Aidl struct {
197 // Top level directories to pass to aidl tool
198 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700199
Colin Crossebe1a512017-11-14 13:12:14 -0800200 // Directories rooted at the Android.bp file to pass to aidl tool
201 Local_include_dirs []string
202
203 // directories that should be added as include directories for any aidl sources of modules
204 // that depend on this module, as well as to aidl for this module.
205 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100206
207 // whether to generate traces (for systrace) for this interface
208 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100209
210 // whether to generate Binder#GetTransaction name method.
211 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800212 }
Colin Cross92430102017-10-09 14:59:32 -0700213
214 // If true, export a copy of the module as a -hostdex module for host testing.
215 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700216
David Brazdil17ef5632018-06-27 10:27:45 +0100217 // If set to true, compile dex regardless of installable. Defaults to false.
218 Compile_dex *bool
219
Colin Cross66dbc0b2017-12-28 12:23:20 -0800220 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700221 // If false, disable all optimization. Defaults to true for android_app and android_test
222 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800223 Enabled *bool
224
225 // If true, optimize for size by removing unused code. Defaults to true for apps,
226 // false for libraries and tests.
227 Shrink *bool
228
229 // If true, optimize bytecode. Defaults to false.
230 Optimize *bool
231
232 // If true, obfuscate bytecode. Defaults to false.
233 Obfuscate *bool
234
235 // If true, do not use the flag files generated by aapt that automatically keep
236 // classes referenced by the app manifest. Defaults to false.
237 No_aapt_flags *bool
238
239 // Flags to pass to proguard.
240 Proguard_flags []string
241
242 // Specifies the locations of files containing proguard flags.
243 Proguard_flags_files []string
244 }
245
Colin Cross1369cdb2017-09-29 17:58:17 -0700246 // When targeting 1.9, override the modules to use with --system
247 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700248
249 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800250 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700251}
252
Colin Cross46c9b8b2017-06-22 16:51:17 -0700253// Module contains the properties and members used by all java module types
254type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700255 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700256 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700257
Colin Cross89536d42017-07-07 14:35:50 -0700258 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700259 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700260 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700261
Colin Cross331a1212018-08-15 20:40:52 -0700262 // jar file containing header classes including static library dependencies, suitable for
263 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700264 headerJarFile android.Path
265
Colin Cross331a1212018-08-15 20:40:52 -0700266 // jar file containing implementation classes including static library dependencies but no
267 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700268 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700269
Colin Cross331a1212018-08-15 20:40:52 -0700270 // jar file containing only resources including from static library dependencies
271 resourceJar android.Path
272
273 // jar file containing implementation classes and resources including static library
274 // dependencies
275 implementationAndResourcesJar android.Path
276
277 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700278 dexJarFile android.Path
279
Colin Cross43f08db2018-11-12 10:13:39 -0800280 // output file that contains classes.dex if it should be in the output file
281 maybeStrippedDexJarFile android.Path
282
Colin Crosscb933592017-11-22 13:49:43 -0800283 // output file containing uninstrumented classes that will be instrumented by jacoco
284 jacocoReportClassesFile android.Path
285
Colin Cross66dbc0b2017-12-28 12:23:20 -0800286 // output file containing mapping of obfuscated names
287 proguardDictionary android.Path
288
Colin Cross331a1212018-08-15 20:40:52 -0700289 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700290 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700291
Colin Cross635c3b02016-05-18 15:37:25 -0700292 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700293
Colin Cross635c3b02016-05-18 15:37:25 -0700294 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700295
Colin Cross2fe66872015-03-30 17:20:39 -0700296 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700297 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800298
299 // list of .java files and srcjars that was passed to javac
300 compiledJavaSrcs android.Paths
301 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800302
303 // list of extra progurad flag files
304 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900305
Colin Cross094054a2018-10-17 15:10:48 -0700306 // manifest file to use instead of properties.Manifest
307 overrideManifest android.OptionalPath
308
Jiyong Park1be96912018-05-28 18:02:19 +0900309 // list of SDK lib names that this java moudule is exporting
310 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700311
312 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
313 // filter out Exclude_srcs, will be used by android.IDEInfo struct
314 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800315
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800316 // expanded Jarjar_rules
317 expandJarjarRules android.Path
318
Colin Cross43f08db2018-11-12 10:13:39 -0800319 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700320}
321
Colin Cross54250902017-12-05 09:28:08 -0800322func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700323 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800324}
325
Jiyong Park8fd61922018-11-08 02:50:25 +0900326func (j *Module) DexJarFile() android.Path {
327 return j.dexJarFile
328}
329
Colin Cross54250902017-12-05 09:28:08 -0800330var _ android.SourceFileProducer = (*Module)(nil)
331
Colin Crossf506d872017-07-19 15:53:04 -0700332type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700333 HeaderJars() android.Paths
334 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700335 ResourceJars() android.Paths
336 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700337 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900338 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700339}
340
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341type SdkLibraryDependency interface {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900342 HeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
343 ImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900344}
345
Nan Zhangb2b33de2018-02-23 11:18:47 -0800346type SrcDependency interface {
347 CompiledSrcs() android.Paths
348 CompiledSrcJars() android.Paths
349}
350
351func (j *Module) CompiledSrcs() android.Paths {
352 return j.compiledJavaSrcs
353}
354
355func (j *Module) CompiledSrcJars() android.Paths {
356 return j.compiledSrcJars
357}
358
359var _ SrcDependency = (*Module)(nil)
360
Colin Cross89536d42017-07-07 14:35:50 -0700361func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
362 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
363 android.InitDefaultableModule(module)
364}
365
Colin Crossbe1da472017-07-07 15:59:46 -0700366type dependencyTag struct {
367 blueprint.BaseDependencyTag
368 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700369}
370
Colin Crossa4f08812018-10-02 22:03:40 -0700371type jniDependencyTag struct {
372 blueprint.BaseDependencyTag
373 target android.Target
374}
375
Colin Crossbe1da472017-07-07 15:59:46 -0700376var (
Colin Cross4b964c02018-10-15 16:18:06 -0700377 staticLibTag = dependencyTag{name: "staticlib"}
378 libTag = dependencyTag{name: "javalib"}
379 annoTag = dependencyTag{name: "annotation processor"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800380 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700381 bootClasspathTag = dependencyTag{name: "bootclasspath"}
382 systemModulesTag = dependencyTag{name: "system modules"}
383 frameworkResTag = dependencyTag{name: "framework-res"}
384 frameworkApkTag = dependencyTag{name: "framework-apk"}
385 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800386 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700387 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
388 certificateTag = dependencyTag{name: "certificate"}
389 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700390)
Colin Cross2fe66872015-03-30 17:20:39 -0700391
Colin Crossfc3674a2017-09-18 17:41:52 -0700392type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700393 useModule, useFiles, useDefaultLibs, invalidVersion bool
394
Colin Cross86a60ae2018-05-29 14:44:55 -0700395 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700396 systemModules string
397
Colin Crossa97c5d32018-03-28 14:58:31 -0700398 frameworkResModule string
399
Colin Cross86a60ae2018-05-29 14:44:55 -0700400 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700401 aidl android.Path
402}
403
Colin Crossa4f08812018-10-02 22:03:40 -0700404type jniLib struct {
405 name string
406 path android.Path
407 target android.Target
408}
409
Colin Cross3144dfc2018-01-03 15:06:47 -0800410func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
411 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
412}
413
414func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
415 return j.shouldInstrument(ctx) &&
416 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
417 ctx.Config().UnbundledBuild())
418}
419
Colin Cross83bb3162018-06-25 15:48:06 -0700420func (j *Module) sdkVersion() string {
421 return String(j.deviceProperties.Sdk_version)
422}
423
424func (j *Module) minSdkVersion() string {
425 if j.deviceProperties.Min_sdk_version != nil {
426 return *j.deviceProperties.Min_sdk_version
427 }
428 return j.sdkVersion()
429}
430
Dan Willemsen419290a2018-10-31 15:28:47 -0700431func (j *Module) targetSdkVersion() string {
432 if j.deviceProperties.Target_sdk_version != nil {
433 return *j.deviceProperties.Target_sdk_version
434 }
435 return j.sdkVersion()
436}
437
Colin Crossbe1da472017-07-07 15:59:46 -0700438func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700439 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700440 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700441 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700442 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700443 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100444 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700445 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700446 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700447 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700448 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100449 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700450 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800451 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700452 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
453 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800454 }
Colin Crossbe1da472017-07-07 15:59:46 -0700455 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700456 } else if j.deviceProperties.System_modules == nil {
457 ctx.PropertyErrorf("no_standard_libs",
458 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100459 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700460 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700461 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100462 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700463 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800464 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800465 if ctx.ModuleName() == "android_stubs_current" ||
466 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700467 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700468 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800469 }
Colin Cross2fe66872015-03-30 17:20:39 -0700470 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700471
Colin Cross42d48b72018-08-29 14:10:52 -0700472 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
473 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100474 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700475 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100476 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700477
Colin Crossbe9cdb82019-01-21 21:37:16 -0800478 ctx.AddFarVariationDependencies([]blueprint.Variation{
479 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
480 }, pluginTag, j.properties.Plugins...)
481
Colin Cross7f9036c2017-08-30 13:27:57 -0700482 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000483 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700484 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800485 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800486 android.ExtractSourceDeps(ctx, j.properties.Jarjar_rules)
Colin Cross6af17aa2017-09-20 12:59:05 -0700487
488 if j.hasSrcExt(".proto") {
489 protoDeps(ctx, &j.protoProperties)
490 }
Colin Cross93e85952017-08-15 13:34:18 -0700491
492 if j.hasSrcExt(".kt") {
493 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
494 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700495 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross3a3e94c2019-01-23 15:39:50 -0800496 if len(j.properties.Annotation_processors) > 0 || len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800497 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
498 }
Colin Cross93e85952017-08-15 13:34:18 -0700499 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800500
501 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700502 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800503 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700504}
505
506func hasSrcExt(srcs []string, ext string) bool {
507 for _, src := range srcs {
508 if filepath.Ext(src) == ext {
509 return true
510 }
511 }
512
513 return false
514}
515
Nan Zhang61eaedb2017-11-02 13:28:15 -0700516func shardPaths(paths android.Paths, shardSize int) []android.Paths {
517 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
518 for len(paths) > shardSize {
519 ret = append(ret, paths[0:shardSize])
520 paths = paths[shardSize:]
521 }
522 if len(paths) > 0 {
523 ret = append(ret, paths)
524 }
525 return ret
526}
527
Colin Cross6af17aa2017-09-20 12:59:05 -0700528func (j *Module) hasSrcExt(ext string) bool {
529 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700530}
531
Colin Cross46c9b8b2017-06-22 16:51:17 -0700532func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700533 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700534
Colin Crossebe1a512017-11-14 13:12:14 -0800535 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
536 aidlIncludes = append(aidlIncludes,
537 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
538 aidlIncludes = append(aidlIncludes,
539 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700540
Steven Moreland667f6882018-07-26 12:55:08 -0700541 flags := []string{"-b"}
542
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700543 if aidlPreprocess.Valid() {
544 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700545 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700546 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700547 }
548
Colin Cross635c3b02016-05-18 15:37:25 -0700549 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800550 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700551 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800552 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700553 flags = append(flags, "-I"+src.String())
554 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700555
Martijn Coeneneab15642018-03-09 09:29:59 +0100556 if Bool(j.deviceProperties.Aidl.Generate_traces) {
557 flags = append(flags, "-t")
558 }
559
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100560 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
561 flags = append(flags, "--transaction_names")
562 }
563
Colin Crossf03c82b2015-04-13 13:53:40 -0700564 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700565}
566
Colin Cross32f676a2017-09-06 13:41:06 -0700567type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800568 classpath classpath
569 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700570 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800571 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700572 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700573 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700574 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700575 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800576 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700577 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700578 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700579 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700580 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800581 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800582
583 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700584}
Colin Cross2fe66872015-03-30 17:20:39 -0700585
Colin Cross54250902017-12-05 09:28:08 -0800586func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
587 for _, f := range dep.Srcs() {
588 if f.Ext() != ".jar" {
589 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
590 ctx.OtherModuleName(dep.(blueprint.Module)))
591 }
592 }
593}
594
Jiyong Park2d492942018-03-05 17:44:10 +0900595type linkType int
596
597const (
598 javaCore linkType = iota
599 javaSdk
600 javaSystem
601 javaPlatform
602)
603
Jiyong Park46f78fb2018-10-20 16:33:17 +0900604func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700605 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700606 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900607 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
608 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
609 name == "core-lambda-stubs":
610 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100611 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900612 return javaCore, false
613 case name == "android_system_stubs_current":
614 return javaSystem, true
615 case strings.HasPrefix(ver, "system_"):
616 return javaSystem, false
617 case name == "android_test_stubs_current":
618 return javaSystem, true
619 case strings.HasPrefix(ver, "test_"):
620 return javaPlatform, false
621 case name == "android_stubs_current":
622 return javaSdk, true
623 case ver == "current":
624 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700625 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900626 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700627 default:
628 if _, err := strconv.Atoi(ver); err != nil {
629 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
630 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900631 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900632 }
633}
634
Jiyong Park750e5572018-01-31 00:20:13 +0900635func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700636 if ctx.Host() {
637 return
638 }
639
Jiyong Park46f78fb2018-10-20 16:33:17 +0900640 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
641 if stubs {
642 return
643 }
644 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900645 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."
646
647 switch myLinkType {
648 case javaCore:
649 if otherLinkType != javaCore {
650 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 +0900651 ctx.OtherModuleName(to))
652 }
Jiyong Park2d492942018-03-05 17:44:10 +0900653 break
654 case javaSdk:
655 if otherLinkType != javaCore && otherLinkType != javaSdk {
656 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
657 ctx.OtherModuleName(to))
658 }
659 break
660 case javaSystem:
661 if otherLinkType == javaPlatform {
662 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
663 ctx.OtherModuleName(to))
664 }
665 break
666 case javaPlatform:
667 // no restriction on link-type
668 break
Jiyong Park750e5572018-01-31 00:20:13 +0900669 }
670}
671
Colin Cross32f676a2017-09-06 13:41:06 -0700672func (j *Module) collectDeps(ctx android.ModuleContext) deps {
673 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700674
Colin Cross300f0382018-03-06 13:11:51 -0800675 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700676 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800677 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700678 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800679 } else if sdkDep.useFiles {
680 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700681 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800682 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
683 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700684 }
685
Colin Crossd11fcda2017-10-23 17:59:01 -0700686 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700687 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700688 tag := ctx.OtherModuleDependencyTag(module)
689
Colin Crossa4f08812018-10-02 22:03:40 -0700690 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700691 // Handled by AndroidApp.collectAppDeps
692 return
693 }
694 if tag == certificateTag {
695 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700696 return
697 }
698
Jiyong Park750e5572018-01-31 00:20:13 +0900699 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700700 switch tag {
701 case bootClasspathTag, libTag, staticLibTag:
702 checkLinkType(ctx, j, to, tag.(dependencyTag))
703 }
Jiyong Park750e5572018-01-31 00:20:13 +0900704 }
Colin Cross54250902017-12-05 09:28:08 -0800705 switch dep := module.(type) {
706 case Dependency:
707 switch tag {
708 case bootClasspathTag:
709 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700710 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800711 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900712 // sdk lib names from dependencies are re-exported
713 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800714 case staticLibTag:
715 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
716 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
717 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700718 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900719 // sdk lib names from dependencies are re-exported
720 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700721 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700722 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800723 case pluginTag:
724 if plugin, ok := dep.(*Plugin); ok {
725 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
726 if plugin.pluginProperties.Processor_class != nil {
727 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
728 }
729 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
730 } else {
731 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
732 }
Colin Cross54250902017-12-05 09:28:08 -0800733 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100734 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800735 // framework.jar has a one-off dependency on the R.java and Manifest.java files
736 // generated by framework-res.apk
737 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
738 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800739 case frameworkApkTag:
740 if ctx.ModuleName() == "android_stubs_current" ||
741 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700742 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800743 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
744 // resource files out of there for aapt.
745 //
746 // Normally the package rule runs aapt, which includes the resource,
747 // but we're not running that in our package rule so just copy in the
748 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700749 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800750 }
Colin Cross54250902017-12-05 09:28:08 -0800751 case kotlinStdlibTag:
752 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800753 case kotlinAnnotationsTag:
754 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800755 }
756
757 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900758 case SdkLibraryDependency:
759 switch tag {
760 case libTag:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900761 deps.classpath = append(deps.classpath, dep.HeaderJars(ctx, j.sdkVersion())...)
Jiyong Park1be96912018-05-28 18:02:19 +0900762 // names of sdk libs that are directly depended are exported
763 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900764 default:
765 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
766 }
Colin Cross54250902017-12-05 09:28:08 -0800767 case android.SourceFileProducer:
768 switch tag {
769 case libTag:
770 checkProducesJars(ctx, dep)
771 deps.classpath = append(deps.classpath, dep.Srcs()...)
772 case staticLibTag:
773 checkProducesJars(ctx, dep)
774 deps.classpath = append(deps.classpath, dep.Srcs()...)
775 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
776 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
777 case android.DefaultsDepTag, android.SourceDepTag:
778 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900779 case publicApiFileTag, systemApiFileTag, testApiFileTag:
780 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800781 default:
782 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
783 }
784 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700785 switch tag {
786 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700787 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700788 case systemModulesTag:
789 if deps.systemModules != nil {
790 panic("Found two system module dependencies")
791 }
792 sm := module.(*SystemModules)
793 if sm.outputFile == nil {
794 panic("Missing directory for system module dependency")
795 }
796 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700797 default:
798 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700799 }
Colin Crossec7a0422017-07-07 14:47:12 -0700800 }
Colin Cross2fe66872015-03-30 17:20:39 -0700801 })
802
Jiyong Park1be96912018-05-28 18:02:19 +0900803 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
804
Colin Cross32f676a2017-09-06 13:41:06 -0700805 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700806}
807
Colin Cross83bb3162018-06-25 15:48:06 -0700808func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700809 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800810 v := sdkContext.sdkVersion()
811 // For PDK builds, use the latest SDK version instead of "current"
812 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
813 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
814 latestSdkVersion := 0
815 if len(sdkVersions) > 0 {
816 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
817 }
818 v = strconv.Itoa(latestSdkVersion)
819 }
820
821 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700822 if err != nil {
823 ctx.PropertyErrorf("sdk_version", "%s", err)
824 }
Nan Zhang357466b2018-04-17 17:38:36 -0700825 if javaVersion != "" {
826 ret = javaVersion
827 } else if ctx.Device() && sdk <= 23 {
828 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900829 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700830 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700831 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700832 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
833 ret = "1.8"
834 } else {
835 ret = "1.9"
836 }
837
838 return ret
839}
840
Nan Zhanged19fc32017-10-19 13:06:22 -0700841func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700842
Colin Crossf03c82b2015-04-13 13:53:40 -0700843 var flags javaBuilderFlags
844
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100845 // javaVersion flag.
846 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
847
Nan Zhanged19fc32017-10-19 13:06:22 -0700848 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700849 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100850 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700851 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700852 }
Colin Cross6510f912017-11-29 00:27:14 -0800853 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700854 // Override the -g flag passed globally to remove local variable debug info to reduce
855 // disk and memory usage.
856 javacFlags = append(javacFlags, "-g:source,lines")
857 }
Colin Cross64162712017-08-08 13:17:59 -0700858
Colin Cross66548102018-06-19 22:47:35 -0700859 if ctx.Config().RunErrorProne() {
860 if config.ErrorProneClasspath == nil {
861 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
862 }
863
864 errorProneFlags := []string{
865 "-Xplugin:ErrorProne",
866 "${config.ErrorProneChecks}",
867 }
868 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
869
870 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
871 "'" + strings.Join(errorProneFlags, " ") + "'"
872 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800873 }
874
Nan Zhanged19fc32017-10-19 13:06:22 -0700875 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800876 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
877 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700878 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800879
Colin Crossbe9cdb82019-01-21 21:37:16 -0800880 flags.processor = strings.Join(deps.processorClasses, ",")
881
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100882 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800883 !Bool(j.properties.No_standard_libs) &&
884 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
885 // Give host-side tools a version of OpenJDK's standard libraries
886 // close to what they're targeting. As of Dec 2017, AOSP is only
887 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
888 //
889 // When building with OpenJDK 8, the following should have no
890 // effect since those jars would be available by default.
891 //
892 // When building with OpenJDK 9 but targeting a version < 1.8,
893 // putting them on the bootclasspath means that:
894 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
895 // b) references to existing APIs are not reinterpreted in an
896 // OpenJDK 9-specific way, eg. calls to subclasses of
897 // java.nio.Buffer as in http://b/70862583
898 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
899 flags.bootClasspath = append(flags.bootClasspath,
900 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
901 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800902 if Bool(j.properties.Use_tools_jar) {
903 flags.bootClasspath = append(flags.bootClasspath,
904 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
905 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800906 }
907
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100908 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800909 // Manually specify build directory in case it is not under the repo root.
910 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
911 // just adding a symlink under the root doesn't help.)
912 patchPaths := ".:" + ctx.Config().BuildDir()
913 classPath := flags.classpath.FormJavaClassPath("")
914 if classPath != "" {
915 patchPaths += ":" + classPath
916 }
917 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700918 }
919
Nan Zhanged19fc32017-10-19 13:06:22 -0700920 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700921 if deps.systemModules != nil {
922 flags.systemModules = append(flags.systemModules, deps.systemModules)
923 }
924
Nan Zhanged19fc32017-10-19 13:06:22 -0700925 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700926 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700927 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700928 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700929 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
930 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700931 }
932
Colin Cross81440082018-08-15 20:21:55 -0700933 if len(javacFlags) > 0 {
934 // optimization.
935 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
936 flags.javacFlags = "$javacFlags"
937 }
938
Nan Zhanged19fc32017-10-19 13:06:22 -0700939 return flags
940}
Colin Crossc0b06f12015-04-08 13:03:43 -0700941
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800942func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700943
Colin Crossebe1a512017-11-14 13:12:14 -0800944 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700945
946 deps := j.collectDeps(ctx)
947 flags := j.collectBuilderFlags(ctx, deps)
948
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100949 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700950 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
951 }
952 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700953 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800954 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700955 }
956
Colin Crossaf050172017-11-15 23:01:59 -0800957 srcFiles = j.genSources(ctx, srcFiles, flags)
958
959 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700960 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800961 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700962
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700963 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
964 // that IDEInfo struct will use
965 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
966
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800967 if j.properties.Jarjar_rules != nil {
968 j.expandJarjarRules = ctx.ExpandSource(*j.properties.Jarjar_rules, "jarjar_rules")
969 }
970
Colin Cross1ee23172017-10-18 14:44:18 -0700971 jarName := ctx.ModuleName() + ".jar"
972
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000973 javaSrcFiles := srcFiles.FilterByExt(".java")
974 var uniqueSrcFiles android.Paths
975 set := make(map[string]bool)
976 for _, v := range javaSrcFiles {
977 if _, found := set[v.String()]; !found {
978 set[v.String()] = true
979 uniqueSrcFiles = append(uniqueSrcFiles, v)
980 }
981 }
982
Colin Cross55f63ea2018-08-27 12:37:09 -0700983 var kotlinJars android.Paths
984
Colin Cross93e85952017-08-15 13:34:18 -0700985 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200986 // user defined kotlin flags.
987 kotlincFlags := j.properties.Kotlincflags
988 CheckKotlincFlags(ctx, kotlincFlags)
989
Colin Cross93e85952017-08-15 13:34:18 -0700990 // If there are kotlin files, compile them first but pass all the kotlin and java files
991 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
992 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200993 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700994 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200995 kotlincFlags = append(kotlincFlags, "-no-jdk")
996 }
997 if len(kotlincFlags) > 0 {
998 // optimization.
999 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1000 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001001 }
1002
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001003 var kotlinSrcFiles android.Paths
1004 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1005 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1006
Colin Crossafbb1732019-01-17 15:42:52 -08001007 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1008 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1009
1010 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1011 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1012
1013 if len(flags.processorPath) > 0 {
1014 // Use kapt for annotation processing
1015 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1016 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1017 srcJars = append(srcJars, kaptSrcJar)
1018 // Disable annotation processing in javac, it's already been handled by kapt
1019 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001020 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001021 }
Colin Cross93e85952017-08-15 13:34:18 -07001022
Colin Cross1ee23172017-10-18 14:44:18 -07001023 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001024 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001025 if ctx.Failed() {
1026 return
1027 }
1028
1029 // Make javac rule depend on the kotlinc rule
1030 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001031
Colin Cross93e85952017-08-15 13:34:18 -07001032 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001033 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001034 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001035 }
1036
Colin Cross55f63ea2018-08-27 12:37:09 -07001037 jars := append(android.Paths(nil), kotlinJars...)
1038
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001039 // Store the list of .java files that was passed to javac
1040 j.compiledJavaSrcs = uniqueSrcFiles
1041 j.compiledSrcJars = srcJars
1042
Nan Zhang61eaedb2017-11-02 13:28:15 -07001043 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001044 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001045 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1046 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001047 // Formerly, there was a check here that prevented annotation processors
1048 // from being used when sharding was enabled, as some annotation processors
1049 // do not function correctly in sharded environments. It was removed to
1050 // allow for the use of annotation processors that do function correctly
1051 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001052 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001053 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001054 if ctx.Failed() {
1055 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001056 }
1057 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001058 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001059 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001060 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001061 // If error-prone is enabled, add an additional rule to compile the java files into
1062 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001063 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001064 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1065 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001066 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001067 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001068 extraJarDeps = append(extraJarDeps, errorprone)
1069 }
1070
Nan Zhang61eaedb2017-11-02 13:28:15 -07001071 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001072 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001073 shardSize := int(*(j.properties.Javac_shard_size))
1074 var shardSrcs []android.Paths
1075 if len(uniqueSrcFiles) > 0 {
1076 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1077 for idx, shardSrc := range shardSrcs {
1078 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1079 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1080 jars = append(jars, classes)
1081 }
1082 }
1083 if len(srcJars) > 0 {
1084 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1085 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1086 jars = append(jars, classes)
1087 }
1088 } else {
1089 classes := android.PathForModuleOut(ctx, "javac", jarName)
1090 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1091 jars = append(jars, classes)
1092 }
Colin Crossd6891432017-09-27 17:39:56 -07001093 if ctx.Failed() {
1094 return
1095 }
Colin Cross2fe66872015-03-30 17:20:39 -07001096 }
1097
Colin Crosscedd4762018-09-13 11:26:19 -07001098 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1099 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001100 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1101
1102 var resArgs []string
1103 var resDeps android.Paths
1104
1105 resArgs = append(resArgs, dirArgs...)
1106 resDeps = append(resDeps, dirDeps...)
1107
1108 resArgs = append(resArgs, fileArgs...)
1109 resDeps = append(resDeps, fileDeps...)
1110
Colin Crossff3ae9d2018-04-10 16:15:18 -07001111 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001112 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001113 resArgs = append(resArgs, srcArgs...)
1114 resDeps = append(resDeps, srcDeps...)
1115 }
Colin Cross40a36712017-09-27 17:41:35 -07001116
1117 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001118 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001119 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001120 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001121 if ctx.Failed() {
1122 return
1123 }
1124 }
1125
Colin Cross331a1212018-08-15 20:40:52 -07001126 if len(deps.staticResourceJars) > 0 {
1127 var jars android.Paths
1128 if j.resourceJar != nil {
1129 jars = append(jars, j.resourceJar)
1130 }
1131 jars = append(jars, deps.staticResourceJars...)
1132
1133 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1134 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1135 false, nil, nil)
1136 j.resourceJar = combinedJar
1137 }
1138
Colin Cross32f676a2017-09-06 13:41:06 -07001139 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001140 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001141
Colin Cross094054a2018-10-17 15:10:48 -07001142 manifest := j.overrideManifest
1143 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001144 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1145 }
Colin Cross635acc92017-09-12 22:50:46 -07001146
Colin Cross0a6e0072017-08-30 14:24:55 -07001147 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001148 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001149 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001150
1151 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001152 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1153 // Optimization: skip the combine step if there is nothing to do
1154 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1155 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1156 // any if len(jars) == 1.
1157 outputFile = moduleOutPath
1158 } else {
1159 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1160 ctx.Build(pctx, android.BuildParams{
1161 Rule: android.Cp,
1162 Input: jars[0],
1163 Output: combinedJar,
1164 })
1165 outputFile = combinedJar
1166 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001167 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001168 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001169 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001170 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001171 outputFile = combinedJar
1172 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001173
Colin Cross331a1212018-08-15 20:40:52 -07001174 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001175 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001176 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001177 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001178 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001179 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001180
1181 // jarjar resource jar if necessary
1182 if j.resourceJar != nil {
1183 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001184 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001185 j.resourceJar = resourceJarJarFile
1186 }
1187
Colin Cross0a6e0072017-08-30 14:24:55 -07001188 if ctx.Failed() {
1189 return
1190 }
1191 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001192 j.implementationJarFile = outputFile
1193 if j.headerJarFile == nil {
1194 j.headerJarFile = j.implementationJarFile
1195 }
Colin Cross2fe66872015-03-30 17:20:39 -07001196
Colin Cross6510f912017-11-29 00:27:14 -08001197 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001198 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1199 j.properties.Instrument = true
1200 }
1201 }
1202
Colin Cross3144dfc2018-01-03 15:06:47 -08001203 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001204 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1205 }
1206
Colin Cross331a1212018-08-15 20:40:52 -07001207 // merge implementation jar with resources if necessary
1208 implementationAndResourcesJar := outputFile
1209 if j.resourceJar != nil {
1210 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1211 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1212 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1213 false, nil, nil)
1214 implementationAndResourcesJar = combinedJar
1215 }
1216
1217 j.implementationAndResourcesJar = implementationAndResourcesJar
1218
Colin Cross9ae1b922018-06-26 17:59:05 -07001219 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001220 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001221 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001222 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001223 if ctx.Failed() {
1224 return
1225 }
Colin Cross331a1212018-08-15 20:40:52 -07001226
Colin Cross8faf8fc2019-01-16 15:15:52 -08001227 // Hidden API CSV generation and dex encoding
David Brazdil9fc36a62019-01-18 11:12:05 +00001228 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
1229 isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars())
1230 if isBootJar || inList(ctx.ModuleName(), ctx.Config().HiddenAPIExtraAppUsageJars()) {
1231 // Derive the greylist from classes jar.
1232 hiddenAPIGenerateCSV(ctx, j.implementationJarFile)
1233 }
1234 if isBootJar {
1235 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", jarName)
Colin Crosscd964b32019-01-18 22:03:02 -08001236 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexOutputFile, j.deviceProperties.UncompressDex)
David Brazdil9fc36a62019-01-18 11:12:05 +00001237 dexOutputFile = hiddenAPIJar
1238 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001239 }
1240
Colin Cross331a1212018-08-15 20:40:52 -07001241 // merge dex jar with resources if necessary
1242 if j.resourceJar != nil {
1243 jars := android.Paths{dexOutputFile, j.resourceJar}
1244 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1245 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1246 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001247 if j.deviceProperties.UncompressDex {
1248 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1249 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1250 dexOutputFile = combinedAlignedJar
1251 } else {
1252 dexOutputFile = combinedJar
1253 }
Colin Cross331a1212018-08-15 20:40:52 -07001254 }
1255
1256 j.dexJarFile = dexOutputFile
1257
Colin Cross8faf8fc2019-01-16 15:15:52 -08001258 // Dexpreopting
Colin Crossdc2da912019-01-05 22:13:05 -08001259 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross2fc72f62018-12-21 12:59:54 -08001260 j.dexpreopter.uncompressedDex = j.deviceProperties.UncompressDex
Colin Cross43f08db2018-11-12 10:13:39 -08001261 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1262
1263 j.maybeStrippedDexJarFile = dexOutputFile
1264
Colin Cross3063b782018-08-15 11:19:12 -07001265 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001266
1267 if ctx.Failed() {
1268 return
1269 }
Colin Cross331a1212018-08-15 20:40:52 -07001270 } else {
1271 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001272 }
Colin Cross331a1212018-08-15 20:40:52 -07001273
Colin Crossb7a63242015-04-16 14:09:14 -07001274 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001275
1276 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1277 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001278}
1279
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001280// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1281// since some of these flags may be used internally.
1282func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1283 for _, flag := range flags {
1284 flag = strings.TrimSpace(flag)
1285
1286 if !strings.HasPrefix(flag, "-") {
1287 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1288 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1289 ctx.PropertyErrorf("kotlincflags",
1290 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1291 } else if inList(flag, config.KotlincIllegalFlags) {
1292 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1293 } else if flag == "-include-runtime" {
1294 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1295 } else {
1296 args := strings.Split(flag, " ")
1297 if args[0] == "-kotlin-home" {
1298 ctx.PropertyErrorf("kotlincflags",
1299 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1300 }
1301 }
1302 }
1303}
1304
Colin Cross8eadbf02017-10-24 17:46:00 -07001305func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001306 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001307
1308 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001309 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001310 // Compile java sources into turbine.jar.
1311 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1312 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1313 if ctx.Failed() {
1314 return nil
1315 }
1316 jars = append(jars, turbineJar)
1317 }
1318
Colin Cross55f63ea2018-08-27 12:37:09 -07001319 jars = append(jars, extraJars...)
1320
Nan Zhanged19fc32017-10-19 13:06:22 -07001321 // Combine any static header libraries into classes-header.jar. If there is only
1322 // one input jar this step will be skipped.
1323 var headerJar android.Path
1324 jars = append(jars, deps.staticHeaderJars...)
1325
Colin Cross5c6ecc12017-10-23 18:12:27 -07001326 // we cannot skip the combine step for now if there is only one jar
1327 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1328 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001329 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1330 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001331 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001332
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001333 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001334 // Transform classes.jar into classes-jarjar.jar
1335 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001336 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001337 headerJar = jarjarFile
1338 if ctx.Failed() {
1339 return nil
1340 }
1341 }
1342
1343 return headerJar
1344}
1345
Colin Crosscb933592017-11-22 13:49:43 -08001346func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001347 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001348
Colin Cross7a3139e2017-12-19 13:57:50 -08001349 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001350
Colin Cross84c38822018-01-03 15:59:46 -08001351 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001352 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1353
1354 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1355
1356 j.jacocoReportClassesFile = jacocoReportClassesFile
1357
1358 return instrumentedJar
1359}
1360
albaltai36ff7dc2018-12-25 14:35:23 +08001361var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001362
Nan Zhanged19fc32017-10-19 13:06:22 -07001363func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001364 if j.headerJarFile == nil {
1365 return nil
1366 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001367 return android.Paths{j.headerJarFile}
1368}
1369
1370func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001371 if j.implementationJarFile == nil {
1372 return nil
1373 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001374 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001375}
1376
Colin Cross331a1212018-08-15 20:40:52 -07001377func (j *Module) ResourceJars() android.Paths {
1378 if j.resourceJar == nil {
1379 return nil
1380 }
1381 return android.Paths{j.resourceJar}
1382}
1383
1384func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001385 if j.implementationAndResourcesJar == nil {
1386 return nil
1387 }
Colin Cross331a1212018-08-15 20:40:52 -07001388 return android.Paths{j.implementationAndResourcesJar}
1389}
1390
Colin Cross46c9b8b2017-06-22 16:51:17 -07001391func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001392 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001393 return j.exportAidlIncludeDirs
1394}
1395
Jiyong Park1be96912018-05-28 18:02:19 +09001396func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001397 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001398 return j.exportedSdkLibs
1399}
1400
Colin Cross46c9b8b2017-06-22 16:51:17 -07001401var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001402
Colin Cross46c9b8b2017-06-22 16:51:17 -07001403func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001404 return j.logtagsSrcs
1405}
1406
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001407// Collect information for opening IDE project files in java/jdeps.go.
1408func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1409 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1410 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1411 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001412 if j.expandJarjarRules != nil {
1413 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001414 }
1415}
1416
1417func (j *Module) CompilerDeps() []string {
1418 jdeps := []string{}
1419 jdeps = append(jdeps, j.properties.Libs...)
1420 jdeps = append(jdeps, j.properties.Static_libs...)
1421 return jdeps
1422}
1423
Colin Cross2fe66872015-03-30 17:20:39 -07001424//
1425// Java libraries (.jar file)
1426//
1427
Colin Crossf506d872017-07-19 15:53:04 -07001428type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001429 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001430}
1431
Colin Cross2fc72f62018-12-21 12:59:54 -08001432func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001433 // Store uncompressed (and do not strip) dex files from boot class path jars that are not
1434 // part of the boot image.
1435 if inList(ctx.ModuleName(), ctx.Config().BootJars()) &&
1436 !inList(ctx.ModuleName(), ctx.Config().PreoptBootJars()) {
1437 return true
1438 }
Colin Cross2fc72f62018-12-21 12:59:54 -08001439 return false
1440}
1441
Colin Crossf506d872017-07-19 15:53:04 -07001442func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001443 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1444 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross2fc72f62018-12-21 12:59:54 -08001445 j.deviceProperties.UncompressDex = j.shouldUncompressDex(ctx)
Colin Cross46c9b8b2017-06-22 16:51:17 -07001446 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001447
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001448 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001449 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1450 ctx.ModuleName()+".jar", j.outputFile)
1451 }
Colin Crossb7a63242015-04-16 14:09:14 -07001452}
1453
Colin Crossf506d872017-07-19 15:53:04 -07001454func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001455 j.deps(ctx)
1456}
1457
Colin Cross9ae1b922018-06-26 17:59:05 -07001458func LibraryFactory() android.Module {
1459 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001460
Colin Cross9ae1b922018-06-26 17:59:05 -07001461 module.AddProperties(
1462 &module.Module.properties,
1463 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001464 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001465 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001466
Colin Cross9ae1b922018-06-26 17:59:05 -07001467 InitJavaModule(module, android.HostAndDeviceSupported)
1468 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001469}
1470
Colin Crossf506d872017-07-19 15:53:04 -07001471func LibraryHostFactory() android.Module {
1472 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001473
Colin Cross6af17aa2017-09-20 12:59:05 -07001474 module.AddProperties(
1475 &module.Module.properties,
1476 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001477
Colin Cross9ae1b922018-06-26 17:59:05 -07001478 module.Module.properties.Installable = proptools.BoolPtr(true)
1479
Colin Cross89536d42017-07-07 14:35:50 -07001480 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001481 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001482}
1483
1484//
Colin Crossb628ea52018-08-14 16:42:33 -07001485// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001486//
1487
1488type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001489 // list of compatibility suites (for example "cts", "vts") that the module should be
1490 // installed into.
1491 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001492
1493 // the name of the test configuration (for example "AndroidTest.xml") that should be
1494 // installed with the module.
1495 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001496
Jack He33338892018-09-19 02:21:28 -07001497 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1498 // should be installed with the module.
1499 Test_config_template *string `android:"arch_variant"`
1500
Colin Crossd96ca352018-08-10 16:06:24 -07001501 // list of files or filegroup modules that provide data that should be installed alongside
1502 // the test
1503 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001504}
1505
1506type Test struct {
1507 Library
1508
1509 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001510
1511 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001512 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001513}
1514
1515func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001516 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001517 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001518
1519 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001520}
1521
1522func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1523 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001524 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001525 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001526 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001527}
1528
1529func TestFactory() android.Module {
1530 module := &Test{}
1531
1532 module.AddProperties(
1533 &module.Module.properties,
1534 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001535 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001536 &module.Module.protoProperties,
1537 &module.testProperties)
1538
Colin Cross9ae1b922018-06-26 17:59:05 -07001539 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001540 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001541
Colin Cross05638fc2018-04-09 18:40:24 -07001542 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001543 return module
1544}
1545
1546func TestHostFactory() android.Module {
1547 module := &Test{}
1548
1549 module.AddProperties(
1550 &module.Module.properties,
1551 &module.Module.protoProperties,
1552 &module.testProperties)
1553
Colin Cross9ae1b922018-06-26 17:59:05 -07001554 module.Module.properties.Installable = proptools.BoolPtr(true)
1555
Colin Cross05638fc2018-04-09 18:40:24 -07001556 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001557 return module
1558}
1559
1560//
Colin Cross2fe66872015-03-30 17:20:39 -07001561// Java Binaries (.jar file plus wrapper script)
1562//
1563
Colin Crossf506d872017-07-19 15:53:04 -07001564type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001565 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001566 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001567
1568 // Name of the class containing main to be inserted into the manifest as Main-Class.
1569 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001570}
1571
Colin Crossf506d872017-07-19 15:53:04 -07001572type Binary struct {
1573 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001574
Colin Crossf506d872017-07-19 15:53:04 -07001575 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001576
Colin Cross6b4a32d2017-12-05 13:42:45 -08001577 isWrapperVariant bool
1578
Colin Crossc3315992017-12-08 19:12:36 -08001579 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001580 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001581}
1582
Alex Light24237172017-10-26 09:46:21 -07001583func (j *Binary) HostToolPath() android.OptionalPath {
1584 return android.OptionalPathForPath(j.binaryFile)
1585}
1586
Colin Crossf506d872017-07-19 15:53:04 -07001587func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001588 if ctx.Arch().ArchType == android.Common {
1589 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001590 if j.binaryProperties.Main_class != nil {
1591 if j.properties.Manifest != nil {
1592 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1593 }
1594 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1595 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1596 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1597 }
1598
Colin Cross6b4a32d2017-12-05 13:42:45 -08001599 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001600 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001601 // Handle the binary wrapper
1602 j.isWrapperVariant = true
1603
Colin Cross366938f2017-12-11 16:29:02 -08001604 if j.binaryProperties.Wrapper != nil {
1605 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001606 } else {
1607 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1608 }
1609
1610 // Depend on the installed jar so that the wrapper doesn't get executed by
1611 // another build rule before the jar has been installed.
1612 jarFile := ctx.PrimaryModule().(*Binary).installFile
1613
1614 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1615 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001616 }
Colin Cross2fe66872015-03-30 17:20:39 -07001617}
1618
Colin Crossf506d872017-07-19 15:53:04 -07001619func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001620 if ctx.Arch().ArchType == android.Common {
1621 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001622 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001623 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001624 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001625}
1626
Colin Crossf506d872017-07-19 15:53:04 -07001627func BinaryFactory() android.Module {
1628 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001629
Colin Cross36242852017-06-23 15:06:31 -07001630 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001631 &module.Module.properties,
1632 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001633 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001634 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001635 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001636
Colin Cross9ae1b922018-06-26 17:59:05 -07001637 module.Module.properties.Installable = proptools.BoolPtr(true)
1638
Colin Cross6b4a32d2017-12-05 13:42:45 -08001639 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1640 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001641 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001642}
1643
Colin Crossf506d872017-07-19 15:53:04 -07001644func BinaryHostFactory() android.Module {
1645 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001646
Colin Cross36242852017-06-23 15:06:31 -07001647 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001648 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001649 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001650 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001651
Colin Cross9ae1b922018-06-26 17:59:05 -07001652 module.Module.properties.Installable = proptools.BoolPtr(true)
1653
Colin Cross6b4a32d2017-12-05 13:42:45 -08001654 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1655 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001656 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001657}
1658
1659//
1660// Java prebuilts
1661//
1662
Colin Cross74d73e22017-08-02 11:05:49 -07001663type ImportProperties struct {
1664 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001665
Nan Zhangea568a42017-11-08 21:20:04 -08001666 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001667
1668 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001669
1670 // List of shared java libs that this module has dependencies to
1671 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001672
1673 // List of files to remove from the jar file(s)
1674 Exclude_files []string
1675
1676 // List of directories to remove from the jar file(s)
1677 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001678
1679 // if set to true, run Jetifier against .jar file. Defaults to false.
1680 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001681}
1682
1683type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001684 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001685 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001686 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001687
Colin Cross74d73e22017-08-02 11:05:49 -07001688 properties ImportProperties
1689
Colin Cross0a6e0072017-08-30 14:24:55 -07001690 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001691 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001692}
1693
Colin Cross83bb3162018-06-25 15:48:06 -07001694func (j *Import) sdkVersion() string {
1695 return String(j.properties.Sdk_version)
1696}
1697
1698func (j *Import) minSdkVersion() string {
1699 return j.sdkVersion()
1700}
1701
Colin Cross74d73e22017-08-02 11:05:49 -07001702func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001703 return &j.prebuilt
1704}
1705
Colin Cross74d73e22017-08-02 11:05:49 -07001706func (j *Import) PrebuiltSrcs() []string {
1707 return j.properties.Jars
1708}
1709
1710func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001711 return j.prebuilt.Name(j.ModuleBase.Name())
1712}
1713
Colin Cross74d73e22017-08-02 11:05:49 -07001714func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001715 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001716 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001717}
1718
Colin Cross74d73e22017-08-02 11:05:49 -07001719func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001720 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001721
Nan Zhang4c819fb2018-08-27 18:31:46 -07001722 jarName := ctx.ModuleName() + ".jar"
1723 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001724 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1725 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001726 if Bool(j.properties.Jetifier_enabled) {
1727 inputFile := outputFile
1728 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1729 TransformJetifier(ctx, outputFile, inputFile)
1730 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001731 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001732
1733 ctx.VisitDirectDeps(func(module android.Module) {
1734 otherName := ctx.OtherModuleName(module)
1735 tag := ctx.OtherModuleDependencyTag(module)
1736
1737 switch dep := module.(type) {
1738 case Dependency:
1739 switch tag {
1740 case libTag, staticLibTag:
1741 // sdk lib names from dependencies are re-exported
1742 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1743 }
1744 case SdkLibraryDependency:
1745 switch tag {
1746 case libTag:
1747 // names of sdk libs that are directly depended are exported
1748 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1749 }
1750 }
1751 })
1752
1753 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001754 if Bool(j.properties.Installable) {
1755 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1756 ctx.ModuleName()+".jar", outputFile)
1757 }
Colin Cross2fe66872015-03-30 17:20:39 -07001758}
1759
Colin Cross74d73e22017-08-02 11:05:49 -07001760var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001761
Nan Zhanged19fc32017-10-19 13:06:22 -07001762func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001763 if j.combinedClasspathFile == nil {
1764 return nil
1765 }
Colin Cross37f6d792018-07-12 12:28:41 -07001766 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001767}
1768
1769func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001770 if j.combinedClasspathFile == nil {
1771 return nil
1772 }
Colin Cross37f6d792018-07-12 12:28:41 -07001773 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001774}
1775
Colin Cross331a1212018-08-15 20:40:52 -07001776func (j *Import) ResourceJars() android.Paths {
1777 return nil
1778}
1779
1780func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001781 if j.combinedClasspathFile == nil {
1782 return nil
1783 }
Colin Cross331a1212018-08-15 20:40:52 -07001784 return android.Paths{j.combinedClasspathFile}
1785}
1786
Colin Cross74d73e22017-08-02 11:05:49 -07001787func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001788 return nil
1789}
1790
Jiyong Park1be96912018-05-28 18:02:19 +09001791func (j *Import) ExportedSdkLibs() []string {
1792 return j.exportedSdkLibs
1793}
1794
albaltai36ff7dc2018-12-25 14:35:23 +08001795// Add compile time check for interface implementation
1796var _ android.IDEInfo = (*Import)(nil)
1797var _ android.IDECustomizedModuleName = (*Import)(nil)
1798
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001799// Collect information for opening IDE project files in java/jdeps.go.
1800const (
1801 removedPrefix = "prebuilt_"
1802)
1803
1804func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1805 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1806}
1807
1808func (j *Import) IDECustomizedModuleName() string {
1809 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1810 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1811 // solution to get the Import name.
1812 name := j.Name()
1813 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001814 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001815 }
1816 return name
1817}
1818
Colin Cross74d73e22017-08-02 11:05:49 -07001819var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001820
Colin Cross74d73e22017-08-02 11:05:49 -07001821func ImportFactory() android.Module {
1822 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001823
Colin Cross74d73e22017-08-02 11:05:49 -07001824 module.AddProperties(&module.properties)
1825
1826 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001827 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001828 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001829}
1830
Colin Cross74d73e22017-08-02 11:05:49 -07001831func ImportFactoryHost() android.Module {
1832 module := &Import{}
1833
1834 module.AddProperties(&module.properties)
1835
1836 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001837 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001838 return module
1839}
1840
Colin Cross89536d42017-07-07 14:35:50 -07001841//
1842// Defaults
1843//
1844type Defaults struct {
1845 android.ModuleBase
1846 android.DefaultsModuleBase
1847}
1848
1849func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1850}
1851
1852func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1853}
1854
1855func defaultsFactory() android.Module {
1856 return DefaultsFactory()
1857}
1858
1859func DefaultsFactory(props ...interface{}) android.Module {
1860 module := &Defaults{}
1861
1862 module.AddProperties(props...)
1863 module.AddProperties(
1864 &CompilerProperties{},
1865 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001866 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001867 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001868 &aaptProperties{},
1869 &androidLibraryProperties{},
1870 &appProperties{},
1871 &appTestProperties{},
1872 &ImportProperties{},
1873 &AARImportProperties{},
1874 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001875 )
1876
1877 android.InitDefaultsModule(module)
1878
1879 return module
1880}
Nan Zhangea568a42017-11-08 21:20:04 -08001881
1882var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001883var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001884var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001885var inList = android.InList