blob: fac28ed0dfe0f7894a4be33d2ca221d3485bd8c6 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Colin Cross2fe66872015-03-30 17:20:39 -070055// TODO:
56// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070057// Renderscript
58// Post-jar passes:
59// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070060// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070061// DroidDoc
62// Findbugs
63
Colin Cross89536d42017-07-07 14:35:50 -070064type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070065 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
66 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070068
69 // list of source files that should not be used to build the Java module.
70 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080071 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070072
73 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070074 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070075
Colin Cross86a63ff2017-09-27 17:33:10 -070076 // list of directories that should be excluded from java_resource_dirs
77 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070078
Colin Cross0f37af02017-09-27 17:42:05 -070079 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080080 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070081
Colin Crosscedd4762018-09-13 11:26:19 -070082 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080083 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070084
Paul Duffin2fbbfb82019-02-13 12:49:33 +000085 // don't build against the default libraries (bootclasspath, ext, and framework for device
86 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070087 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070088
Paul Duffin2fbbfb82019-02-13 12:49:33 +000089 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070090 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020095 // list of module-specific flags that will be used for kotlinc compiles
96 Kotlincflags []string `android:"arch_variant"`
97
Colin Cross7d5136f2015-05-11 13:39:40 -070098 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700102 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
104 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700106
Colin Cross540eff82017-06-22 17:01:52 -0700107 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700109
110 // If not blank, set the java version passed to javac as -source and -target
111 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700112
Colin Cross9ae1b922018-06-26 17:59:05 -0700113 // If set to true, allow this module to be dexed and installed on devices. Has no
114 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700115 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700116
Colin Cross0f37af02017-09-27 17:42:05 -0700117 // If set to true, include sources used to compile the module in to the final jar
118 Include_srcs *bool
119
Vladimir Marko0975ee02019-04-02 10:29:55 +0100120 // If not empty, classes are restricted to the specified packages and their sub-packages.
121 // This restriction is checked after applying jarjar rules and including static libs.
122 Permitted_packages []string
123
Colin Crossbe9cdb82019-01-21 21:37:16 -0800124 // List of modules to use as annotation processors
125 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700126
Nan Zhang61eaedb2017-11-02 13:28:15 -0700127 // The number of Java source entries each Javac instance can process
128 Javac_shard_size *int64
129
Nan Zhang5f8cb422018-02-06 10:34:32 -0800130 // Add host jdk tools.jar to bootclasspath
131 Use_tools_jar *bool
132
Colin Cross1369cdb2017-09-29 17:58:17 -0700133 Openjdk9 struct {
134 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800135 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700136
137 // List of javac flags that should only be used when passing -source 1.9
138 Javacflags []string
139 }
Colin Crosscb933592017-11-22 13:49:43 -0800140
Colin Cross81440082018-08-15 20:21:55 -0700141 // When compiling language level 9+ .java code in packages that are part of
142 // a system module, patch_module names the module that your sources and
143 // dependencies should be patched into. The Android runtime currently
144 // doesn't implement the JEP 261 module system so this option is only
145 // supported at compile time. It should only be needed to compile tests in
146 // packages that exist in libcore and which are inconvenient to move
147 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100148 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700149
Colin Crosscb933592017-11-22 13:49:43 -0800150 Jacoco struct {
151 // List of classes to include for instrumentation with jacoco to collect coverage
152 // information at runtime when building with coverage enabled. If unset defaults to all
153 // classes.
154 // Supports '*' as the last character of an entry in the list as a wildcard match.
155 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
156 // it matches classes in the package that have the class name as a prefix.
157 Include_filter []string
158
159 // List of classes to exclude from instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. Overrides classes selected
161 // by the include_filter property.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Exclude_filter []string
166 }
167
Andreas Gampef3e5b552018-01-22 21:27:21 -0800168 Errorprone struct {
169 // List of javac flags that should only be used when running errorprone.
170 Javacflags []string
171 }
172
Colin Cross0f2ee152017-12-14 15:22:43 -0800173 Proto struct {
174 // List of extra options that will be passed to the proto generator.
175 Output_params []string
176 }
177
Colin Crosscb933592017-11-22 13:49:43 -0800178 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800179
180 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800181 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700182}
183
Colin Cross89536d42017-07-07 14:35:50 -0700184type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700185 // list of module-specific flags that will be used for dex compiles
186 Dxflags []string `android:"arch_variant"`
187
Colin Cross83bb3162018-06-25 15:48:06 -0700188 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
189 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800190 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700191
Colin Cross83bb3162018-06-25 15:48:06 -0700192 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
193 // Defaults to sdk_version if not set.
194 Min_sdk_version *string
195
Dan Willemsen419290a2018-10-31 15:28:47 -0700196 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
197 // Defaults to sdk_version if not set.
198 Target_sdk_version *string
199
Colin Cross6af2e492018-05-22 11:12:33 -0700200 // if true, compile against the platform APIs instead of an SDK.
201 Platform_apis *bool
202
Colin Crossebe1a512017-11-14 13:12:14 -0800203 Aidl struct {
204 // Top level directories to pass to aidl tool
205 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700206
Colin Crossebe1a512017-11-14 13:12:14 -0800207 // Directories rooted at the Android.bp file to pass to aidl tool
208 Local_include_dirs []string
209
210 // directories that should be added as include directories for any aidl sources of modules
211 // that depend on this module, as well as to aidl for this module.
212 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100213
214 // whether to generate traces (for systrace) for this interface
215 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100216
217 // whether to generate Binder#GetTransaction name method.
218 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800219 }
Colin Cross92430102017-10-09 14:59:32 -0700220
221 // If true, export a copy of the module as a -hostdex module for host testing.
222 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700223
David Brazdil17ef5632018-06-27 10:27:45 +0100224 // If set to true, compile dex regardless of installable. Defaults to false.
225 Compile_dex *bool
226
Colin Cross66dbc0b2017-12-28 12:23:20 -0800227 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700228 // If false, disable all optimization. Defaults to true for android_app and android_test
229 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800230 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700231 // True if the module containing this has it set by default.
232 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800233
234 // If true, optimize for size by removing unused code. Defaults to true for apps,
235 // false for libraries and tests.
236 Shrink *bool
237
238 // If true, optimize bytecode. Defaults to false.
239 Optimize *bool
240
241 // If true, obfuscate bytecode. Defaults to false.
242 Obfuscate *bool
243
244 // If true, do not use the flag files generated by aapt that automatically keep
245 // classes referenced by the app manifest. Defaults to false.
246 No_aapt_flags *bool
247
248 // Flags to pass to proguard.
249 Proguard_flags []string
250
251 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800252 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800253 }
254
Colin Cross1369cdb2017-09-29 17:58:17 -0700255 // When targeting 1.9, override the modules to use with --system
256 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700257
258 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800259 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700260}
261
Sasha Smundak2057f822019-04-16 17:16:58 -0700262func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
263 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
264}
265
Colin Cross46c9b8b2017-06-22 16:51:17 -0700266// Module contains the properties and members used by all java module types
267type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700268 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700269 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700270
Colin Cross89536d42017-07-07 14:35:50 -0700271 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700272 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700273 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700274
Colin Cross331a1212018-08-15 20:40:52 -0700275 // jar file containing header classes including static library dependencies, suitable for
276 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 headerJarFile android.Path
278
Colin Cross331a1212018-08-15 20:40:52 -0700279 // jar file containing implementation classes including static library dependencies but no
280 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700281 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700282
Colin Cross331a1212018-08-15 20:40:52 -0700283 // jar file containing only resources including from static library dependencies
284 resourceJar android.Path
285
286 // jar file containing implementation classes and resources including static library
287 // dependencies
288 implementationAndResourcesJar android.Path
289
290 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700291 dexJarFile android.Path
292
Colin Cross43f08db2018-11-12 10:13:39 -0800293 // output file that contains classes.dex if it should be in the output file
294 maybeStrippedDexJarFile android.Path
295
Colin Crosscb933592017-11-22 13:49:43 -0800296 // output file containing uninstrumented classes that will be instrumented by jacoco
297 jacocoReportClassesFile android.Path
298
Colin Cross66dbc0b2017-12-28 12:23:20 -0800299 // output file containing mapping of obfuscated names
300 proguardDictionary android.Path
301
Colin Cross331a1212018-08-15 20:40:52 -0700302 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700303 outputFile android.Path
304 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700305
Colin Cross635c3b02016-05-18 15:37:25 -0700306 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700307
Colin Cross635c3b02016-05-18 15:37:25 -0700308 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700309
Colin Cross2fe66872015-03-30 17:20:39 -0700310 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700311 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800312
313 // list of .java files and srcjars that was passed to javac
314 compiledJavaSrcs android.Paths
315 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800316
317 // list of extra progurad flag files
318 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900319
Colin Cross094054a2018-10-17 15:10:48 -0700320 // manifest file to use instead of properties.Manifest
321 overrideManifest android.OptionalPath
322
Jiyong Park1be96912018-05-28 18:02:19 +0900323 // list of SDK lib names that this java moudule is exporting
324 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700325
326 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
327 // filter out Exclude_srcs, will be used by android.IDEInfo struct
328 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800329
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800330 // expanded Jarjar_rules
331 expandJarjarRules android.Path
332
Vladimir Marko0975ee02019-04-02 10:29:55 +0100333 // list of additional targets for checkbuild
334 additionalCheckedModules android.Paths
335
Colin Crossf24a22a2019-01-31 14:12:44 -0800336 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800337 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700338}
339
Colin Cross54250902017-12-05 09:28:08 -0800340func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700341 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800342}
343
Jiyong Park8fd61922018-11-08 02:50:25 +0900344func (j *Module) DexJarFile() android.Path {
345 return j.dexJarFile
346}
347
Colin Cross54250902017-12-05 09:28:08 -0800348var _ android.SourceFileProducer = (*Module)(nil)
349
Colin Crossf506d872017-07-19 15:53:04 -0700350type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700351 HeaderJars() android.Paths
352 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700353 ResourceJars() android.Paths
354 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800355 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700356 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900357 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700358}
359
Jiyong Parkc678ad32018-04-10 13:07:10 +0900360type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800361 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
362 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900363}
364
Nan Zhangb2b33de2018-02-23 11:18:47 -0800365type SrcDependency interface {
366 CompiledSrcs() android.Paths
367 CompiledSrcJars() android.Paths
368}
369
370func (j *Module) CompiledSrcs() android.Paths {
371 return j.compiledJavaSrcs
372}
373
374func (j *Module) CompiledSrcJars() android.Paths {
375 return j.compiledSrcJars
376}
377
378var _ SrcDependency = (*Module)(nil)
379
Colin Cross89536d42017-07-07 14:35:50 -0700380func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
381 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
382 android.InitDefaultableModule(module)
383}
384
Colin Crossbe1da472017-07-07 15:59:46 -0700385type dependencyTag struct {
386 blueprint.BaseDependencyTag
387 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700388}
389
Colin Crossa4f08812018-10-02 22:03:40 -0700390type jniDependencyTag struct {
391 blueprint.BaseDependencyTag
392 target android.Target
393}
394
Colin Crossbe1da472017-07-07 15:59:46 -0700395var (
Colin Cross4b964c02018-10-15 16:18:06 -0700396 staticLibTag = dependencyTag{name: "staticlib"}
397 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800398 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700399 bootClasspathTag = dependencyTag{name: "bootclasspath"}
400 systemModulesTag = dependencyTag{name: "system modules"}
401 frameworkResTag = dependencyTag{name: "framework-res"}
402 frameworkApkTag = dependencyTag{name: "framework-apk"}
403 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800404 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700405 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
406 certificateTag = dependencyTag{name: "certificate"}
407 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700408)
Colin Cross2fe66872015-03-30 17:20:39 -0700409
Colin Crossfc3674a2017-09-18 17:41:52 -0700410type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700411 useModule, useFiles, useDefaultLibs, invalidVersion bool
412
Colin Cross86a60ae2018-05-29 14:44:55 -0700413 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700414 systemModules string
415
Colin Crossa97c5d32018-03-28 14:58:31 -0700416 frameworkResModule string
417
Colin Cross86a60ae2018-05-29 14:44:55 -0700418 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700419 aidl android.OptionalPath
Colin Cross1369cdb2017-09-29 17:58:17 -0700420}
421
Colin Crossa4f08812018-10-02 22:03:40 -0700422type jniLib struct {
423 name string
424 path android.Path
425 target android.Target
426}
427
Colin Cross3144dfc2018-01-03 15:06:47 -0800428func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
429 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
430}
431
432func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
433 return j.shouldInstrument(ctx) &&
434 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
435 ctx.Config().UnbundledBuild())
436}
437
Colin Cross83bb3162018-06-25 15:48:06 -0700438func (j *Module) sdkVersion() string {
439 return String(j.deviceProperties.Sdk_version)
440}
441
442func (j *Module) minSdkVersion() string {
443 if j.deviceProperties.Min_sdk_version != nil {
444 return *j.deviceProperties.Min_sdk_version
445 }
446 return j.sdkVersion()
447}
448
Dan Willemsen419290a2018-10-31 15:28:47 -0700449func (j *Module) targetSdkVersion() string {
450 if j.deviceProperties.Target_sdk_version != nil {
451 return *j.deviceProperties.Target_sdk_version
452 }
453 return j.sdkVersion()
454}
455
Colin Crossbe1da472017-07-07 15:59:46 -0700456func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700457 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700458 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700459 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700460 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700461 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100462 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700463 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700464 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700465 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700466 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100467 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700468 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700469 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700470 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
471 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800472 }
Colin Crossbe1da472017-07-07 15:59:46 -0700473 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700474 } else if j.deviceProperties.System_modules == nil {
475 ctx.PropertyErrorf("no_standard_libs",
476 "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 +0100477 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700478 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700479 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100480 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700481 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800482 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800483 if ctx.ModuleName() == "android_stubs_current" ||
484 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700485 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700486 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800487 }
Colin Cross2fe66872015-03-30 17:20:39 -0700488 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700489
Colin Cross42d48b72018-08-29 14:10:52 -0700490 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
491 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700492
Colin Crossbe9cdb82019-01-21 21:37:16 -0800493 ctx.AddFarVariationDependencies([]blueprint.Variation{
494 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
495 }, pluginTag, j.properties.Plugins...)
496
Colin Crossfe17f6f2019-03-28 19:30:56 -0700497 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700498 if j.hasSrcExt(".proto") {
499 protoDeps(ctx, &j.protoProperties)
500 }
Colin Cross93e85952017-08-15 13:34:18 -0700501
502 if j.hasSrcExt(".kt") {
503 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
504 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700505 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800506 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800507 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
508 }
Colin Cross93e85952017-08-15 13:34:18 -0700509 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800510
511 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700512 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800513 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700514}
515
516func hasSrcExt(srcs []string, ext string) bool {
517 for _, src := range srcs {
518 if filepath.Ext(src) == ext {
519 return true
520 }
521 }
522
523 return false
524}
525
Nan Zhang61eaedb2017-11-02 13:28:15 -0700526func shardPaths(paths android.Paths, shardSize int) []android.Paths {
527 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
528 for len(paths) > shardSize {
529 ret = append(ret, paths[0:shardSize])
530 paths = paths[shardSize:]
531 }
532 if len(paths) > 0 {
533 ret = append(ret, paths)
534 }
535 return ret
536}
537
Colin Cross6af17aa2017-09-20 12:59:05 -0700538func (j *Module) hasSrcExt(ext string) bool {
539 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700540}
541
Colin Cross46c9b8b2017-06-22 16:51:17 -0700542func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700543 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700544
Colin Crossebe1a512017-11-14 13:12:14 -0800545 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
546 aidlIncludes = append(aidlIncludes,
547 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
548 aidlIncludes = append(aidlIncludes,
549 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700550
Colin Cross3047fa22019-04-18 10:56:44 -0700551 var flags []string
552 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700553
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700554 if aidlPreprocess.Valid() {
555 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700556 deps = append(deps, aidlPreprocess.Path())
557 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700558 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700559 }
560
Colin Cross3047fa22019-04-18 10:56:44 -0700561 if len(j.exportAidlIncludeDirs) > 0 {
562 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
563 }
564
565 if len(aidlIncludes) > 0 {
566 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
567 }
568
Colin Cross635c3b02016-05-18 15:37:25 -0700569 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800570 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700571 flags = append(flags, "-I"+src.String())
572 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700573
Martijn Coeneneab15642018-03-09 09:29:59 +0100574 if Bool(j.deviceProperties.Aidl.Generate_traces) {
575 flags = append(flags, "-t")
576 }
577
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100578 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
579 flags = append(flags, "--transaction_names")
580 }
581
Colin Cross3047fa22019-04-18 10:56:44 -0700582 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700583}
584
Colin Cross32f676a2017-09-06 13:41:06 -0700585type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800586 classpath classpath
587 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700588 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800589 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700590 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700591 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700592 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700593 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800594 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700595 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700596 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700597 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700598 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800599 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800600
601 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700602}
Colin Cross2fe66872015-03-30 17:20:39 -0700603
Colin Cross54250902017-12-05 09:28:08 -0800604func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
605 for _, f := range dep.Srcs() {
606 if f.Ext() != ".jar" {
607 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
608 ctx.OtherModuleName(dep.(blueprint.Module)))
609 }
610 }
611}
612
Jiyong Park2d492942018-03-05 17:44:10 +0900613type linkType int
614
615const (
616 javaCore linkType = iota
617 javaSdk
618 javaSystem
619 javaPlatform
620)
621
Jiyong Park46f78fb2018-10-20 16:33:17 +0900622func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700623 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700624 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900625 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
626 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
627 name == "core-lambda-stubs":
628 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100629 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900630 return javaCore, false
631 case name == "android_system_stubs_current":
632 return javaSystem, true
633 case strings.HasPrefix(ver, "system_"):
634 return javaSystem, false
635 case name == "android_test_stubs_current":
636 return javaSystem, true
637 case strings.HasPrefix(ver, "test_"):
638 return javaPlatform, false
639 case name == "android_stubs_current":
640 return javaSdk, true
641 case ver == "current":
642 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700643 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900644 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700645 default:
646 if _, err := strconv.Atoi(ver); err != nil {
647 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
648 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900649 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900650 }
651}
652
Jiyong Park750e5572018-01-31 00:20:13 +0900653func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700654 if ctx.Host() {
655 return
656 }
657
Jiyong Park46f78fb2018-10-20 16:33:17 +0900658 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
659 if stubs {
660 return
661 }
662 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900663 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."
664
665 switch myLinkType {
666 case javaCore:
667 if otherLinkType != javaCore {
668 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 +0900669 ctx.OtherModuleName(to))
670 }
Jiyong Park2d492942018-03-05 17:44:10 +0900671 break
672 case javaSdk:
673 if otherLinkType != javaCore && otherLinkType != javaSdk {
674 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
675 ctx.OtherModuleName(to))
676 }
677 break
678 case javaSystem:
679 if otherLinkType == javaPlatform {
680 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
681 ctx.OtherModuleName(to))
682 }
683 break
684 case javaPlatform:
685 // no restriction on link-type
686 break
Jiyong Park750e5572018-01-31 00:20:13 +0900687 }
688}
689
Colin Cross32f676a2017-09-06 13:41:06 -0700690func (j *Module) collectDeps(ctx android.ModuleContext) deps {
691 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700692
Colin Cross300f0382018-03-06 13:11:51 -0800693 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700694 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800695 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700696 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800697 } else if sdkDep.useFiles {
698 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700699 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700700 deps.aidlPreprocess = sdkDep.aidl
701 } else {
702 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800703 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700704 }
705
Colin Crossd11fcda2017-10-23 17:59:01 -0700706 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700707 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700708 tag := ctx.OtherModuleDependencyTag(module)
709
Colin Crossa4f08812018-10-02 22:03:40 -0700710 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700711 // Handled by AndroidApp.collectAppDeps
712 return
713 }
714 if tag == certificateTag {
715 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700716 return
717 }
718
Jiyong Park750e5572018-01-31 00:20:13 +0900719 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700720 switch tag {
721 case bootClasspathTag, libTag, staticLibTag:
722 checkLinkType(ctx, j, to, tag.(dependencyTag))
723 }
Jiyong Park750e5572018-01-31 00:20:13 +0900724 }
Colin Cross54250902017-12-05 09:28:08 -0800725 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800726 case SdkLibraryDependency:
727 switch tag {
728 case libTag:
729 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
730 // names of sdk libs that are directly depended are exported
731 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700732 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800733 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
734 }
Colin Cross54250902017-12-05 09:28:08 -0800735 case Dependency:
736 switch tag {
737 case bootClasspathTag:
738 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700739 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800740 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900741 // sdk lib names from dependencies are re-exported
742 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700743 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800744 case staticLibTag:
745 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
746 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
747 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700748 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900749 // sdk lib names from dependencies are re-exported
750 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700751 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800752 case pluginTag:
753 if plugin, ok := dep.(*Plugin); ok {
754 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
755 if plugin.pluginProperties.Processor_class != nil {
756 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
757 }
758 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
759 } else {
760 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
761 }
Colin Cross54250902017-12-05 09:28:08 -0800762 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100763 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800764 // framework.jar has a one-off dependency on the R.java and Manifest.java files
765 // generated by framework-res.apk
766 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
767 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800768 case frameworkApkTag:
769 if ctx.ModuleName() == "android_stubs_current" ||
770 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700771 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800772 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
773 // resource files out of there for aapt.
774 //
775 // Normally the package rule runs aapt, which includes the resource,
776 // but we're not running that in our package rule so just copy in the
777 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700778 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800779 }
Colin Cross54250902017-12-05 09:28:08 -0800780 case kotlinStdlibTag:
781 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800782 case kotlinAnnotationsTag:
783 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800784 }
785
Colin Cross54250902017-12-05 09:28:08 -0800786 case android.SourceFileProducer:
787 switch tag {
788 case libTag:
789 checkProducesJars(ctx, dep)
790 deps.classpath = append(deps.classpath, dep.Srcs()...)
791 case staticLibTag:
792 checkProducesJars(ctx, dep)
793 deps.classpath = append(deps.classpath, dep.Srcs()...)
794 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
795 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800796 }
797 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700798 switch tag {
799 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700800 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700801 case systemModulesTag:
802 if deps.systemModules != nil {
803 panic("Found two system module dependencies")
804 }
805 sm := module.(*SystemModules)
806 if sm.outputFile == nil {
807 panic("Missing directory for system module dependency")
808 }
809 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700810 default:
811 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700812 }
Colin Crossec7a0422017-07-07 14:47:12 -0700813 }
Colin Cross2fe66872015-03-30 17:20:39 -0700814 })
815
Jiyong Park1be96912018-05-28 18:02:19 +0900816 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
817
Colin Cross32f676a2017-09-06 13:41:06 -0700818 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700819}
820
Colin Cross83bb3162018-06-25 15:48:06 -0700821func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700822 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800823 v := sdkContext.sdkVersion()
824 // For PDK builds, use the latest SDK version instead of "current"
825 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700826 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800827 latestSdkVersion := 0
828 if len(sdkVersions) > 0 {
829 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
830 }
831 v = strconv.Itoa(latestSdkVersion)
832 }
833
834 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700835 if err != nil {
836 ctx.PropertyErrorf("sdk_version", "%s", err)
837 }
Nan Zhang357466b2018-04-17 17:38:36 -0700838 if javaVersion != "" {
839 ret = javaVersion
840 } else if ctx.Device() && sdk <= 23 {
841 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900842 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700843 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700844 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700845 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
846 ret = "1.8"
847 } else {
848 ret = "1.9"
849 }
850
851 return ret
852}
853
Nan Zhanged19fc32017-10-19 13:06:22 -0700854func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700855
Colin Crossf03c82b2015-04-13 13:53:40 -0700856 var flags javaBuilderFlags
857
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100858 // javaVersion flag.
859 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
860
Nan Zhanged19fc32017-10-19 13:06:22 -0700861 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700862 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100863 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700864 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700865 }
Colin Cross6510f912017-11-29 00:27:14 -0800866 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700867 // Override the -g flag passed globally to remove local variable debug info to reduce
868 // disk and memory usage.
869 javacFlags = append(javacFlags, "-g:source,lines")
870 }
Colin Cross64162712017-08-08 13:17:59 -0700871
Colin Cross66548102018-06-19 22:47:35 -0700872 if ctx.Config().RunErrorProne() {
873 if config.ErrorProneClasspath == nil {
874 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
875 }
876
877 errorProneFlags := []string{
878 "-Xplugin:ErrorProne",
879 "${config.ErrorProneChecks}",
880 }
881 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
882
883 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
884 "'" + strings.Join(errorProneFlags, " ") + "'"
885 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800886 }
887
Nan Zhanged19fc32017-10-19 13:06:22 -0700888 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800889 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
890 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700891 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800892
Colin Crossbe9cdb82019-01-21 21:37:16 -0800893 flags.processor = strings.Join(deps.processorClasses, ",")
894
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100895 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800896 !Bool(j.properties.No_standard_libs) &&
897 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
898 // Give host-side tools a version of OpenJDK's standard libraries
899 // close to what they're targeting. As of Dec 2017, AOSP is only
900 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
901 //
902 // When building with OpenJDK 8, the following should have no
903 // effect since those jars would be available by default.
904 //
905 // When building with OpenJDK 9 but targeting a version < 1.8,
906 // putting them on the bootclasspath means that:
907 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
908 // b) references to existing APIs are not reinterpreted in an
909 // OpenJDK 9-specific way, eg. calls to subclasses of
910 // java.nio.Buffer as in http://b/70862583
911 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
912 flags.bootClasspath = append(flags.bootClasspath,
913 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
914 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800915 if Bool(j.properties.Use_tools_jar) {
916 flags.bootClasspath = append(flags.bootClasspath,
917 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
918 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800919 }
920
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100921 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800922 // Manually specify build directory in case it is not under the repo root.
923 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
924 // just adding a symlink under the root doesn't help.)
925 patchPaths := ".:" + ctx.Config().BuildDir()
926 classPath := flags.classpath.FormJavaClassPath("")
927 if classPath != "" {
928 patchPaths += ":" + classPath
929 }
930 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700931 }
932
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700934 if deps.systemModules != nil {
935 flags.systemModules = append(flags.systemModules, deps.systemModules)
936 }
937
Nan Zhanged19fc32017-10-19 13:06:22 -0700938 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700939 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700940
Colin Cross81440082018-08-15 20:21:55 -0700941 if len(javacFlags) > 0 {
942 // optimization.
943 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
944 flags.javacFlags = "$javacFlags"
945 }
946
Nan Zhanged19fc32017-10-19 13:06:22 -0700947 return flags
948}
Colin Crossc0b06f12015-04-08 13:03:43 -0700949
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800950func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700951
Colin Crossebe1a512017-11-14 13:12:14 -0800952 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700953
954 deps := j.collectDeps(ctx)
955 flags := j.collectBuilderFlags(ctx, deps)
956
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100957 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700958 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
959 }
Colin Cross8a497952019-03-05 22:25:09 -0800960 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700961 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800962 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700963 }
964
Colin Crossaf050172017-11-15 23:01:59 -0800965 srcFiles = j.genSources(ctx, srcFiles, flags)
966
967 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700968 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800969 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700970
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700971 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
972 // that IDEInfo struct will use
973 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
974
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800975 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800976 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800977 }
978
Colin Cross1ee23172017-10-18 14:44:18 -0700979 jarName := ctx.ModuleName() + ".jar"
980
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000981 javaSrcFiles := srcFiles.FilterByExt(".java")
982 var uniqueSrcFiles android.Paths
983 set := make(map[string]bool)
984 for _, v := range javaSrcFiles {
985 if _, found := set[v.String()]; !found {
986 set[v.String()] = true
987 uniqueSrcFiles = append(uniqueSrcFiles, v)
988 }
989 }
990
Colin Cross55f63ea2018-08-27 12:37:09 -0700991 var kotlinJars android.Paths
992
Colin Cross93e85952017-08-15 13:34:18 -0700993 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200994 // user defined kotlin flags.
995 kotlincFlags := j.properties.Kotlincflags
996 CheckKotlincFlags(ctx, kotlincFlags)
997
Colin Cross93e85952017-08-15 13:34:18 -0700998 // If there are kotlin files, compile them first but pass all the kotlin and java files
999 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1000 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001001 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001002 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001003 kotlincFlags = append(kotlincFlags, "-no-jdk")
1004 }
1005 if len(kotlincFlags) > 0 {
1006 // optimization.
1007 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1008 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001009 }
1010
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001011 var kotlinSrcFiles android.Paths
1012 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1013 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1014
Colin Crossafbb1732019-01-17 15:42:52 -08001015 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1016 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1017
1018 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1019 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1020
1021 if len(flags.processorPath) > 0 {
1022 // Use kapt for annotation processing
1023 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1024 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1025 srcJars = append(srcJars, kaptSrcJar)
1026 // Disable annotation processing in javac, it's already been handled by kapt
1027 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001028 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001029 }
Colin Cross93e85952017-08-15 13:34:18 -07001030
Colin Cross1ee23172017-10-18 14:44:18 -07001031 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001032 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001033 if ctx.Failed() {
1034 return
1035 }
1036
1037 // Make javac rule depend on the kotlinc rule
1038 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001039
Colin Cross93e85952017-08-15 13:34:18 -07001040 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001041 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001042 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001043 }
1044
Colin Cross55f63ea2018-08-27 12:37:09 -07001045 jars := append(android.Paths(nil), kotlinJars...)
1046
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001047 // Store the list of .java files that was passed to javac
1048 j.compiledJavaSrcs = uniqueSrcFiles
1049 j.compiledSrcJars = srcJars
1050
Nan Zhang61eaedb2017-11-02 13:28:15 -07001051 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001052 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001053 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1054 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001055 // Formerly, there was a check here that prevented annotation processors
1056 // from being used when sharding was enabled, as some annotation processors
1057 // do not function correctly in sharded environments. It was removed to
1058 // allow for the use of annotation processors that do function correctly
1059 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001060 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001061 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001062 if ctx.Failed() {
1063 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001064 }
1065 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001066 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001067 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001068 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001069 // If error-prone is enabled, add an additional rule to compile the java files into
1070 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001071 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001072 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1073 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001074 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001075 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001076 extraJarDeps = append(extraJarDeps, errorprone)
1077 }
1078
Nan Zhang61eaedb2017-11-02 13:28:15 -07001079 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001080 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001081 shardSize := int(*(j.properties.Javac_shard_size))
1082 var shardSrcs []android.Paths
1083 if len(uniqueSrcFiles) > 0 {
1084 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1085 for idx, shardSrc := range shardSrcs {
1086 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1087 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1088 jars = append(jars, classes)
1089 }
1090 }
1091 if len(srcJars) > 0 {
1092 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1093 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1094 jars = append(jars, classes)
1095 }
1096 } else {
1097 classes := android.PathForModuleOut(ctx, "javac", jarName)
1098 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1099 jars = append(jars, classes)
1100 }
Colin Crossd6891432017-09-27 17:39:56 -07001101 if ctx.Failed() {
1102 return
1103 }
Colin Cross2fe66872015-03-30 17:20:39 -07001104 }
1105
Colin Crosscedd4762018-09-13 11:26:19 -07001106 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1107 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001108 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1109
1110 var resArgs []string
1111 var resDeps android.Paths
1112
1113 resArgs = append(resArgs, dirArgs...)
1114 resDeps = append(resDeps, dirDeps...)
1115
1116 resArgs = append(resArgs, fileArgs...)
1117 resDeps = append(resDeps, fileDeps...)
1118
Colin Crossff3ae9d2018-04-10 16:15:18 -07001119 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001120 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001121 resArgs = append(resArgs, srcArgs...)
1122 resDeps = append(resDeps, srcDeps...)
1123 }
Colin Cross40a36712017-09-27 17:41:35 -07001124
1125 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001126 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001127 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001128 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001129 if ctx.Failed() {
1130 return
1131 }
1132 }
1133
Colin Cross331a1212018-08-15 20:40:52 -07001134 if len(deps.staticResourceJars) > 0 {
1135 var jars android.Paths
1136 if j.resourceJar != nil {
1137 jars = append(jars, j.resourceJar)
1138 }
1139 jars = append(jars, deps.staticResourceJars...)
1140
1141 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1142 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1143 false, nil, nil)
1144 j.resourceJar = combinedJar
1145 }
1146
Colin Cross32f676a2017-09-06 13:41:06 -07001147 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001148
Colin Cross094054a2018-10-17 15:10:48 -07001149 manifest := j.overrideManifest
1150 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001151 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001152 }
Colin Cross635acc92017-09-12 22:50:46 -07001153
Colin Cross8a497952019-03-05 22:25:09 -08001154 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001155 if len(services) > 0 {
1156 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1157 var zipargs []string
1158 for _, file := range services {
1159 serviceFile := file.String()
1160 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1161 }
1162 ctx.Build(pctx, android.BuildParams{
1163 Rule: zip,
1164 Output: servicesJar,
1165 Implicits: services,
1166 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001167 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001168 },
1169 })
1170 jars = append(jars, servicesJar)
1171 }
1172
Colin Cross0a6e0072017-08-30 14:24:55 -07001173 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001174 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001175 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001176
1177 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001178 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1179 // Optimization: skip the combine step if there is nothing to do
1180 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1181 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1182 // any if len(jars) == 1.
1183 outputFile = moduleOutPath
1184 } else {
1185 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1186 ctx.Build(pctx, android.BuildParams{
1187 Rule: android.Cp,
1188 Input: jars[0],
1189 Output: combinedJar,
1190 })
1191 outputFile = combinedJar
1192 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001193 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001194 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001195 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001196 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001197 outputFile = combinedJar
1198 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001199
Colin Cross331a1212018-08-15 20:40:52 -07001200 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001201 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001202 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001203 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001204 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001205 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001206
1207 // jarjar resource jar if necessary
1208 if j.resourceJar != nil {
1209 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001210 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001211 j.resourceJar = resourceJarJarFile
1212 }
1213
Colin Cross0a6e0072017-08-30 14:24:55 -07001214 if ctx.Failed() {
1215 return
1216 }
1217 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001218
1219 // Check package restrictions if necessary.
1220 if len(j.properties.Permitted_packages) > 0 {
1221 // Check packages and copy to package-checked file.
1222 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1223 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1224 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1225
1226 if ctx.Failed() {
1227 return
1228 }
1229 }
1230
Nan Zhanged19fc32017-10-19 13:06:22 -07001231 j.implementationJarFile = outputFile
1232 if j.headerJarFile == nil {
1233 j.headerJarFile = j.implementationJarFile
1234 }
Colin Cross2fe66872015-03-30 17:20:39 -07001235
Colin Cross6510f912017-11-29 00:27:14 -08001236 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001237 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1238 j.properties.Instrument = true
1239 }
1240 }
1241
Colin Cross3144dfc2018-01-03 15:06:47 -08001242 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001243 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1244 }
1245
Colin Cross331a1212018-08-15 20:40:52 -07001246 // merge implementation jar with resources if necessary
1247 implementationAndResourcesJar := outputFile
1248 if j.resourceJar != nil {
1249 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1250 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1251 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1252 false, nil, nil)
1253 implementationAndResourcesJar = combinedJar
1254 }
1255
1256 j.implementationAndResourcesJar = implementationAndResourcesJar
1257
Colin Cross9ae1b922018-06-26 17:59:05 -07001258 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001259 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001260 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001261 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001262 if ctx.Failed() {
1263 return
1264 }
Colin Cross331a1212018-08-15 20:40:52 -07001265
Colin Cross8faf8fc2019-01-16 15:15:52 -08001266 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001267 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1268 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001269
Colin Cross331a1212018-08-15 20:40:52 -07001270 // merge dex jar with resources if necessary
1271 if j.resourceJar != nil {
1272 jars := android.Paths{dexOutputFile, j.resourceJar}
1273 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1274 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1275 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001276 if j.deviceProperties.UncompressDex {
1277 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1278 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1279 dexOutputFile = combinedAlignedJar
1280 } else {
1281 dexOutputFile = combinedJar
1282 }
Colin Cross331a1212018-08-15 20:40:52 -07001283 }
1284
1285 j.dexJarFile = dexOutputFile
1286
Colin Cross8faf8fc2019-01-16 15:15:52 -08001287 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001288 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1289
1290 j.maybeStrippedDexJarFile = dexOutputFile
1291
Colin Cross3063b782018-08-15 11:19:12 -07001292 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001293
1294 if ctx.Failed() {
1295 return
1296 }
Colin Cross331a1212018-08-15 20:40:52 -07001297 } else {
1298 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001299 }
Colin Cross331a1212018-08-15 20:40:52 -07001300
Colin Crossb7a63242015-04-16 14:09:14 -07001301 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001302
1303 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1304 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001305}
1306
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001307// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1308// since some of these flags may be used internally.
1309func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1310 for _, flag := range flags {
1311 flag = strings.TrimSpace(flag)
1312
1313 if !strings.HasPrefix(flag, "-") {
1314 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1315 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1316 ctx.PropertyErrorf("kotlincflags",
1317 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1318 } else if inList(flag, config.KotlincIllegalFlags) {
1319 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1320 } else if flag == "-include-runtime" {
1321 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1322 } else {
1323 args := strings.Split(flag, " ")
1324 if args[0] == "-kotlin-home" {
1325 ctx.PropertyErrorf("kotlincflags",
1326 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1327 }
1328 }
1329 }
1330}
1331
Colin Cross8eadbf02017-10-24 17:46:00 -07001332func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001333 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001334
1335 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001336 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001337 // Compile java sources into turbine.jar.
1338 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1339 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1340 if ctx.Failed() {
1341 return nil
1342 }
1343 jars = append(jars, turbineJar)
1344 }
1345
Colin Cross55f63ea2018-08-27 12:37:09 -07001346 jars = append(jars, extraJars...)
1347
Nan Zhanged19fc32017-10-19 13:06:22 -07001348 // Combine any static header libraries into classes-header.jar. If there is only
1349 // one input jar this step will be skipped.
1350 var headerJar android.Path
1351 jars = append(jars, deps.staticHeaderJars...)
1352
Colin Cross5c6ecc12017-10-23 18:12:27 -07001353 // we cannot skip the combine step for now if there is only one jar
1354 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1355 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001356 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1357 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001358 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001359
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001360 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001361 // Transform classes.jar into classes-jarjar.jar
1362 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001363 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001364 headerJar = jarjarFile
1365 if ctx.Failed() {
1366 return nil
1367 }
1368 }
1369
1370 return headerJar
1371}
1372
Colin Crosscb933592017-11-22 13:49:43 -08001373func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001374 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001375
Colin Cross7a3139e2017-12-19 13:57:50 -08001376 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001377
Colin Cross84c38822018-01-03 15:59:46 -08001378 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001379 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1380
1381 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1382
1383 j.jacocoReportClassesFile = jacocoReportClassesFile
1384
1385 return instrumentedJar
1386}
1387
albaltai36ff7dc2018-12-25 14:35:23 +08001388var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001389
Nan Zhanged19fc32017-10-19 13:06:22 -07001390func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001391 if j.headerJarFile == nil {
1392 return nil
1393 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001394 return android.Paths{j.headerJarFile}
1395}
1396
1397func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001398 if j.implementationJarFile == nil {
1399 return nil
1400 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001401 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001402}
1403
Colin Crossf24a22a2019-01-31 14:12:44 -08001404func (j *Module) DexJar() android.Path {
1405 return j.dexJarFile
1406}
1407
Colin Cross331a1212018-08-15 20:40:52 -07001408func (j *Module) ResourceJars() android.Paths {
1409 if j.resourceJar == nil {
1410 return nil
1411 }
1412 return android.Paths{j.resourceJar}
1413}
1414
1415func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001416 if j.implementationAndResourcesJar == nil {
1417 return nil
1418 }
Colin Cross331a1212018-08-15 20:40:52 -07001419 return android.Paths{j.implementationAndResourcesJar}
1420}
1421
Colin Cross46c9b8b2017-06-22 16:51:17 -07001422func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001423 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001424 return j.exportAidlIncludeDirs
1425}
1426
Jiyong Park1be96912018-05-28 18:02:19 +09001427func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001428 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001429 return j.exportedSdkLibs
1430}
1431
Colin Cross46c9b8b2017-06-22 16:51:17 -07001432var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001433
Colin Cross46c9b8b2017-06-22 16:51:17 -07001434func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001435 return j.logtagsSrcs
1436}
1437
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001438// Collect information for opening IDE project files in java/jdeps.go.
1439func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1440 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1441 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1442 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001443 if j.expandJarjarRules != nil {
1444 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001445 }
1446}
1447
1448func (j *Module) CompilerDeps() []string {
1449 jdeps := []string{}
1450 jdeps = append(jdeps, j.properties.Libs...)
1451 jdeps = append(jdeps, j.properties.Static_libs...)
1452 return jdeps
1453}
1454
Colin Cross2fe66872015-03-30 17:20:39 -07001455//
1456// Java libraries (.jar file)
1457//
1458
Colin Crossf506d872017-07-19 15:53:04 -07001459type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001460 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001461}
1462
Colin Cross42be7612019-02-21 18:12:14 -08001463func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001464 // Store uncompressed (and do not strip) dex files from boot class path jars.
1465 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1466 return true
1467 }
1468
1469 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001470 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001471 return true
1472 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001473 if ctx.Config().UncompressPrivAppDex() &&
1474 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1475 return true
1476 }
1477
Colin Cross2fc72f62018-12-21 12:59:54 -08001478 return false
1479}
1480
Colin Crossf506d872017-07-19 15:53:04 -07001481func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001482 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1483 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001484 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001485 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001486 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001487 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001488
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001489 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001490 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1491 ctx.ModuleName()+".jar", j.outputFile)
1492 }
Colin Crossb7a63242015-04-16 14:09:14 -07001493}
1494
Colin Crossf506d872017-07-19 15:53:04 -07001495func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001496 j.deps(ctx)
1497}
1498
Colin Cross1b16b0e2019-02-12 14:41:32 -08001499// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1500//
1501// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1502// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1503// as a `static_libs` dependency of another module.
1504//
1505// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1506// a device.
1507//
1508// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1509// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001510func LibraryFactory() android.Module {
1511 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001512
Colin Cross9ae1b922018-06-26 17:59:05 -07001513 module.AddProperties(
1514 &module.Module.properties,
1515 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001516 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001517 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001518
Colin Cross9ae1b922018-06-26 17:59:05 -07001519 InitJavaModule(module, android.HostAndDeviceSupported)
1520 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001521}
1522
Colin Cross1b16b0e2019-02-12 14:41:32 -08001523// java_library_static is an obsolete alias for java_library.
1524func LibraryStaticFactory() android.Module {
1525 return LibraryFactory()
1526}
1527
1528// java_library_host builds and links sources into a `.jar` file for the host.
1529//
1530// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1531// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001532func LibraryHostFactory() android.Module {
1533 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001534
Colin Cross6af17aa2017-09-20 12:59:05 -07001535 module.AddProperties(
1536 &module.Module.properties,
1537 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001538
Colin Cross9ae1b922018-06-26 17:59:05 -07001539 module.Module.properties.Installable = proptools.BoolPtr(true)
1540
Colin Cross89536d42017-07-07 14:35:50 -07001541 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001542 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001543}
1544
1545//
Colin Crossb628ea52018-08-14 16:42:33 -07001546// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001547//
1548
1549type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001550 // list of compatibility suites (for example "cts", "vts") that the module should be
1551 // installed into.
1552 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001553
1554 // the name of the test configuration (for example "AndroidTest.xml") that should be
1555 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001556 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001557
Jack He33338892018-09-19 02:21:28 -07001558 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1559 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001560 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001561
Colin Crossd96ca352018-08-10 16:06:24 -07001562 // list of files or filegroup modules that provide data that should be installed alongside
1563 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001564 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001565}
1566
Paul Duffin42df1442019-03-20 12:45:53 +00001567type testHelperLibraryProperties struct {
1568 // list of compatibility suites (for example "cts", "vts") that the module should be
1569 // installed into.
1570 Test_suites []string `android:"arch_variant"`
1571}
1572
Colin Cross05638fc2018-04-09 18:40:24 -07001573type Test struct {
1574 Library
1575
1576 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001577
1578 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001579 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001580}
1581
Paul Duffin42df1442019-03-20 12:45:53 +00001582type TestHelperLibrary struct {
1583 Library
1584
1585 testHelperLibraryProperties testHelperLibraryProperties
1586}
1587
Colin Cross303e21f2018-08-07 16:49:25 -07001588func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001589 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, j.testProperties.Test_suites)
Colin Cross8a497952019-03-05 22:25:09 -08001590 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001591
1592 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001593}
1594
Paul Duffin42df1442019-03-20 12:45:53 +00001595func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1596 j.Library.GenerateAndroidBuildActions(ctx)
1597}
1598
Colin Cross1b16b0e2019-02-12 14:41:32 -08001599// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1600// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1601//
1602// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1603// compiled against the device bootclasspath.
1604//
1605// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1606// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001607func TestFactory() android.Module {
1608 module := &Test{}
1609
1610 module.AddProperties(
1611 &module.Module.properties,
1612 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001613 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001614 &module.Module.protoProperties,
1615 &module.testProperties)
1616
Colin Cross9ae1b922018-06-26 17:59:05 -07001617 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001618 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001619
Colin Cross05638fc2018-04-09 18:40:24 -07001620 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001621 return module
1622}
1623
Paul Duffin42df1442019-03-20 12:45:53 +00001624// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1625func TestHelperLibraryFactory() android.Module {
1626 module := &TestHelperLibrary{}
1627
1628 module.AddProperties(
1629 &module.Module.properties,
1630 &module.Module.deviceProperties,
1631 &module.Module.dexpreoptProperties,
1632 &module.Module.protoProperties,
1633 &module.testHelperLibraryProperties)
1634
Colin Cross9a4abed2019-04-24 13:19:28 -07001635 module.Module.properties.Installable = proptools.BoolPtr(true)
1636 module.Module.dexpreopter.isTest = true
1637
Paul Duffin42df1442019-03-20 12:45:53 +00001638 InitJavaModule(module, android.HostAndDeviceSupported)
1639 return module
1640}
1641
Colin Cross1b16b0e2019-02-12 14:41:32 -08001642// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1643// allow running the test with `atest` or a `TEST_MAPPING` file.
1644//
1645// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1646// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001647func TestHostFactory() android.Module {
1648 module := &Test{}
1649
1650 module.AddProperties(
1651 &module.Module.properties,
1652 &module.Module.protoProperties,
1653 &module.testProperties)
1654
Colin Cross9ae1b922018-06-26 17:59:05 -07001655 module.Module.properties.Installable = proptools.BoolPtr(true)
1656
Colin Cross05638fc2018-04-09 18:40:24 -07001657 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001658 return module
1659}
1660
1661//
Colin Cross2fe66872015-03-30 17:20:39 -07001662// Java Binaries (.jar file plus wrapper script)
1663//
1664
Colin Crossf506d872017-07-19 15:53:04 -07001665type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001666 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001667 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001668
1669 // Name of the class containing main to be inserted into the manifest as Main-Class.
1670 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001671}
1672
Colin Crossf506d872017-07-19 15:53:04 -07001673type Binary struct {
1674 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001675
Colin Crossf506d872017-07-19 15:53:04 -07001676 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001677
Colin Cross6b4a32d2017-12-05 13:42:45 -08001678 isWrapperVariant bool
1679
Colin Crossc3315992017-12-08 19:12:36 -08001680 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001681 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001682}
1683
Alex Light24237172017-10-26 09:46:21 -07001684func (j *Binary) HostToolPath() android.OptionalPath {
1685 return android.OptionalPathForPath(j.binaryFile)
1686}
1687
Colin Crossf506d872017-07-19 15:53:04 -07001688func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001689 if ctx.Arch().ArchType == android.Common {
1690 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001691 if j.binaryProperties.Main_class != nil {
1692 if j.properties.Manifest != nil {
1693 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1694 }
1695 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1696 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1697 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1698 }
1699
Colin Cross6b4a32d2017-12-05 13:42:45 -08001700 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001701 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001702 // Handle the binary wrapper
1703 j.isWrapperVariant = true
1704
Colin Cross366938f2017-12-11 16:29:02 -08001705 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001706 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001707 } else {
1708 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1709 }
1710
1711 // Depend on the installed jar so that the wrapper doesn't get executed by
1712 // another build rule before the jar has been installed.
1713 jarFile := ctx.PrimaryModule().(*Binary).installFile
1714
1715 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1716 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001717 }
Colin Cross2fe66872015-03-30 17:20:39 -07001718}
1719
Colin Crossf506d872017-07-19 15:53:04 -07001720func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001721 if ctx.Arch().ArchType == android.Common {
1722 j.deps(ctx)
1723 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001724}
1725
Colin Cross1b16b0e2019-02-12 14:41:32 -08001726// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1727// as well.
1728//
1729// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1730// compiled against the device bootclasspath.
1731//
1732// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1733// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001734func BinaryFactory() android.Module {
1735 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001736
Colin Cross36242852017-06-23 15:06:31 -07001737 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001738 &module.Module.properties,
1739 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001740 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001741 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001742 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001743
Colin Cross9ae1b922018-06-26 17:59:05 -07001744 module.Module.properties.Installable = proptools.BoolPtr(true)
1745
Colin Cross6b4a32d2017-12-05 13:42:45 -08001746 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1747 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001748 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001749}
1750
Colin Cross1b16b0e2019-02-12 14:41:32 -08001751// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1752//
1753// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1754// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001755func BinaryHostFactory() android.Module {
1756 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001757
Colin Cross36242852017-06-23 15:06:31 -07001758 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001759 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001760 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001761 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001762
Colin Cross9ae1b922018-06-26 17:59:05 -07001763 module.Module.properties.Installable = proptools.BoolPtr(true)
1764
Colin Cross6b4a32d2017-12-05 13:42:45 -08001765 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1766 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001767 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001768}
1769
1770//
1771// Java prebuilts
1772//
1773
Colin Cross74d73e22017-08-02 11:05:49 -07001774type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001775 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001776
Nan Zhangea568a42017-11-08 21:20:04 -08001777 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001778
1779 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001780
1781 // List of shared java libs that this module has dependencies to
1782 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001783
1784 // List of files to remove from the jar file(s)
1785 Exclude_files []string
1786
1787 // List of directories to remove from the jar file(s)
1788 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001789
1790 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001791 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001792}
1793
1794type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001795 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001796 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001797 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001798
Colin Cross74d73e22017-08-02 11:05:49 -07001799 properties ImportProperties
1800
Colin Cross0a6e0072017-08-30 14:24:55 -07001801 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001802 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001803}
1804
Colin Cross83bb3162018-06-25 15:48:06 -07001805func (j *Import) sdkVersion() string {
1806 return String(j.properties.Sdk_version)
1807}
1808
1809func (j *Import) minSdkVersion() string {
1810 return j.sdkVersion()
1811}
1812
Colin Cross74d73e22017-08-02 11:05:49 -07001813func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001814 return &j.prebuilt
1815}
1816
Colin Cross74d73e22017-08-02 11:05:49 -07001817func (j *Import) PrebuiltSrcs() []string {
1818 return j.properties.Jars
1819}
1820
1821func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001822 return j.prebuilt.Name(j.ModuleBase.Name())
1823}
1824
Colin Cross74d73e22017-08-02 11:05:49 -07001825func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001826 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001827}
1828
Colin Cross74d73e22017-08-02 11:05:49 -07001829func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001830 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001831
Nan Zhang4c819fb2018-08-27 18:31:46 -07001832 jarName := ctx.ModuleName() + ".jar"
1833 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001834 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1835 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001836 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001837 inputFile := outputFile
1838 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1839 TransformJetifier(ctx, outputFile, inputFile)
1840 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001841 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001842
1843 ctx.VisitDirectDeps(func(module android.Module) {
1844 otherName := ctx.OtherModuleName(module)
1845 tag := ctx.OtherModuleDependencyTag(module)
1846
1847 switch dep := module.(type) {
1848 case Dependency:
1849 switch tag {
1850 case libTag, staticLibTag:
1851 // sdk lib names from dependencies are re-exported
1852 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1853 }
1854 case SdkLibraryDependency:
1855 switch tag {
1856 case libTag:
1857 // names of sdk libs that are directly depended are exported
1858 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1859 }
1860 }
1861 })
1862
1863 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001864 if Bool(j.properties.Installable) {
1865 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1866 ctx.ModuleName()+".jar", outputFile)
1867 }
Colin Cross2fe66872015-03-30 17:20:39 -07001868}
1869
Colin Cross74d73e22017-08-02 11:05:49 -07001870var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001871
Nan Zhanged19fc32017-10-19 13:06:22 -07001872func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001873 if j.combinedClasspathFile == nil {
1874 return nil
1875 }
Colin Cross37f6d792018-07-12 12:28:41 -07001876 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001877}
1878
1879func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001880 if j.combinedClasspathFile == nil {
1881 return nil
1882 }
Colin Cross37f6d792018-07-12 12:28:41 -07001883 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001884}
1885
Colin Cross331a1212018-08-15 20:40:52 -07001886func (j *Import) ResourceJars() android.Paths {
1887 return nil
1888}
1889
1890func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001891 if j.combinedClasspathFile == nil {
1892 return nil
1893 }
Colin Cross331a1212018-08-15 20:40:52 -07001894 return android.Paths{j.combinedClasspathFile}
1895}
1896
Colin Crossf24a22a2019-01-31 14:12:44 -08001897func (j *Import) DexJar() android.Path {
1898 return nil
1899}
1900
Colin Cross74d73e22017-08-02 11:05:49 -07001901func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001902 return nil
1903}
1904
Jiyong Park1be96912018-05-28 18:02:19 +09001905func (j *Import) ExportedSdkLibs() []string {
1906 return j.exportedSdkLibs
1907}
1908
albaltai36ff7dc2018-12-25 14:35:23 +08001909// Add compile time check for interface implementation
1910var _ android.IDEInfo = (*Import)(nil)
1911var _ android.IDECustomizedModuleName = (*Import)(nil)
1912
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001913// Collect information for opening IDE project files in java/jdeps.go.
1914const (
1915 removedPrefix = "prebuilt_"
1916)
1917
1918func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1919 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1920}
1921
1922func (j *Import) IDECustomizedModuleName() string {
1923 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1924 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1925 // solution to get the Import name.
1926 name := j.Name()
1927 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001928 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001929 }
1930 return name
1931}
1932
Colin Cross74d73e22017-08-02 11:05:49 -07001933var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001934
Colin Cross1b16b0e2019-02-12 14:41:32 -08001935// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1936//
1937// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1938// compiled against an Android classpath.
1939//
1940// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1941// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001942func ImportFactory() android.Module {
1943 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001944
Colin Cross74d73e22017-08-02 11:05:49 -07001945 module.AddProperties(&module.properties)
1946
1947 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001948 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001949 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001950}
1951
Colin Cross1b16b0e2019-02-12 14:41:32 -08001952// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1953// module.
1954//
1955// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1956// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001957func ImportFactoryHost() android.Module {
1958 module := &Import{}
1959
1960 module.AddProperties(&module.properties)
1961
1962 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001963 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001964 return module
1965}
1966
Colin Cross42be7612019-02-21 18:12:14 -08001967// dex_import module
1968
1969type DexImportProperties struct {
1970 Jars []string
1971}
1972
1973type DexImport struct {
1974 android.ModuleBase
1975 android.DefaultableModuleBase
1976 prebuilt android.Prebuilt
1977
1978 properties DexImportProperties
1979
1980 dexJarFile android.Path
1981 maybeStrippedDexJarFile android.Path
1982
1983 dexpreopter
1984}
1985
1986func (j *DexImport) Prebuilt() *android.Prebuilt {
1987 return &j.prebuilt
1988}
1989
1990func (j *DexImport) PrebuiltSrcs() []string {
1991 return j.properties.Jars
1992}
1993
1994func (j *DexImport) Name() string {
1995 return j.prebuilt.Name(j.ModuleBase.Name())
1996}
1997
1998func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1999 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2000}
2001
2002func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2003 if len(j.properties.Jars) != 1 {
2004 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2005 }
2006
2007 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2008 j.dexpreopter.isInstallable = true
2009 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2010
2011 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2012 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2013
2014 if j.dexpreopter.uncompressedDex {
2015 rule := android.NewRuleBuilder()
2016
2017 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2018 rule.Temporary(temporary)
2019
2020 // use zip2zip to uncompress classes*.dex files
2021 rule.Command().
2022 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2023 FlagWithInput("-i ", inputJar).
2024 FlagWithOutput("-o ", temporary).
2025 FlagWithArg("-0 ", "'classes*.dex'")
2026
2027 // use zipalign to align uncompressed classes*.dex files
2028 rule.Command().
2029 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2030 Flag("-f").
2031 Text("4").
2032 Input(temporary).
2033 Output(dexOutputFile)
2034
2035 rule.DeleteTemporaryFiles()
2036
2037 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2038 } else {
2039 ctx.Build(pctx, android.BuildParams{
2040 Rule: android.Cp,
2041 Input: inputJar,
2042 Output: dexOutputFile,
2043 })
2044 }
2045
2046 j.dexJarFile = dexOutputFile
2047
2048 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2049
2050 j.maybeStrippedDexJarFile = dexOutputFile
2051
2052 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2053 ctx.ModuleName()+".jar", dexOutputFile)
2054}
2055
2056func (j *DexImport) DexJar() android.Path {
2057 return j.dexJarFile
2058}
2059
2060// dex_import imports a `.jar` file containing classes.dex files.
2061//
2062// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2063// to the device.
2064func DexImportFactory() android.Module {
2065 module := &DexImport{}
2066
2067 module.AddProperties(&module.properties)
2068
2069 android.InitPrebuiltModule(module, &module.properties.Jars)
2070 InitJavaModule(module, android.DeviceSupported)
2071 return module
2072}
2073
Colin Cross89536d42017-07-07 14:35:50 -07002074//
2075// Defaults
2076//
2077type Defaults struct {
2078 android.ModuleBase
2079 android.DefaultsModuleBase
2080}
2081
2082func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2083}
2084
Colin Cross1b16b0e2019-02-12 14:41:32 -08002085// java_defaults provides a set of properties that can be inherited by other java or android modules.
2086//
2087// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2088// property in the defaults module that exists in the depending module will be prepended to the depending module's
2089// value for that property.
2090//
2091// Example:
2092//
2093// java_defaults {
2094// name: "example_defaults",
2095// srcs: ["common/**/*.java"],
2096// javacflags: ["-Xlint:all"],
2097// aaptflags: ["--auto-add-overlay"],
2098// }
2099//
2100// java_library {
2101// name: "example",
2102// defaults: ["example_defaults"],
2103// srcs: ["example/**/*.java"],
2104// }
2105//
2106// is functionally identical to:
2107//
2108// java_library {
2109// name: "example",
2110// srcs: [
2111// "common/**/*.java",
2112// "example/**/*.java",
2113// ],
2114// javacflags: ["-Xlint:all"],
2115// }
Colin Cross89536d42017-07-07 14:35:50 -07002116func defaultsFactory() android.Module {
2117 return DefaultsFactory()
2118}
2119
2120func DefaultsFactory(props ...interface{}) android.Module {
2121 module := &Defaults{}
2122
2123 module.AddProperties(props...)
2124 module.AddProperties(
2125 &CompilerProperties{},
2126 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002127 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002128 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002129 &aaptProperties{},
2130 &androidLibraryProperties{},
2131 &appProperties{},
2132 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002133 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002134 &ImportProperties{},
2135 &AARImportProperties{},
2136 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002137 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002138 )
2139
2140 android.InitDefaultsModule(module)
2141
2142 return module
2143}
Nan Zhangea568a42017-11-08 21:20:04 -08002144
2145var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002146var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002147var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002148var inList = android.InList