blob: 0e1ae23909f6eedf7744d04c66999a99d7ecdc92 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
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 Cross3d7c9822019-03-01 13:46:24 -080047 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
48 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070049
Colin Cross798bfce2016-10-12 14:28:16 -070050 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070051}
52
Colin Cross2fe66872015-03-30 17:20:39 -070053// TODO:
54// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070055// Renderscript
56// Post-jar passes:
57// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070058// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070059// DroidDoc
60// Findbugs
61
Colin Cross89536d42017-07-07 14:35:50 -070062type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070063 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
64 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070065 Srcs []string `android:"arch_variant"`
66
67 // list of source files that should not be used to build the Java module.
68 // This is most useful in the arch/multilib variants to remove non-common files
69 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070070
71 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070072 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070073
Colin Cross86a63ff2017-09-27 17:33:10 -070074 // list of directories that should be excluded from java_resource_dirs
75 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070076
Colin Cross0f37af02017-09-27 17:42:05 -070077 // list of files to use as Java resources
78 Java_resources []string `android:"arch_variant"`
79
Colin Crosscedd4762018-09-13 11:26:19 -070080 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross0f37af02017-09-27 17:42:05 -070081 Exclude_java_resources []string `android:"arch_variant"`
82
Paul Duffin2fbbfb82019-02-13 12:49:33 +000083 // don't build against the default libraries (bootclasspath, ext, and framework for device
84 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070085 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070086
Paul Duffin2fbbfb82019-02-13 12:49:33 +000087 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070088 No_framework_libs *bool
89
Colin Cross7d5136f2015-05-11 13:39:40 -070090 // list of module-specific flags that will be used for javac compiles
91 Javacflags []string `android:"arch_variant"`
92
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020093 // list of module-specific flags that will be used for kotlinc compiles
94 Kotlincflags []string `android:"arch_variant"`
95
Colin Cross7d5136f2015-05-11 13:39:40 -070096 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070097 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070098
99 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700100 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700101
102 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700103 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700104
Colin Cross540eff82017-06-22 17:01:52 -0700105 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700106 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700107
108 // If not blank, set the java version passed to javac as -source and -target
109 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700110
Colin Cross9ae1b922018-06-26 17:59:05 -0700111 // If set to true, allow this module to be dexed and installed on devices. Has no
112 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700113 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700114
Colin Cross0f37af02017-09-27 17:42:05 -0700115 // If set to true, include sources used to compile the module in to the final jar
116 Include_srcs *bool
117
Colin Crossbe9cdb82019-01-21 21:37:16 -0800118 // List of modules to use as annotation processors
119 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700120
Nan Zhang61eaedb2017-11-02 13:28:15 -0700121 // The number of Java source entries each Javac instance can process
122 Javac_shard_size *int64
123
Nan Zhang5f8cb422018-02-06 10:34:32 -0800124 // Add host jdk tools.jar to bootclasspath
125 Use_tools_jar *bool
126
Colin Cross1369cdb2017-09-29 17:58:17 -0700127 Openjdk9 struct {
128 // List of source files that should only be used when passing -source 1.9
129 Srcs []string
130
131 // List of javac flags that should only be used when passing -source 1.9
132 Javacflags []string
133 }
Colin Crosscb933592017-11-22 13:49:43 -0800134
Colin Cross81440082018-08-15 20:21:55 -0700135 // When compiling language level 9+ .java code in packages that are part of
136 // a system module, patch_module names the module that your sources and
137 // dependencies should be patched into. The Android runtime currently
138 // doesn't implement the JEP 261 module system so this option is only
139 // supported at compile time. It should only be needed to compile tests in
140 // packages that exist in libcore and which are inconvenient to move
141 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100142 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700143
Colin Crosscb933592017-11-22 13:49:43 -0800144 Jacoco struct {
145 // List of classes to include for instrumentation with jacoco to collect coverage
146 // information at runtime when building with coverage enabled. If unset defaults to all
147 // classes.
148 // Supports '*' as the last character of an entry in the list as a wildcard match.
149 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
150 // it matches classes in the package that have the class name as a prefix.
151 Include_filter []string
152
153 // List of classes to exclude from instrumentation with jacoco to collect coverage
154 // information at runtime when building with coverage enabled. Overrides classes selected
155 // by the include_filter property.
156 // Supports '*' as the last character of an entry in the list as a wildcard match.
157 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
158 // it matches classes in the package that have the class name as a prefix.
159 Exclude_filter []string
160 }
161
Andreas Gampef3e5b552018-01-22 21:27:21 -0800162 Errorprone struct {
163 // List of javac flags that should only be used when running errorprone.
164 Javacflags []string
165 }
166
Colin Cross0f2ee152017-12-14 15:22:43 -0800167 Proto struct {
168 // List of extra options that will be passed to the proto generator.
169 Output_params []string
170 }
171
Colin Crosscb933592017-11-22 13:49:43 -0800172 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800173
174 // List of files to include in the META-INF/services folder of the resulting jar.
175 Services []string `android:"arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700176}
177
Colin Cross89536d42017-07-07 14:35:50 -0700178type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700179 // list of module-specific flags that will be used for dex compiles
180 Dxflags []string `android:"arch_variant"`
181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
183 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800184 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700185
Colin Cross83bb3162018-06-25 15:48:06 -0700186 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
187 // Defaults to sdk_version if not set.
188 Min_sdk_version *string
189
Dan Willemsen419290a2018-10-31 15:28:47 -0700190 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
191 // Defaults to sdk_version if not set.
192 Target_sdk_version *string
193
Colin Cross6af2e492018-05-22 11:12:33 -0700194 // if true, compile against the platform APIs instead of an SDK.
195 Platform_apis *bool
196
Colin Crossebe1a512017-11-14 13:12:14 -0800197 Aidl struct {
198 // Top level directories to pass to aidl tool
199 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700200
Colin Crossebe1a512017-11-14 13:12:14 -0800201 // Directories rooted at the Android.bp file to pass to aidl tool
202 Local_include_dirs []string
203
204 // directories that should be added as include directories for any aidl sources of modules
205 // that depend on this module, as well as to aidl for this module.
206 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100207
208 // whether to generate traces (for systrace) for this interface
209 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100210
211 // whether to generate Binder#GetTransaction name method.
212 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800213 }
Colin Cross92430102017-10-09 14:59:32 -0700214
215 // If true, export a copy of the module as a -hostdex module for host testing.
216 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700217
David Brazdil17ef5632018-06-27 10:27:45 +0100218 // If set to true, compile dex regardless of installable. Defaults to false.
219 Compile_dex *bool
220
Colin Cross66dbc0b2017-12-28 12:23:20 -0800221 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700222 // If false, disable all optimization. Defaults to true for android_app and android_test
223 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800224 Enabled *bool
225
226 // If true, optimize for size by removing unused code. Defaults to true for apps,
227 // false for libraries and tests.
228 Shrink *bool
229
230 // If true, optimize bytecode. Defaults to false.
231 Optimize *bool
232
233 // If true, obfuscate bytecode. Defaults to false.
234 Obfuscate *bool
235
236 // If true, do not use the flag files generated by aapt that automatically keep
237 // classes referenced by the app manifest. Defaults to false.
238 No_aapt_flags *bool
239
240 // Flags to pass to proguard.
241 Proguard_flags []string
242
243 // Specifies the locations of files containing proguard flags.
244 Proguard_flags_files []string
245 }
246
Colin Cross1369cdb2017-09-29 17:58:17 -0700247 // When targeting 1.9, override the modules to use with --system
248 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700249
250 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800251 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700252}
253
Colin Cross46c9b8b2017-06-22 16:51:17 -0700254// Module contains the properties and members used by all java module types
255type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700256 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700257 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700258
Colin Cross89536d42017-07-07 14:35:50 -0700259 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700260 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700261 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700262
Colin Cross331a1212018-08-15 20:40:52 -0700263 // jar file containing header classes including static library dependencies, suitable for
264 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700265 headerJarFile android.Path
266
Colin Cross331a1212018-08-15 20:40:52 -0700267 // jar file containing implementation classes including static library dependencies but no
268 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700269 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700270
Colin Cross331a1212018-08-15 20:40:52 -0700271 // jar file containing only resources including from static library dependencies
272 resourceJar android.Path
273
274 // jar file containing implementation classes and resources including static library
275 // dependencies
276 implementationAndResourcesJar android.Path
277
278 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700279 dexJarFile android.Path
280
Colin Cross43f08db2018-11-12 10:13:39 -0800281 // output file that contains classes.dex if it should be in the output file
282 maybeStrippedDexJarFile android.Path
283
Colin Crosscb933592017-11-22 13:49:43 -0800284 // output file containing uninstrumented classes that will be instrumented by jacoco
285 jacocoReportClassesFile android.Path
286
Colin Cross66dbc0b2017-12-28 12:23:20 -0800287 // output file containing mapping of obfuscated names
288 proguardDictionary android.Path
289
Colin Cross331a1212018-08-15 20:40:52 -0700290 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700291 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700292
Colin Cross635c3b02016-05-18 15:37:25 -0700293 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700294
Colin Cross635c3b02016-05-18 15:37:25 -0700295 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700296
Colin Cross2fe66872015-03-30 17:20:39 -0700297 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700298 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800299
300 // list of .java files and srcjars that was passed to javac
301 compiledJavaSrcs android.Paths
302 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303
304 // list of extra progurad flag files
305 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900306
Colin Cross094054a2018-10-17 15:10:48 -0700307 // manifest file to use instead of properties.Manifest
308 overrideManifest android.OptionalPath
309
Jiyong Park1be96912018-05-28 18:02:19 +0900310 // list of SDK lib names that this java moudule is exporting
311 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700312
313 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
314 // filter out Exclude_srcs, will be used by android.IDEInfo struct
315 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800316
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800317 // expanded Jarjar_rules
318 expandJarjarRules android.Path
319
Colin Crossf24a22a2019-01-31 14:12:44 -0800320 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800321 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700322}
323
Colin Cross54250902017-12-05 09:28:08 -0800324func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700325 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800326}
327
Jiyong Park8fd61922018-11-08 02:50:25 +0900328func (j *Module) DexJarFile() android.Path {
329 return j.dexJarFile
330}
331
Colin Cross54250902017-12-05 09:28:08 -0800332var _ android.SourceFileProducer = (*Module)(nil)
333
Colin Crossf506d872017-07-19 15:53:04 -0700334type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700335 HeaderJars() android.Paths
336 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700337 ResourceJars() android.Paths
338 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800339 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700340 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900341 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700342}
343
Jiyong Parkc678ad32018-04-10 13:07:10 +0900344type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800345 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
346 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900347}
348
Nan Zhangb2b33de2018-02-23 11:18:47 -0800349type SrcDependency interface {
350 CompiledSrcs() android.Paths
351 CompiledSrcJars() android.Paths
352}
353
354func (j *Module) CompiledSrcs() android.Paths {
355 return j.compiledJavaSrcs
356}
357
358func (j *Module) CompiledSrcJars() android.Paths {
359 return j.compiledSrcJars
360}
361
362var _ SrcDependency = (*Module)(nil)
363
Colin Cross89536d42017-07-07 14:35:50 -0700364func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
365 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
366 android.InitDefaultableModule(module)
367}
368
Colin Crossbe1da472017-07-07 15:59:46 -0700369type dependencyTag struct {
370 blueprint.BaseDependencyTag
371 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700372}
373
Colin Crossa4f08812018-10-02 22:03:40 -0700374type jniDependencyTag struct {
375 blueprint.BaseDependencyTag
376 target android.Target
377}
378
Colin Crossbe1da472017-07-07 15:59:46 -0700379var (
Colin Cross4b964c02018-10-15 16:18:06 -0700380 staticLibTag = dependencyTag{name: "staticlib"}
381 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800382 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700383 bootClasspathTag = dependencyTag{name: "bootclasspath"}
384 systemModulesTag = dependencyTag{name: "system modules"}
385 frameworkResTag = dependencyTag{name: "framework-res"}
386 frameworkApkTag = dependencyTag{name: "framework-apk"}
387 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800388 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700389 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
390 certificateTag = dependencyTag{name: "certificate"}
391 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700392)
Colin Cross2fe66872015-03-30 17:20:39 -0700393
Colin Crossfc3674a2017-09-18 17:41:52 -0700394type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700395 useModule, useFiles, useDefaultLibs, invalidVersion bool
396
Colin Cross86a60ae2018-05-29 14:44:55 -0700397 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700398 systemModules string
399
Colin Crossa97c5d32018-03-28 14:58:31 -0700400 frameworkResModule string
401
Colin Cross86a60ae2018-05-29 14:44:55 -0700402 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700403 aidl android.Path
404}
405
Colin Crossa4f08812018-10-02 22:03:40 -0700406type jniLib struct {
407 name string
408 path android.Path
409 target android.Target
410}
411
Colin Cross3144dfc2018-01-03 15:06:47 -0800412func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
413 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
414}
415
416func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
417 return j.shouldInstrument(ctx) &&
418 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
419 ctx.Config().UnbundledBuild())
420}
421
Colin Cross83bb3162018-06-25 15:48:06 -0700422func (j *Module) sdkVersion() string {
423 return String(j.deviceProperties.Sdk_version)
424}
425
426func (j *Module) minSdkVersion() string {
427 if j.deviceProperties.Min_sdk_version != nil {
428 return *j.deviceProperties.Min_sdk_version
429 }
430 return j.sdkVersion()
431}
432
Dan Willemsen419290a2018-10-31 15:28:47 -0700433func (j *Module) targetSdkVersion() string {
434 if j.deviceProperties.Target_sdk_version != nil {
435 return *j.deviceProperties.Target_sdk_version
436 }
437 return j.sdkVersion()
438}
439
Colin Crossbe1da472017-07-07 15:59:46 -0700440func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700441 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700442 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700443 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700444 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700445 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100446 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700447 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700448 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700449 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700450 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100451 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700452 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800453 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700454 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
455 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800456 }
Colin Crossbe1da472017-07-07 15:59:46 -0700457 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700458 } else if j.deviceProperties.System_modules == nil {
459 ctx.PropertyErrorf("no_standard_libs",
460 "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 +0100461 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700462 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700463 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100464 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700465 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800466 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800467 if ctx.ModuleName() == "android_stubs_current" ||
468 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700469 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700470 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800471 }
Colin Cross2fe66872015-03-30 17:20:39 -0700472 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700473
Colin Cross42d48b72018-08-29 14:10:52 -0700474 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
475 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700476
Colin Crossbe9cdb82019-01-21 21:37:16 -0800477 ctx.AddFarVariationDependencies([]blueprint.Variation{
478 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
479 }, pluginTag, j.properties.Plugins...)
480
Colin Cross7f9036c2017-08-30 13:27:57 -0700481 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000482 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700483 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800484 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800485 android.ExtractSourceDeps(ctx, j.properties.Jarjar_rules)
Alex Light7f004a72019-02-21 13:27:37 -0800486 android.ExtractSourcesDeps(ctx, j.properties.Services)
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 Cross7788c122019-01-23 16:14:02 -0800496 if 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 Moreland36b130f2019-02-05 17:02:55 -0800541 flags := []string{}
Steven Moreland667f6882018-07-26 12:55:08 -0700542
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) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800706 case SdkLibraryDependency:
707 switch tag {
708 case libTag:
709 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
710 // names of sdk libs that are directly depended are exported
711 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
712 default:
713 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
714 }
Colin Cross54250902017-12-05 09:28:08 -0800715 case Dependency:
716 switch tag {
717 case bootClasspathTag:
718 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700719 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800720 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900721 // sdk lib names from dependencies are re-exported
722 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800723 case staticLibTag:
724 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
725 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
726 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700727 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900728 // sdk lib names from dependencies are re-exported
729 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800730 case pluginTag:
731 if plugin, ok := dep.(*Plugin); ok {
732 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
733 if plugin.pluginProperties.Processor_class != nil {
734 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
735 }
736 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
737 } else {
738 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
739 }
Colin Cross54250902017-12-05 09:28:08 -0800740 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100741 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800742 // framework.jar has a one-off dependency on the R.java and Manifest.java files
743 // generated by framework-res.apk
744 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
745 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800746 case frameworkApkTag:
747 if ctx.ModuleName() == "android_stubs_current" ||
748 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700749 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800750 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
751 // resource files out of there for aapt.
752 //
753 // Normally the package rule runs aapt, which includes the resource,
754 // but we're not running that in our package rule so just copy in the
755 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700756 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800757 }
Colin Cross54250902017-12-05 09:28:08 -0800758 case kotlinStdlibTag:
759 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800760 case kotlinAnnotationsTag:
761 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800762 }
763
764 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
765 case android.SourceFileProducer:
766 switch tag {
767 case libTag:
768 checkProducesJars(ctx, dep)
769 deps.classpath = append(deps.classpath, dep.Srcs()...)
770 case staticLibTag:
771 checkProducesJars(ctx, dep)
772 deps.classpath = append(deps.classpath, dep.Srcs()...)
773 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
774 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
775 case android.DefaultsDepTag, android.SourceDepTag:
776 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900777 case publicApiFileTag, systemApiFileTag, testApiFileTag:
778 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800779 default:
780 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
781 }
782 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700783 switch tag {
784 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700785 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700786 case systemModulesTag:
787 if deps.systemModules != nil {
788 panic("Found two system module dependencies")
789 }
790 sm := module.(*SystemModules)
791 if sm.outputFile == nil {
792 panic("Missing directory for system module dependency")
793 }
794 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700795 default:
796 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700797 }
Colin Crossec7a0422017-07-07 14:47:12 -0700798 }
Colin Cross2fe66872015-03-30 17:20:39 -0700799 })
800
Jiyong Park1be96912018-05-28 18:02:19 +0900801 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
802
Colin Cross32f676a2017-09-06 13:41:06 -0700803 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700804}
805
Colin Cross83bb3162018-06-25 15:48:06 -0700806func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700807 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800808 v := sdkContext.sdkVersion()
809 // For PDK builds, use the latest SDK version instead of "current"
810 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
811 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
812 latestSdkVersion := 0
813 if len(sdkVersions) > 0 {
814 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
815 }
816 v = strconv.Itoa(latestSdkVersion)
817 }
818
819 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700820 if err != nil {
821 ctx.PropertyErrorf("sdk_version", "%s", err)
822 }
Nan Zhang357466b2018-04-17 17:38:36 -0700823 if javaVersion != "" {
824 ret = javaVersion
825 } else if ctx.Device() && sdk <= 23 {
826 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900827 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700828 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700829 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700830 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
831 ret = "1.8"
832 } else {
833 ret = "1.9"
834 }
835
836 return ret
837}
838
Nan Zhanged19fc32017-10-19 13:06:22 -0700839func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700840
Colin Crossf03c82b2015-04-13 13:53:40 -0700841 var flags javaBuilderFlags
842
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100843 // javaVersion flag.
844 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
845
Nan Zhanged19fc32017-10-19 13:06:22 -0700846 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700847 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100848 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700849 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700850 }
Colin Cross6510f912017-11-29 00:27:14 -0800851 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700852 // Override the -g flag passed globally to remove local variable debug info to reduce
853 // disk and memory usage.
854 javacFlags = append(javacFlags, "-g:source,lines")
855 }
Colin Cross64162712017-08-08 13:17:59 -0700856
Colin Cross66548102018-06-19 22:47:35 -0700857 if ctx.Config().RunErrorProne() {
858 if config.ErrorProneClasspath == nil {
859 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
860 }
861
862 errorProneFlags := []string{
863 "-Xplugin:ErrorProne",
864 "${config.ErrorProneChecks}",
865 }
866 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
867
868 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
869 "'" + strings.Join(errorProneFlags, " ") + "'"
870 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800871 }
872
Nan Zhanged19fc32017-10-19 13:06:22 -0700873 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800874 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
875 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700876 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800877
Colin Crossbe9cdb82019-01-21 21:37:16 -0800878 flags.processor = strings.Join(deps.processorClasses, ",")
879
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100880 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800881 !Bool(j.properties.No_standard_libs) &&
882 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
883 // Give host-side tools a version of OpenJDK's standard libraries
884 // close to what they're targeting. As of Dec 2017, AOSP is only
885 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
886 //
887 // When building with OpenJDK 8, the following should have no
888 // effect since those jars would be available by default.
889 //
890 // When building with OpenJDK 9 but targeting a version < 1.8,
891 // putting them on the bootclasspath means that:
892 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
893 // b) references to existing APIs are not reinterpreted in an
894 // OpenJDK 9-specific way, eg. calls to subclasses of
895 // java.nio.Buffer as in http://b/70862583
896 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
897 flags.bootClasspath = append(flags.bootClasspath,
898 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
899 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800900 if Bool(j.properties.Use_tools_jar) {
901 flags.bootClasspath = append(flags.bootClasspath,
902 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
903 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800904 }
905
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100906 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800907 // Manually specify build directory in case it is not under the repo root.
908 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
909 // just adding a symlink under the root doesn't help.)
910 patchPaths := ".:" + ctx.Config().BuildDir()
911 classPath := flags.classpath.FormJavaClassPath("")
912 if classPath != "" {
913 patchPaths += ":" + classPath
914 }
915 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700916 }
917
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700919 if deps.systemModules != nil {
920 flags.systemModules = append(flags.systemModules, deps.systemModules)
921 }
922
Nan Zhanged19fc32017-10-19 13:06:22 -0700923 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700924 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700925 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700926 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700927 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
928 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700929 }
930
Colin Cross81440082018-08-15 20:21:55 -0700931 if len(javacFlags) > 0 {
932 // optimization.
933 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
934 flags.javacFlags = "$javacFlags"
935 }
936
Nan Zhanged19fc32017-10-19 13:06:22 -0700937 return flags
938}
Colin Crossc0b06f12015-04-08 13:03:43 -0700939
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800940func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700941
Colin Crossebe1a512017-11-14 13:12:14 -0800942 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700943
944 deps := j.collectDeps(ctx)
945 flags := j.collectBuilderFlags(ctx, deps)
946
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100947 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700948 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
949 }
950 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700951 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800952 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700953 }
954
Colin Crossaf050172017-11-15 23:01:59 -0800955 srcFiles = j.genSources(ctx, srcFiles, flags)
956
957 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700958 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800959 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700960
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700961 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
962 // that IDEInfo struct will use
963 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
964
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800965 if j.properties.Jarjar_rules != nil {
966 j.expandJarjarRules = ctx.ExpandSource(*j.properties.Jarjar_rules, "jarjar_rules")
967 }
968
Colin Cross1ee23172017-10-18 14:44:18 -0700969 jarName := ctx.ModuleName() + ".jar"
970
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000971 javaSrcFiles := srcFiles.FilterByExt(".java")
972 var uniqueSrcFiles android.Paths
973 set := make(map[string]bool)
974 for _, v := range javaSrcFiles {
975 if _, found := set[v.String()]; !found {
976 set[v.String()] = true
977 uniqueSrcFiles = append(uniqueSrcFiles, v)
978 }
979 }
980
Colin Cross55f63ea2018-08-27 12:37:09 -0700981 var kotlinJars android.Paths
982
Colin Cross93e85952017-08-15 13:34:18 -0700983 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200984 // user defined kotlin flags.
985 kotlincFlags := j.properties.Kotlincflags
986 CheckKotlincFlags(ctx, kotlincFlags)
987
Colin Cross93e85952017-08-15 13:34:18 -0700988 // If there are kotlin files, compile them first but pass all the kotlin and java files
989 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
990 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200991 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700992 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200993 kotlincFlags = append(kotlincFlags, "-no-jdk")
994 }
995 if len(kotlincFlags) > 0 {
996 // optimization.
997 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
998 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700999 }
1000
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001001 var kotlinSrcFiles android.Paths
1002 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1003 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1004
Colin Crossafbb1732019-01-17 15:42:52 -08001005 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1006 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1007
1008 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1009 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1010
1011 if len(flags.processorPath) > 0 {
1012 // Use kapt for annotation processing
1013 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1014 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1015 srcJars = append(srcJars, kaptSrcJar)
1016 // Disable annotation processing in javac, it's already been handled by kapt
1017 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001018 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001019 }
Colin Cross93e85952017-08-15 13:34:18 -07001020
Colin Cross1ee23172017-10-18 14:44:18 -07001021 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001022 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001023 if ctx.Failed() {
1024 return
1025 }
1026
1027 // Make javac rule depend on the kotlinc rule
1028 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001029
Colin Cross93e85952017-08-15 13:34:18 -07001030 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001031 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001032 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001033 }
1034
Colin Cross55f63ea2018-08-27 12:37:09 -07001035 jars := append(android.Paths(nil), kotlinJars...)
1036
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001037 // Store the list of .java files that was passed to javac
1038 j.compiledJavaSrcs = uniqueSrcFiles
1039 j.compiledSrcJars = srcJars
1040
Nan Zhang61eaedb2017-11-02 13:28:15 -07001041 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001042 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001043 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1044 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001045 // Formerly, there was a check here that prevented annotation processors
1046 // from being used when sharding was enabled, as some annotation processors
1047 // do not function correctly in sharded environments. It was removed to
1048 // allow for the use of annotation processors that do function correctly
1049 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001050 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001051 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001052 if ctx.Failed() {
1053 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001054 }
1055 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001056 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001057 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001058 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001059 // If error-prone is enabled, add an additional rule to compile the java files into
1060 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001061 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001062 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1063 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001064 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001065 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001066 extraJarDeps = append(extraJarDeps, errorprone)
1067 }
1068
Nan Zhang61eaedb2017-11-02 13:28:15 -07001069 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001070 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001071 shardSize := int(*(j.properties.Javac_shard_size))
1072 var shardSrcs []android.Paths
1073 if len(uniqueSrcFiles) > 0 {
1074 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1075 for idx, shardSrc := range shardSrcs {
1076 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1077 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1078 jars = append(jars, classes)
1079 }
1080 }
1081 if len(srcJars) > 0 {
1082 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1083 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1084 jars = append(jars, classes)
1085 }
1086 } else {
1087 classes := android.PathForModuleOut(ctx, "javac", jarName)
1088 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1089 jars = append(jars, classes)
1090 }
Colin Crossd6891432017-09-27 17:39:56 -07001091 if ctx.Failed() {
1092 return
1093 }
Colin Cross2fe66872015-03-30 17:20:39 -07001094 }
1095
Colin Crosscedd4762018-09-13 11:26:19 -07001096 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1097 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001098 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1099
1100 var resArgs []string
1101 var resDeps android.Paths
1102
1103 resArgs = append(resArgs, dirArgs...)
1104 resDeps = append(resDeps, dirDeps...)
1105
1106 resArgs = append(resArgs, fileArgs...)
1107 resDeps = append(resDeps, fileDeps...)
1108
Colin Crossff3ae9d2018-04-10 16:15:18 -07001109 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001110 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001111 resArgs = append(resArgs, srcArgs...)
1112 resDeps = append(resDeps, srcDeps...)
1113 }
Colin Cross40a36712017-09-27 17:41:35 -07001114
1115 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001116 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001117 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001118 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001119 if ctx.Failed() {
1120 return
1121 }
1122 }
1123
Colin Cross331a1212018-08-15 20:40:52 -07001124 if len(deps.staticResourceJars) > 0 {
1125 var jars android.Paths
1126 if j.resourceJar != nil {
1127 jars = append(jars, j.resourceJar)
1128 }
1129 jars = append(jars, deps.staticResourceJars...)
1130
1131 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1132 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1133 false, nil, nil)
1134 j.resourceJar = combinedJar
1135 }
1136
Colin Cross32f676a2017-09-06 13:41:06 -07001137 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001138
Colin Cross094054a2018-10-17 15:10:48 -07001139 manifest := j.overrideManifest
1140 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001141 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1142 }
Colin Cross635acc92017-09-12 22:50:46 -07001143
Alex Light7f004a72019-02-21 13:27:37 -08001144 services := ctx.ExpandSources(j.properties.Services, nil)
1145 if len(services) > 0 {
1146 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1147 var zipargs []string
1148 for _, file := range services {
1149 serviceFile := file.String()
1150 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1151 }
1152 ctx.Build(pctx, android.BuildParams{
1153 Rule: zip,
1154 Output: servicesJar,
1155 Implicits: services,
1156 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001157 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001158 },
1159 })
1160 jars = append(jars, servicesJar)
1161 }
1162
Colin Cross0a6e0072017-08-30 14:24:55 -07001163 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001164 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001165 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001166
1167 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001168 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1169 // Optimization: skip the combine step if there is nothing to do
1170 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1171 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1172 // any if len(jars) == 1.
1173 outputFile = moduleOutPath
1174 } else {
1175 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1176 ctx.Build(pctx, android.BuildParams{
1177 Rule: android.Cp,
1178 Input: jars[0],
1179 Output: combinedJar,
1180 })
1181 outputFile = combinedJar
1182 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001183 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001184 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001185 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001186 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001187 outputFile = combinedJar
1188 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001189
Colin Cross331a1212018-08-15 20:40:52 -07001190 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001191 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001192 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001193 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001194 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001195 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001196
1197 // jarjar resource jar if necessary
1198 if j.resourceJar != nil {
1199 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001200 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001201 j.resourceJar = resourceJarJarFile
1202 }
1203
Colin Cross0a6e0072017-08-30 14:24:55 -07001204 if ctx.Failed() {
1205 return
1206 }
1207 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001208 j.implementationJarFile = outputFile
1209 if j.headerJarFile == nil {
1210 j.headerJarFile = j.implementationJarFile
1211 }
Colin Cross2fe66872015-03-30 17:20:39 -07001212
Colin Cross6510f912017-11-29 00:27:14 -08001213 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001214 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1215 j.properties.Instrument = true
1216 }
1217 }
1218
Colin Cross3144dfc2018-01-03 15:06:47 -08001219 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001220 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1221 }
1222
Colin Cross331a1212018-08-15 20:40:52 -07001223 // merge implementation jar with resources if necessary
1224 implementationAndResourcesJar := outputFile
1225 if j.resourceJar != nil {
1226 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1227 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1228 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1229 false, nil, nil)
1230 implementationAndResourcesJar = combinedJar
1231 }
1232
1233 j.implementationAndResourcesJar = implementationAndResourcesJar
1234
Colin Cross9ae1b922018-06-26 17:59:05 -07001235 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001236 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001237 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001238 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001239 if ctx.Failed() {
1240 return
1241 }
Colin Cross331a1212018-08-15 20:40:52 -07001242
Colin Cross8faf8fc2019-01-16 15:15:52 -08001243 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001244 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1245 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001246
Colin Cross331a1212018-08-15 20:40:52 -07001247 // merge dex jar with resources if necessary
1248 if j.resourceJar != nil {
1249 jars := android.Paths{dexOutputFile, j.resourceJar}
1250 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1251 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1252 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001253 if j.deviceProperties.UncompressDex {
1254 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1255 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1256 dexOutputFile = combinedAlignedJar
1257 } else {
1258 dexOutputFile = combinedJar
1259 }
Colin Cross331a1212018-08-15 20:40:52 -07001260 }
1261
1262 j.dexJarFile = dexOutputFile
1263
Colin Cross8faf8fc2019-01-16 15:15:52 -08001264 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001265 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1266
1267 j.maybeStrippedDexJarFile = dexOutputFile
1268
Colin Cross3063b782018-08-15 11:19:12 -07001269 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001270
1271 if ctx.Failed() {
1272 return
1273 }
Colin Cross331a1212018-08-15 20:40:52 -07001274 } else {
1275 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001276 }
Colin Cross331a1212018-08-15 20:40:52 -07001277
Colin Crossb7a63242015-04-16 14:09:14 -07001278 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001279
1280 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1281 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001282}
1283
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001284// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1285// since some of these flags may be used internally.
1286func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1287 for _, flag := range flags {
1288 flag = strings.TrimSpace(flag)
1289
1290 if !strings.HasPrefix(flag, "-") {
1291 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1292 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1293 ctx.PropertyErrorf("kotlincflags",
1294 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1295 } else if inList(flag, config.KotlincIllegalFlags) {
1296 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1297 } else if flag == "-include-runtime" {
1298 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1299 } else {
1300 args := strings.Split(flag, " ")
1301 if args[0] == "-kotlin-home" {
1302 ctx.PropertyErrorf("kotlincflags",
1303 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1304 }
1305 }
1306 }
1307}
1308
Colin Cross8eadbf02017-10-24 17:46:00 -07001309func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001310 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001311
1312 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001313 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001314 // Compile java sources into turbine.jar.
1315 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1316 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1317 if ctx.Failed() {
1318 return nil
1319 }
1320 jars = append(jars, turbineJar)
1321 }
1322
Colin Cross55f63ea2018-08-27 12:37:09 -07001323 jars = append(jars, extraJars...)
1324
Nan Zhanged19fc32017-10-19 13:06:22 -07001325 // Combine any static header libraries into classes-header.jar. If there is only
1326 // one input jar this step will be skipped.
1327 var headerJar android.Path
1328 jars = append(jars, deps.staticHeaderJars...)
1329
Colin Cross5c6ecc12017-10-23 18:12:27 -07001330 // we cannot skip the combine step for now if there is only one jar
1331 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1332 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001333 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1334 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001335 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001336
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001337 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001338 // Transform classes.jar into classes-jarjar.jar
1339 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001340 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001341 headerJar = jarjarFile
1342 if ctx.Failed() {
1343 return nil
1344 }
1345 }
1346
1347 return headerJar
1348}
1349
Colin Crosscb933592017-11-22 13:49:43 -08001350func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001351 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001352
Colin Cross7a3139e2017-12-19 13:57:50 -08001353 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001354
Colin Cross84c38822018-01-03 15:59:46 -08001355 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001356 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1357
1358 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1359
1360 j.jacocoReportClassesFile = jacocoReportClassesFile
1361
1362 return instrumentedJar
1363}
1364
albaltai36ff7dc2018-12-25 14:35:23 +08001365var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001366
Nan Zhanged19fc32017-10-19 13:06:22 -07001367func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001368 if j.headerJarFile == nil {
1369 return nil
1370 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001371 return android.Paths{j.headerJarFile}
1372}
1373
1374func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001375 if j.implementationJarFile == nil {
1376 return nil
1377 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001378 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001379}
1380
Colin Crossf24a22a2019-01-31 14:12:44 -08001381func (j *Module) DexJar() android.Path {
1382 return j.dexJarFile
1383}
1384
Colin Cross331a1212018-08-15 20:40:52 -07001385func (j *Module) ResourceJars() android.Paths {
1386 if j.resourceJar == nil {
1387 return nil
1388 }
1389 return android.Paths{j.resourceJar}
1390}
1391
1392func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001393 if j.implementationAndResourcesJar == nil {
1394 return nil
1395 }
Colin Cross331a1212018-08-15 20:40:52 -07001396 return android.Paths{j.implementationAndResourcesJar}
1397}
1398
Colin Cross46c9b8b2017-06-22 16:51:17 -07001399func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001400 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001401 return j.exportAidlIncludeDirs
1402}
1403
Jiyong Park1be96912018-05-28 18:02:19 +09001404func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001405 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001406 return j.exportedSdkLibs
1407}
1408
Colin Cross46c9b8b2017-06-22 16:51:17 -07001409var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001410
Colin Cross46c9b8b2017-06-22 16:51:17 -07001411func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001412 return j.logtagsSrcs
1413}
1414
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001415// Collect information for opening IDE project files in java/jdeps.go.
1416func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1417 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1418 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1419 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001420 if j.expandJarjarRules != nil {
1421 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001422 }
1423}
1424
1425func (j *Module) CompilerDeps() []string {
1426 jdeps := []string{}
1427 jdeps = append(jdeps, j.properties.Libs...)
1428 jdeps = append(jdeps, j.properties.Static_libs...)
1429 return jdeps
1430}
1431
Colin Cross2fe66872015-03-30 17:20:39 -07001432//
1433// Java libraries (.jar file)
1434//
1435
Colin Crossf506d872017-07-19 15:53:04 -07001436type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001437 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001438}
1439
Colin Cross2fc72f62018-12-21 12:59:54 -08001440func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001441 // Store uncompressed (and do not strip) dex files from boot class path jars.
1442 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1443 return true
1444 }
1445
1446 // Store uncompressed dex files that are preopted on /system.
1447 if !j.dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, j.dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001448 return true
1449 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001450 if ctx.Config().UncompressPrivAppDex() &&
1451 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1452 return true
1453 }
1454
Colin Cross2fc72f62018-12-21 12:59:54 -08001455 return false
1456}
1457
Colin Crossf506d872017-07-19 15:53:04 -07001458func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001459 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1460 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001461 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
1462 j.dexpreopter.uncompressedDex = j.shouldUncompressDex(ctx)
1463 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001464 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001465
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001466 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001467 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1468 ctx.ModuleName()+".jar", j.outputFile)
1469 }
Colin Crossb7a63242015-04-16 14:09:14 -07001470}
1471
Colin Crossf506d872017-07-19 15:53:04 -07001472func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001473 j.deps(ctx)
1474}
1475
Colin Cross1b16b0e2019-02-12 14:41:32 -08001476// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1477//
1478// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1479// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1480// as a `static_libs` dependency of another module.
1481//
1482// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1483// a device.
1484//
1485// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1486// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001487func LibraryFactory() android.Module {
1488 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001489
Colin Cross9ae1b922018-06-26 17:59:05 -07001490 module.AddProperties(
1491 &module.Module.properties,
1492 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001493 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001494 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001495
Colin Cross9ae1b922018-06-26 17:59:05 -07001496 InitJavaModule(module, android.HostAndDeviceSupported)
1497 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001498}
1499
Colin Cross1b16b0e2019-02-12 14:41:32 -08001500// java_library_static is an obsolete alias for java_library.
1501func LibraryStaticFactory() android.Module {
1502 return LibraryFactory()
1503}
1504
1505// java_library_host builds and links sources into a `.jar` file for the host.
1506//
1507// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1508// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001509func LibraryHostFactory() android.Module {
1510 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001511
Colin Cross6af17aa2017-09-20 12:59:05 -07001512 module.AddProperties(
1513 &module.Module.properties,
1514 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001515
Colin Cross9ae1b922018-06-26 17:59:05 -07001516 module.Module.properties.Installable = proptools.BoolPtr(true)
1517
Colin Cross89536d42017-07-07 14:35:50 -07001518 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001519 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001520}
1521
1522//
Colin Crossb628ea52018-08-14 16:42:33 -07001523// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001524//
1525
1526type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001527 // list of compatibility suites (for example "cts", "vts") that the module should be
1528 // installed into.
1529 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001530
1531 // the name of the test configuration (for example "AndroidTest.xml") that should be
1532 // installed with the module.
1533 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001534
Jack He33338892018-09-19 02:21:28 -07001535 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1536 // should be installed with the module.
1537 Test_config_template *string `android:"arch_variant"`
1538
Colin Crossd96ca352018-08-10 16:06:24 -07001539 // list of files or filegroup modules that provide data that should be installed alongside
1540 // the test
1541 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001542}
1543
1544type Test struct {
1545 Library
1546
1547 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001548
1549 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001550 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001551}
1552
1553func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001554 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, j.testProperties.Test_suites)
Colin Crossd96ca352018-08-10 16:06:24 -07001555 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001556
1557 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001558}
1559
1560func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1561 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001562 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001563 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001564 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001565}
1566
Colin Cross1b16b0e2019-02-12 14:41:32 -08001567// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1568// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1569//
1570// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1571// compiled against the device bootclasspath.
1572//
1573// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1574// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001575func TestFactory() android.Module {
1576 module := &Test{}
1577
1578 module.AddProperties(
1579 &module.Module.properties,
1580 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001581 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001582 &module.Module.protoProperties,
1583 &module.testProperties)
1584
Colin Cross9ae1b922018-06-26 17:59:05 -07001585 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001586 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001587
Colin Cross05638fc2018-04-09 18:40:24 -07001588 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001589 return module
1590}
1591
Colin Cross1b16b0e2019-02-12 14:41:32 -08001592// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1593// allow running the test with `atest` or a `TEST_MAPPING` file.
1594//
1595// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1596// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001597func TestHostFactory() android.Module {
1598 module := &Test{}
1599
1600 module.AddProperties(
1601 &module.Module.properties,
1602 &module.Module.protoProperties,
1603 &module.testProperties)
1604
Colin Cross9ae1b922018-06-26 17:59:05 -07001605 module.Module.properties.Installable = proptools.BoolPtr(true)
1606
Colin Cross05638fc2018-04-09 18:40:24 -07001607 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001608 return module
1609}
1610
1611//
Colin Cross2fe66872015-03-30 17:20:39 -07001612// Java Binaries (.jar file plus wrapper script)
1613//
1614
Colin Crossf506d872017-07-19 15:53:04 -07001615type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001616 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001617 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001618
1619 // Name of the class containing main to be inserted into the manifest as Main-Class.
1620 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001621}
1622
Colin Crossf506d872017-07-19 15:53:04 -07001623type Binary struct {
1624 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001625
Colin Crossf506d872017-07-19 15:53:04 -07001626 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001627
Colin Cross6b4a32d2017-12-05 13:42:45 -08001628 isWrapperVariant bool
1629
Colin Crossc3315992017-12-08 19:12:36 -08001630 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001631 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001632}
1633
Alex Light24237172017-10-26 09:46:21 -07001634func (j *Binary) HostToolPath() android.OptionalPath {
1635 return android.OptionalPathForPath(j.binaryFile)
1636}
1637
Colin Crossf506d872017-07-19 15:53:04 -07001638func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001639 if ctx.Arch().ArchType == android.Common {
1640 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001641 if j.binaryProperties.Main_class != nil {
1642 if j.properties.Manifest != nil {
1643 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1644 }
1645 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1646 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1647 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1648 }
1649
Colin Cross6b4a32d2017-12-05 13:42:45 -08001650 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001651 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001652 // Handle the binary wrapper
1653 j.isWrapperVariant = true
1654
Colin Cross366938f2017-12-11 16:29:02 -08001655 if j.binaryProperties.Wrapper != nil {
1656 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001657 } else {
1658 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1659 }
1660
1661 // Depend on the installed jar so that the wrapper doesn't get executed by
1662 // another build rule before the jar has been installed.
1663 jarFile := ctx.PrimaryModule().(*Binary).installFile
1664
1665 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1666 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001667 }
Colin Cross2fe66872015-03-30 17:20:39 -07001668}
1669
Colin Crossf506d872017-07-19 15:53:04 -07001670func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001671 if ctx.Arch().ArchType == android.Common {
1672 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001673 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001674 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001675 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001676}
1677
Colin Cross1b16b0e2019-02-12 14:41:32 -08001678// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1679// as well.
1680//
1681// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1682// compiled against the device bootclasspath.
1683//
1684// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1685// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001686func BinaryFactory() android.Module {
1687 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001688
Colin Cross36242852017-06-23 15:06:31 -07001689 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001690 &module.Module.properties,
1691 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001692 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001693 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001694 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001695
Colin Cross9ae1b922018-06-26 17:59:05 -07001696 module.Module.properties.Installable = proptools.BoolPtr(true)
1697
Colin Cross6b4a32d2017-12-05 13:42:45 -08001698 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1699 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001700 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001701}
1702
Colin Cross1b16b0e2019-02-12 14:41:32 -08001703// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1704//
1705// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1706// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001707func BinaryHostFactory() android.Module {
1708 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001709
Colin Cross36242852017-06-23 15:06:31 -07001710 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001711 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001712 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001713 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001714
Colin Cross9ae1b922018-06-26 17:59:05 -07001715 module.Module.properties.Installable = proptools.BoolPtr(true)
1716
Colin Cross6b4a32d2017-12-05 13:42:45 -08001717 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1718 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001719 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001720}
1721
1722//
1723// Java prebuilts
1724//
1725
Colin Cross74d73e22017-08-02 11:05:49 -07001726type ImportProperties struct {
1727 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001728
Nan Zhangea568a42017-11-08 21:20:04 -08001729 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001730
1731 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001732
1733 // List of shared java libs that this module has dependencies to
1734 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001735
1736 // List of files to remove from the jar file(s)
1737 Exclude_files []string
1738
1739 // List of directories to remove from the jar file(s)
1740 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001741
1742 // if set to true, run Jetifier against .jar file. Defaults to false.
1743 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001744}
1745
1746type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001747 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001748 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001749 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001750
Colin Cross74d73e22017-08-02 11:05:49 -07001751 properties ImportProperties
1752
Colin Cross0a6e0072017-08-30 14:24:55 -07001753 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001754 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001755}
1756
Colin Cross83bb3162018-06-25 15:48:06 -07001757func (j *Import) sdkVersion() string {
1758 return String(j.properties.Sdk_version)
1759}
1760
1761func (j *Import) minSdkVersion() string {
1762 return j.sdkVersion()
1763}
1764
Colin Cross74d73e22017-08-02 11:05:49 -07001765func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001766 return &j.prebuilt
1767}
1768
Colin Cross74d73e22017-08-02 11:05:49 -07001769func (j *Import) PrebuiltSrcs() []string {
1770 return j.properties.Jars
1771}
1772
1773func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001774 return j.prebuilt.Name(j.ModuleBase.Name())
1775}
1776
Colin Cross74d73e22017-08-02 11:05:49 -07001777func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001778 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001779 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001780}
1781
Colin Cross74d73e22017-08-02 11:05:49 -07001782func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001783 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001784
Nan Zhang4c819fb2018-08-27 18:31:46 -07001785 jarName := ctx.ModuleName() + ".jar"
1786 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001787 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1788 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001789 if Bool(j.properties.Jetifier_enabled) {
1790 inputFile := outputFile
1791 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1792 TransformJetifier(ctx, outputFile, inputFile)
1793 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001794 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001795
1796 ctx.VisitDirectDeps(func(module android.Module) {
1797 otherName := ctx.OtherModuleName(module)
1798 tag := ctx.OtherModuleDependencyTag(module)
1799
1800 switch dep := module.(type) {
1801 case Dependency:
1802 switch tag {
1803 case libTag, staticLibTag:
1804 // sdk lib names from dependencies are re-exported
1805 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1806 }
1807 case SdkLibraryDependency:
1808 switch tag {
1809 case libTag:
1810 // names of sdk libs that are directly depended are exported
1811 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1812 }
1813 }
1814 })
1815
1816 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001817 if Bool(j.properties.Installable) {
1818 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1819 ctx.ModuleName()+".jar", outputFile)
1820 }
Colin Cross2fe66872015-03-30 17:20:39 -07001821}
1822
Colin Cross74d73e22017-08-02 11:05:49 -07001823var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001824
Nan Zhanged19fc32017-10-19 13:06:22 -07001825func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001826 if j.combinedClasspathFile == nil {
1827 return nil
1828 }
Colin Cross37f6d792018-07-12 12:28:41 -07001829 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001830}
1831
1832func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001833 if j.combinedClasspathFile == nil {
1834 return nil
1835 }
Colin Cross37f6d792018-07-12 12:28:41 -07001836 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001837}
1838
Colin Cross331a1212018-08-15 20:40:52 -07001839func (j *Import) ResourceJars() android.Paths {
1840 return nil
1841}
1842
1843func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001844 if j.combinedClasspathFile == nil {
1845 return nil
1846 }
Colin Cross331a1212018-08-15 20:40:52 -07001847 return android.Paths{j.combinedClasspathFile}
1848}
1849
Colin Crossf24a22a2019-01-31 14:12:44 -08001850func (j *Import) DexJar() android.Path {
1851 return nil
1852}
1853
Colin Cross74d73e22017-08-02 11:05:49 -07001854func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001855 return nil
1856}
1857
Jiyong Park1be96912018-05-28 18:02:19 +09001858func (j *Import) ExportedSdkLibs() []string {
1859 return j.exportedSdkLibs
1860}
1861
albaltai36ff7dc2018-12-25 14:35:23 +08001862// Add compile time check for interface implementation
1863var _ android.IDEInfo = (*Import)(nil)
1864var _ android.IDECustomizedModuleName = (*Import)(nil)
1865
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001866// Collect information for opening IDE project files in java/jdeps.go.
1867const (
1868 removedPrefix = "prebuilt_"
1869)
1870
1871func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1872 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1873}
1874
1875func (j *Import) IDECustomizedModuleName() string {
1876 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1877 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1878 // solution to get the Import name.
1879 name := j.Name()
1880 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001881 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001882 }
1883 return name
1884}
1885
Colin Cross74d73e22017-08-02 11:05:49 -07001886var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001887
Colin Cross1b16b0e2019-02-12 14:41:32 -08001888// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1889//
1890// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1891// compiled against an Android classpath.
1892//
1893// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1894// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001895func ImportFactory() android.Module {
1896 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001897
Colin Cross74d73e22017-08-02 11:05:49 -07001898 module.AddProperties(&module.properties)
1899
1900 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001901 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001902 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001903}
1904
Colin Cross1b16b0e2019-02-12 14:41:32 -08001905// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1906// module.
1907//
1908// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1909// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001910func ImportFactoryHost() android.Module {
1911 module := &Import{}
1912
1913 module.AddProperties(&module.properties)
1914
1915 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001916 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001917 return module
1918}
1919
Colin Cross89536d42017-07-07 14:35:50 -07001920//
1921// Defaults
1922//
1923type Defaults struct {
1924 android.ModuleBase
1925 android.DefaultsModuleBase
1926}
1927
1928func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1929}
1930
Colin Cross1b16b0e2019-02-12 14:41:32 -08001931// java_defaults provides a set of properties that can be inherited by other java or android modules.
1932//
1933// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1934// property in the defaults module that exists in the depending module will be prepended to the depending module's
1935// value for that property.
1936//
1937// Example:
1938//
1939// java_defaults {
1940// name: "example_defaults",
1941// srcs: ["common/**/*.java"],
1942// javacflags: ["-Xlint:all"],
1943// aaptflags: ["--auto-add-overlay"],
1944// }
1945//
1946// java_library {
1947// name: "example",
1948// defaults: ["example_defaults"],
1949// srcs: ["example/**/*.java"],
1950// }
1951//
1952// is functionally identical to:
1953//
1954// java_library {
1955// name: "example",
1956// srcs: [
1957// "common/**/*.java",
1958// "example/**/*.java",
1959// ],
1960// javacflags: ["-Xlint:all"],
1961// }
Colin Cross89536d42017-07-07 14:35:50 -07001962func defaultsFactory() android.Module {
1963 return DefaultsFactory()
1964}
1965
1966func DefaultsFactory(props ...interface{}) android.Module {
1967 module := &Defaults{}
1968
1969 module.AddProperties(props...)
1970 module.AddProperties(
1971 &CompilerProperties{},
1972 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001973 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001974 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001975 &aaptProperties{},
1976 &androidLibraryProperties{},
1977 &appProperties{},
1978 &appTestProperties{},
1979 &ImportProperties{},
1980 &AARImportProperties{},
1981 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001982 )
1983
1984 android.InitDefaultsModule(module)
1985
1986 return module
1987}
Nan Zhangea568a42017-11-08 21:20:04 -08001988
1989var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001990var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001991var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001992var inList = android.InList