blob: 84518a12e49de4e798f4438a4b8929e79f77a40a [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
Colin Cross7f87f4f2019-04-24 13:41:45 -0700224 Target struct {
225 Hostdex struct {
226 // Additional required dependencies to add to -hostdex modules.
227 Required []string
228 }
229 }
230
David Brazdil17ef5632018-06-27 10:27:45 +0100231 // If set to true, compile dex regardless of installable. Defaults to false.
232 Compile_dex *bool
233
Colin Cross66dbc0b2017-12-28 12:23:20 -0800234 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700235 // If false, disable all optimization. Defaults to true for android_app and android_test
236 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800237 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700238 // True if the module containing this has it set by default.
239 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800240
241 // If true, optimize for size by removing unused code. Defaults to true for apps,
242 // false for libraries and tests.
243 Shrink *bool
244
245 // If true, optimize bytecode. Defaults to false.
246 Optimize *bool
247
248 // If true, obfuscate bytecode. Defaults to false.
249 Obfuscate *bool
250
251 // If true, do not use the flag files generated by aapt that automatically keep
252 // classes referenced by the app manifest. Defaults to false.
253 No_aapt_flags *bool
254
255 // Flags to pass to proguard.
256 Proguard_flags []string
257
258 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800259 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800260 }
261
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 // When targeting 1.9, override the modules to use with --system
263 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700264
265 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800266 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700267}
268
Sasha Smundak2057f822019-04-16 17:16:58 -0700269func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
270 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
271}
272
Colin Cross46c9b8b2017-06-22 16:51:17 -0700273// Module contains the properties and members used by all java module types
274type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700275 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700276 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700277
Colin Cross89536d42017-07-07 14:35:50 -0700278 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700279 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700280 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700281
Colin Cross331a1212018-08-15 20:40:52 -0700282 // jar file containing header classes including static library dependencies, suitable for
283 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 headerJarFile android.Path
285
Colin Cross331a1212018-08-15 20:40:52 -0700286 // jar file containing implementation classes including static library dependencies but no
287 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700289
Colin Cross331a1212018-08-15 20:40:52 -0700290 // jar file containing only resources including from static library dependencies
291 resourceJar android.Path
292
Colin Cross0c4ce212019-05-03 15:28:19 -0700293 // args and dependencies to package source files into a srcjar
294 srcJarArgs []string
295 srcJarDeps android.Paths
296
Colin Cross331a1212018-08-15 20:40:52 -0700297 // jar file containing implementation classes and resources including static library
298 // dependencies
299 implementationAndResourcesJar android.Path
300
301 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700302 dexJarFile android.Path
303
Colin Cross43f08db2018-11-12 10:13:39 -0800304 // output file that contains classes.dex if it should be in the output file
305 maybeStrippedDexJarFile android.Path
306
Colin Crosscb933592017-11-22 13:49:43 -0800307 // output file containing uninstrumented classes that will be instrumented by jacoco
308 jacocoReportClassesFile android.Path
309
Colin Cross66dbc0b2017-12-28 12:23:20 -0800310 // output file containing mapping of obfuscated names
311 proguardDictionary android.Path
312
Colin Cross331a1212018-08-15 20:40:52 -0700313 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700314 outputFile android.Path
315 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700316
Colin Cross635c3b02016-05-18 15:37:25 -0700317 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700318
Colin Cross635c3b02016-05-18 15:37:25 -0700319 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700320
Colin Cross2fe66872015-03-30 17:20:39 -0700321 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700322 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800323
324 // list of .java files and srcjars that was passed to javac
325 compiledJavaSrcs android.Paths
326 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800327
328 // list of extra progurad flag files
329 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900330
Colin Cross094054a2018-10-17 15:10:48 -0700331 // manifest file to use instead of properties.Manifest
332 overrideManifest android.OptionalPath
333
Jiyong Park1be96912018-05-28 18:02:19 +0900334 // list of SDK lib names that this java moudule is exporting
335 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700336
337 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
338 // filter out Exclude_srcs, will be used by android.IDEInfo struct
339 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800340
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800341 // expanded Jarjar_rules
342 expandJarjarRules android.Path
343
Vladimir Marko0975ee02019-04-02 10:29:55 +0100344 // list of additional targets for checkbuild
345 additionalCheckedModules android.Paths
346
Colin Cross988708c2019-05-06 14:04:11 -0700347 // Extra files generated by the module type to be added as java resources.
348 extraResources android.Paths
349
Colin Crossf24a22a2019-01-31 14:12:44 -0800350 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800351 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700352}
353
Colin Cross54250902017-12-05 09:28:08 -0800354func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700355 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800356}
357
Jiyong Park8fd61922018-11-08 02:50:25 +0900358func (j *Module) DexJarFile() android.Path {
359 return j.dexJarFile
360}
361
Colin Cross54250902017-12-05 09:28:08 -0800362var _ android.SourceFileProducer = (*Module)(nil)
363
Colin Crossf506d872017-07-19 15:53:04 -0700364type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700365 HeaderJars() android.Paths
366 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700367 ResourceJars() android.Paths
368 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800369 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700370 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900371 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700372 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700373}
374
Jiyong Parkc678ad32018-04-10 13:07:10 +0900375type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800376 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
377 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378}
379
Nan Zhangb2b33de2018-02-23 11:18:47 -0800380type SrcDependency interface {
381 CompiledSrcs() android.Paths
382 CompiledSrcJars() android.Paths
383}
384
385func (j *Module) CompiledSrcs() android.Paths {
386 return j.compiledJavaSrcs
387}
388
389func (j *Module) CompiledSrcJars() android.Paths {
390 return j.compiledSrcJars
391}
392
393var _ SrcDependency = (*Module)(nil)
394
Colin Cross89536d42017-07-07 14:35:50 -0700395func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
396 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
397 android.InitDefaultableModule(module)
398}
399
Colin Crossbe1da472017-07-07 15:59:46 -0700400type dependencyTag struct {
401 blueprint.BaseDependencyTag
402 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700403}
404
Colin Crossa4f08812018-10-02 22:03:40 -0700405type jniDependencyTag struct {
406 blueprint.BaseDependencyTag
407 target android.Target
408}
409
Colin Crossbe1da472017-07-07 15:59:46 -0700410var (
Colin Cross4b964c02018-10-15 16:18:06 -0700411 staticLibTag = dependencyTag{name: "staticlib"}
412 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800413 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700414 bootClasspathTag = dependencyTag{name: "bootclasspath"}
415 systemModulesTag = dependencyTag{name: "system modules"}
416 frameworkResTag = dependencyTag{name: "framework-res"}
417 frameworkApkTag = dependencyTag{name: "framework-apk"}
418 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800419 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700420 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
421 certificateTag = dependencyTag{name: "certificate"}
422 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700423 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700424)
Colin Cross2fe66872015-03-30 17:20:39 -0700425
Colin Crossfc3674a2017-09-18 17:41:52 -0700426type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700427 useModule, useFiles, useDefaultLibs, invalidVersion bool
428
Colin Cross86a60ae2018-05-29 14:44:55 -0700429 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700430 systemModules string
431
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 frameworkResModule string
433
Colin Cross86a60ae2018-05-29 14:44:55 -0700434 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700435 aidl android.OptionalPath
Colin Cross1369cdb2017-09-29 17:58:17 -0700436}
437
Colin Crossa4f08812018-10-02 22:03:40 -0700438type jniLib struct {
439 name string
440 path android.Path
441 target android.Target
442}
443
Colin Cross3144dfc2018-01-03 15:06:47 -0800444func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
445 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
446}
447
448func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
449 return j.shouldInstrument(ctx) &&
450 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
451 ctx.Config().UnbundledBuild())
452}
453
Colin Cross83bb3162018-06-25 15:48:06 -0700454func (j *Module) sdkVersion() string {
455 return String(j.deviceProperties.Sdk_version)
456}
457
458func (j *Module) minSdkVersion() string {
459 if j.deviceProperties.Min_sdk_version != nil {
460 return *j.deviceProperties.Min_sdk_version
461 }
462 return j.sdkVersion()
463}
464
Dan Willemsen419290a2018-10-31 15:28:47 -0700465func (j *Module) targetSdkVersion() string {
466 if j.deviceProperties.Target_sdk_version != nil {
467 return *j.deviceProperties.Target_sdk_version
468 }
469 return j.sdkVersion()
470}
471
Colin Crossbe1da472017-07-07 15:59:46 -0700472func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700473 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700474 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700475 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700476 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700477 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100478 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700479 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700480 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700481 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700482 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100483 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700484 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700485 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700486 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
487 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800488 }
Colin Crossbe1da472017-07-07 15:59:46 -0700489 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700490 } else if j.deviceProperties.System_modules == nil {
491 ctx.PropertyErrorf("no_standard_libs",
492 "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 +0100493 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700494 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700495 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100496 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700497 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800498 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800499 if ctx.ModuleName() == "android_stubs_current" ||
500 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700501 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700502 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800503 }
Colin Cross2fe66872015-03-30 17:20:39 -0700504 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700505
Colin Cross42d48b72018-08-29 14:10:52 -0700506 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
507 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700508
Colin Crossbe9cdb82019-01-21 21:37:16 -0800509 ctx.AddFarVariationDependencies([]blueprint.Variation{
510 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
511 }, pluginTag, j.properties.Plugins...)
512
Colin Crossfe17f6f2019-03-28 19:30:56 -0700513 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700514 if j.hasSrcExt(".proto") {
515 protoDeps(ctx, &j.protoProperties)
516 }
Colin Cross93e85952017-08-15 13:34:18 -0700517
518 if j.hasSrcExt(".kt") {
519 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
520 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700521 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
522 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800523 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800524 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
525 }
Colin Cross93e85952017-08-15 13:34:18 -0700526 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800527
528 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700529 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800530 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700531}
532
533func hasSrcExt(srcs []string, ext string) bool {
534 for _, src := range srcs {
535 if filepath.Ext(src) == ext {
536 return true
537 }
538 }
539
540 return false
541}
542
Nan Zhang61eaedb2017-11-02 13:28:15 -0700543func shardPaths(paths android.Paths, shardSize int) []android.Paths {
544 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
545 for len(paths) > shardSize {
546 ret = append(ret, paths[0:shardSize])
547 paths = paths[shardSize:]
548 }
549 if len(paths) > 0 {
550 ret = append(ret, paths)
551 }
552 return ret
553}
554
Colin Cross6af17aa2017-09-20 12:59:05 -0700555func (j *Module) hasSrcExt(ext string) bool {
556 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700557}
558
Colin Cross46c9b8b2017-06-22 16:51:17 -0700559func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700560 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700561
Colin Crossebe1a512017-11-14 13:12:14 -0800562 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
563 aidlIncludes = append(aidlIncludes,
564 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
565 aidlIncludes = append(aidlIncludes,
566 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700567
Colin Cross3047fa22019-04-18 10:56:44 -0700568 var flags []string
569 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700570
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700571 if aidlPreprocess.Valid() {
572 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700573 deps = append(deps, aidlPreprocess.Path())
574 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700575 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700576 }
577
Colin Cross3047fa22019-04-18 10:56:44 -0700578 if len(j.exportAidlIncludeDirs) > 0 {
579 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
580 }
581
582 if len(aidlIncludes) > 0 {
583 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
584 }
585
Colin Cross635c3b02016-05-18 15:37:25 -0700586 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800587 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700588 flags = append(flags, "-I"+src.String())
589 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700590
Martijn Coeneneab15642018-03-09 09:29:59 +0100591 if Bool(j.deviceProperties.Aidl.Generate_traces) {
592 flags = append(flags, "-t")
593 }
594
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100595 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
596 flags = append(flags, "--transaction_names")
597 }
598
Colin Cross3047fa22019-04-18 10:56:44 -0700599 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700600}
601
Colin Cross32f676a2017-09-06 13:41:06 -0700602type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800603 classpath classpath
604 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700605 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800606 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700607 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700608 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700609 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700610 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800611 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700612 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700613 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700614 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700615 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800616 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800617
618 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700619}
Colin Cross2fe66872015-03-30 17:20:39 -0700620
Colin Cross54250902017-12-05 09:28:08 -0800621func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
622 for _, f := range dep.Srcs() {
623 if f.Ext() != ".jar" {
624 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
625 ctx.OtherModuleName(dep.(blueprint.Module)))
626 }
627 }
628}
629
Jiyong Park2d492942018-03-05 17:44:10 +0900630type linkType int
631
632const (
633 javaCore linkType = iota
634 javaSdk
635 javaSystem
636 javaPlatform
637)
638
Jiyong Park46f78fb2018-10-20 16:33:17 +0900639func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700640 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700641 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900642 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
643 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100644 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900645 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100646 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900647 return javaCore, false
648 case name == "android_system_stubs_current":
649 return javaSystem, true
650 case strings.HasPrefix(ver, "system_"):
651 return javaSystem, false
652 case name == "android_test_stubs_current":
653 return javaSystem, true
654 case strings.HasPrefix(ver, "test_"):
655 return javaPlatform, false
656 case name == "android_stubs_current":
657 return javaSdk, true
658 case ver == "current":
659 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700660 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900661 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700662 default:
663 if _, err := strconv.Atoi(ver); err != nil {
664 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
665 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900666 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900667 }
668}
669
Jiyong Park750e5572018-01-31 00:20:13 +0900670func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700671 if ctx.Host() {
672 return
673 }
674
Jiyong Park46f78fb2018-10-20 16:33:17 +0900675 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
676 if stubs {
677 return
678 }
679 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900680 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."
681
682 switch myLinkType {
683 case javaCore:
684 if otherLinkType != javaCore {
685 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 +0900686 ctx.OtherModuleName(to))
687 }
Jiyong Park2d492942018-03-05 17:44:10 +0900688 break
689 case javaSdk:
690 if otherLinkType != javaCore && otherLinkType != javaSdk {
691 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
692 ctx.OtherModuleName(to))
693 }
694 break
695 case javaSystem:
696 if otherLinkType == javaPlatform {
697 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
698 ctx.OtherModuleName(to))
699 }
700 break
701 case javaPlatform:
702 // no restriction on link-type
703 break
Jiyong Park750e5572018-01-31 00:20:13 +0900704 }
705}
706
Colin Cross32f676a2017-09-06 13:41:06 -0700707func (j *Module) collectDeps(ctx android.ModuleContext) deps {
708 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700709
Colin Cross300f0382018-03-06 13:11:51 -0800710 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700711 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800712 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700713 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800714 } else if sdkDep.useFiles {
715 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700716 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700717 deps.aidlPreprocess = sdkDep.aidl
718 } else {
719 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800720 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700721 }
722
Colin Crossd11fcda2017-10-23 17:59:01 -0700723 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700724 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700725 tag := ctx.OtherModuleDependencyTag(module)
726
Colin Crossa4f08812018-10-02 22:03:40 -0700727 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700728 // Handled by AndroidApp.collectAppDeps
729 return
730 }
731 if tag == certificateTag {
732 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700733 return
734 }
735
Jiyong Park750e5572018-01-31 00:20:13 +0900736 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700737 switch tag {
738 case bootClasspathTag, libTag, staticLibTag:
739 checkLinkType(ctx, j, to, tag.(dependencyTag))
740 }
Jiyong Park750e5572018-01-31 00:20:13 +0900741 }
Colin Cross54250902017-12-05 09:28:08 -0800742 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800743 case SdkLibraryDependency:
744 switch tag {
745 case libTag:
746 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
747 // names of sdk libs that are directly depended are exported
748 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700749 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800750 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
751 }
Colin Cross54250902017-12-05 09:28:08 -0800752 case Dependency:
753 switch tag {
754 case bootClasspathTag:
755 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700756 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800757 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900758 // sdk lib names from dependencies are re-exported
759 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700760 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800761 case staticLibTag:
762 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
763 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
764 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700765 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900766 // sdk lib names from dependencies are re-exported
767 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700768 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800769 case pluginTag:
770 if plugin, ok := dep.(*Plugin); ok {
771 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
772 if plugin.pluginProperties.Processor_class != nil {
773 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
774 }
775 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
776 } else {
777 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
778 }
Colin Cross54250902017-12-05 09:28:08 -0800779 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100780 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800781 // framework.jar has a one-off dependency on the R.java and Manifest.java files
782 // generated by framework-res.apk
783 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
784 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800785 case frameworkApkTag:
786 if ctx.ModuleName() == "android_stubs_current" ||
787 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700788 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800789 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
790 // resource files out of there for aapt.
791 //
792 // Normally the package rule runs aapt, which includes the resource,
793 // but we're not running that in our package rule so just copy in the
794 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700795 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800796 }
Colin Cross54250902017-12-05 09:28:08 -0800797 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700798 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800799 case kotlinAnnotationsTag:
800 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800801 }
802
Colin Cross54250902017-12-05 09:28:08 -0800803 case android.SourceFileProducer:
804 switch tag {
805 case libTag:
806 checkProducesJars(ctx, dep)
807 deps.classpath = append(deps.classpath, dep.Srcs()...)
808 case staticLibTag:
809 checkProducesJars(ctx, dep)
810 deps.classpath = append(deps.classpath, dep.Srcs()...)
811 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
812 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800813 }
814 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700815 switch tag {
816 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700817 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700818 case systemModulesTag:
819 if deps.systemModules != nil {
820 panic("Found two system module dependencies")
821 }
822 sm := module.(*SystemModules)
823 if sm.outputFile == nil {
824 panic("Missing directory for system module dependency")
825 }
826 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700827 default:
828 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700829 }
Colin Crossec7a0422017-07-07 14:47:12 -0700830 }
Colin Cross2fe66872015-03-30 17:20:39 -0700831 })
832
Jiyong Park1be96912018-05-28 18:02:19 +0900833 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
834
Colin Cross32f676a2017-09-06 13:41:06 -0700835 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700836}
837
Colin Cross83bb3162018-06-25 15:48:06 -0700838func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700839 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800840 v := sdkContext.sdkVersion()
841 // For PDK builds, use the latest SDK version instead of "current"
842 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700843 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800844 latestSdkVersion := 0
845 if len(sdkVersions) > 0 {
846 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
847 }
848 v = strconv.Itoa(latestSdkVersion)
849 }
850
851 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700852 if err != nil {
853 ctx.PropertyErrorf("sdk_version", "%s", err)
854 }
Nan Zhang357466b2018-04-17 17:38:36 -0700855 if javaVersion != "" {
856 ret = javaVersion
857 } else if ctx.Device() && sdk <= 23 {
858 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100859 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700860 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700861 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700862 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
863 ret = "1.8"
864 } else {
865 ret = "1.9"
866 }
867
868 return ret
869}
870
Nan Zhanged19fc32017-10-19 13:06:22 -0700871func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700872
Colin Crossf03c82b2015-04-13 13:53:40 -0700873 var flags javaBuilderFlags
874
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100875 // javaVersion flag.
876 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
877
Nan Zhanged19fc32017-10-19 13:06:22 -0700878 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700879 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100880 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700881 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700882 }
Colin Cross6510f912017-11-29 00:27:14 -0800883 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700884 // Override the -g flag passed globally to remove local variable debug info to reduce
885 // disk and memory usage.
886 javacFlags = append(javacFlags, "-g:source,lines")
887 }
Colin Cross64162712017-08-08 13:17:59 -0700888
Colin Cross66548102018-06-19 22:47:35 -0700889 if ctx.Config().RunErrorProne() {
890 if config.ErrorProneClasspath == nil {
891 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
892 }
893
894 errorProneFlags := []string{
895 "-Xplugin:ErrorProne",
896 "${config.ErrorProneChecks}",
897 }
898 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
899
900 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
901 "'" + strings.Join(errorProneFlags, " ") + "'"
902 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800903 }
904
Nan Zhanged19fc32017-10-19 13:06:22 -0700905 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800906 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
907 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700908 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800909
Colin Crossbe9cdb82019-01-21 21:37:16 -0800910 flags.processor = strings.Join(deps.processorClasses, ",")
911
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100912 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800913 !Bool(j.properties.No_standard_libs) &&
914 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
915 // Give host-side tools a version of OpenJDK's standard libraries
916 // close to what they're targeting. As of Dec 2017, AOSP is only
917 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
918 //
919 // When building with OpenJDK 8, the following should have no
920 // effect since those jars would be available by default.
921 //
922 // When building with OpenJDK 9 but targeting a version < 1.8,
923 // putting them on the bootclasspath means that:
924 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
925 // b) references to existing APIs are not reinterpreted in an
926 // OpenJDK 9-specific way, eg. calls to subclasses of
927 // java.nio.Buffer as in http://b/70862583
928 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
929 flags.bootClasspath = append(flags.bootClasspath,
930 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
931 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800932 if Bool(j.properties.Use_tools_jar) {
933 flags.bootClasspath = append(flags.bootClasspath,
934 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
935 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800936 }
937
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100938 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800939 // Manually specify build directory in case it is not under the repo root.
940 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
941 // just adding a symlink under the root doesn't help.)
942 patchPaths := ".:" + ctx.Config().BuildDir()
943 classPath := flags.classpath.FormJavaClassPath("")
944 if classPath != "" {
945 patchPaths += ":" + classPath
946 }
947 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700948 }
949
Nan Zhanged19fc32017-10-19 13:06:22 -0700950 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700951 if deps.systemModules != nil {
952 flags.systemModules = append(flags.systemModules, deps.systemModules)
953 }
954
Nan Zhanged19fc32017-10-19 13:06:22 -0700955 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700956 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700957
Colin Cross81440082018-08-15 20:21:55 -0700958 if len(javacFlags) > 0 {
959 // optimization.
960 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
961 flags.javacFlags = "$javacFlags"
962 }
963
Nan Zhanged19fc32017-10-19 13:06:22 -0700964 return flags
965}
Colin Crossc0b06f12015-04-08 13:03:43 -0700966
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700967func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
968
969 hasSrcs := false
Nan Zhanged19fc32017-10-19 13:06:22 -0700970
Colin Crossebe1a512017-11-14 13:12:14 -0800971 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700972
973 deps := j.collectDeps(ctx)
974 flags := j.collectBuilderFlags(ctx, deps)
975
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100976 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700977 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
978 }
Colin Cross8a497952019-03-05 22:25:09 -0800979 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700980 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800981 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700982 }
983
Colin Crossaf050172017-11-15 23:01:59 -0800984 srcFiles = j.genSources(ctx, srcFiles, flags)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700985 if len(srcFiles) > 0 {
986 hasSrcs = true
987 }
Colin Crossaf050172017-11-15 23:01:59 -0800988
989 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700990 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700991 if aaptSrcJar != nil {
992 srcJars = append(srcJars, aaptSrcJar)
993 }
Colin Crossb7a63242015-04-16 14:09:14 -0700994
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700995 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
996 // that IDEInfo struct will use
997 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
998
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800999 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001000 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001001 }
1002
Colin Cross1ee23172017-10-18 14:44:18 -07001003 jarName := ctx.ModuleName() + ".jar"
1004
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001005 javaSrcFiles := srcFiles.FilterByExt(".java")
1006 var uniqueSrcFiles android.Paths
1007 set := make(map[string]bool)
1008 for _, v := range javaSrcFiles {
1009 if _, found := set[v.String()]; !found {
1010 set[v.String()] = true
1011 uniqueSrcFiles = append(uniqueSrcFiles, v)
1012 }
1013 }
1014
Colin Cross55f63ea2018-08-27 12:37:09 -07001015 var kotlinJars android.Paths
1016
Colin Cross93e85952017-08-15 13:34:18 -07001017 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001018 // user defined kotlin flags.
1019 kotlincFlags := j.properties.Kotlincflags
1020 CheckKotlincFlags(ctx, kotlincFlags)
1021
Colin Cross93e85952017-08-15 13:34:18 -07001022 // If there are kotlin files, compile them first but pass all the kotlin and java files
1023 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1024 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001025 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001026 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001027 kotlincFlags = append(kotlincFlags, "-no-jdk")
1028 }
1029 if len(kotlincFlags) > 0 {
1030 // optimization.
1031 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1032 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001033 }
1034
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001035 var kotlinSrcFiles android.Paths
1036 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1037 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1038
Colin Crossafbb1732019-01-17 15:42:52 -08001039 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1040 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1041
1042 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1043 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1044
1045 if len(flags.processorPath) > 0 {
1046 // Use kapt for annotation processing
1047 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1048 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1049 srcJars = append(srcJars, kaptSrcJar)
1050 // Disable annotation processing in javac, it's already been handled by kapt
1051 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001052 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001053 }
Colin Cross93e85952017-08-15 13:34:18 -07001054
Colin Cross1ee23172017-10-18 14:44:18 -07001055 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001056 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001057 if ctx.Failed() {
1058 return
1059 }
1060
1061 // Make javac rule depend on the kotlinc rule
1062 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001063
Colin Cross93e85952017-08-15 13:34:18 -07001064 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001065 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001066 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001067 }
1068
Colin Cross55f63ea2018-08-27 12:37:09 -07001069 jars := append(android.Paths(nil), kotlinJars...)
1070
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001071 // Store the list of .java files that was passed to javac
1072 j.compiledJavaSrcs = uniqueSrcFiles
1073 j.compiledSrcJars = srcJars
1074
Nan Zhang61eaedb2017-11-02 13:28:15 -07001075 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001076 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001077 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1078 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001079 // Formerly, there was a check here that prevented annotation processors
1080 // from being used when sharding was enabled, as some annotation processors
1081 // do not function correctly in sharded environments. It was removed to
1082 // allow for the use of annotation processors that do function correctly
1083 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001084 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001085 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001086 if ctx.Failed() {
1087 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001088 }
1089 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001090 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001091 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001092 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001093 // If error-prone is enabled, add an additional rule to compile the java files into
1094 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001095 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001096 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1097 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001098 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001099 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001100 extraJarDeps = append(extraJarDeps, errorprone)
1101 }
1102
Nan Zhang61eaedb2017-11-02 13:28:15 -07001103 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001104 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001105 shardSize := int(*(j.properties.Javac_shard_size))
1106 var shardSrcs []android.Paths
1107 if len(uniqueSrcFiles) > 0 {
1108 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1109 for idx, shardSrc := range shardSrcs {
1110 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1111 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1112 jars = append(jars, classes)
1113 }
1114 }
1115 if len(srcJars) > 0 {
1116 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1117 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1118 jars = append(jars, classes)
1119 }
1120 } else {
1121 classes := android.PathForModuleOut(ctx, "javac", jarName)
1122 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1123 jars = append(jars, classes)
1124 }
Colin Crossd6891432017-09-27 17:39:56 -07001125 if ctx.Failed() {
1126 return
1127 }
Colin Cross2fe66872015-03-30 17:20:39 -07001128 }
1129
Colin Cross0c4ce212019-05-03 15:28:19 -07001130 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1131
1132 var includeSrcJar android.WritablePath
1133 if Bool(j.properties.Include_srcs) {
1134 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1135 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1136 }
1137
Colin Crosscedd4762018-09-13 11:26:19 -07001138 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1139 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001140 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001141 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001142
1143 var resArgs []string
1144 var resDeps android.Paths
1145
1146 resArgs = append(resArgs, dirArgs...)
1147 resDeps = append(resDeps, dirDeps...)
1148
1149 resArgs = append(resArgs, fileArgs...)
1150 resDeps = append(resDeps, fileDeps...)
1151
Colin Cross988708c2019-05-06 14:04:11 -07001152 resArgs = append(resArgs, extraArgs...)
1153 resDeps = append(resDeps, extraDeps...)
1154
Colin Cross40a36712017-09-27 17:41:35 -07001155 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001156 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001157 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001158 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001159 if ctx.Failed() {
1160 return
1161 }
1162 }
1163
Colin Cross0c4ce212019-05-03 15:28:19 -07001164 var resourceJars android.Paths
1165 if j.resourceJar != nil {
1166 resourceJars = append(resourceJars, j.resourceJar)
1167 }
1168 if Bool(j.properties.Include_srcs) {
1169 resourceJars = append(resourceJars, includeSrcJar)
1170 }
1171 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001172
Colin Cross0c4ce212019-05-03 15:28:19 -07001173 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001174 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001175 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001176 false, nil, nil)
1177 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001178 } else if len(resourceJars) == 1 {
1179 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001180 }
1181
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001182 if len(deps.staticJars) > 0 {
1183 jars = append(jars, deps.staticJars...)
1184 hasSrcs = true
1185 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001186
Colin Cross094054a2018-10-17 15:10:48 -07001187 manifest := j.overrideManifest
1188 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001189 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001190 }
Colin Cross635acc92017-09-12 22:50:46 -07001191
Colin Cross8a497952019-03-05 22:25:09 -08001192 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001193 if len(services) > 0 {
1194 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1195 var zipargs []string
1196 for _, file := range services {
1197 serviceFile := file.String()
1198 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1199 }
1200 ctx.Build(pctx, android.BuildParams{
1201 Rule: zip,
1202 Output: servicesJar,
1203 Implicits: services,
1204 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001205 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001206 },
1207 })
1208 jars = append(jars, servicesJar)
1209 }
1210
Colin Cross0a6e0072017-08-30 14:24:55 -07001211 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001212 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001213 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001214
1215 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001216 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1217 // Optimization: skip the combine step if there is nothing to do
1218 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1219 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1220 // any if len(jars) == 1.
1221 outputFile = moduleOutPath
1222 } else {
1223 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1224 ctx.Build(pctx, android.BuildParams{
1225 Rule: android.Cp,
1226 Input: jars[0],
1227 Output: combinedJar,
1228 })
1229 outputFile = combinedJar
1230 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001231 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001232 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001233 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001234 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001235 outputFile = combinedJar
1236 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001237
Colin Cross331a1212018-08-15 20:40:52 -07001238 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001239 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001240 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001241 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001242 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001243 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001244
1245 // jarjar resource jar if necessary
1246 if j.resourceJar != nil {
1247 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001248 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001249 j.resourceJar = resourceJarJarFile
1250 }
1251
Colin Cross0a6e0072017-08-30 14:24:55 -07001252 if ctx.Failed() {
1253 return
1254 }
1255 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001256
1257 // Check package restrictions if necessary.
1258 if len(j.properties.Permitted_packages) > 0 {
1259 // Check packages and copy to package-checked file.
1260 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1261 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1262 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1263
1264 if ctx.Failed() {
1265 return
1266 }
1267 }
1268
Nan Zhanged19fc32017-10-19 13:06:22 -07001269 j.implementationJarFile = outputFile
1270 if j.headerJarFile == nil {
1271 j.headerJarFile = j.implementationJarFile
1272 }
Colin Cross2fe66872015-03-30 17:20:39 -07001273
Colin Cross6510f912017-11-29 00:27:14 -08001274 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001275 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1276 j.properties.Instrument = true
1277 }
1278 }
1279
Colin Cross3144dfc2018-01-03 15:06:47 -08001280 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001281 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1282 }
1283
Colin Cross331a1212018-08-15 20:40:52 -07001284 // merge implementation jar with resources if necessary
1285 implementationAndResourcesJar := outputFile
1286 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001287 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001288 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001289 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001290 false, nil, nil)
1291 implementationAndResourcesJar = combinedJar
1292 }
1293
1294 j.implementationAndResourcesJar = implementationAndResourcesJar
1295
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001296 if ctx.Device() && hasSrcs &&
1297 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001298 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001299 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001300 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001301 if ctx.Failed() {
1302 return
1303 }
Colin Cross331a1212018-08-15 20:40:52 -07001304
Colin Cross9c74a1e2019-05-28 14:06:17 -07001305 if !ctx.Config().UnbundledBuild() {
1306 // Hidden API CSV generation and dex encoding
1307 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1308 j.deviceProperties.UncompressDex)
1309 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001310
Colin Cross331a1212018-08-15 20:40:52 -07001311 // merge dex jar with resources if necessary
1312 if j.resourceJar != nil {
1313 jars := android.Paths{dexOutputFile, j.resourceJar}
1314 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1315 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1316 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001317 if j.deviceProperties.UncompressDex {
1318 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1319 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1320 dexOutputFile = combinedAlignedJar
1321 } else {
1322 dexOutputFile = combinedJar
1323 }
Colin Cross331a1212018-08-15 20:40:52 -07001324 }
1325
1326 j.dexJarFile = dexOutputFile
1327
Colin Cross8faf8fc2019-01-16 15:15:52 -08001328 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001329 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1330
1331 j.maybeStrippedDexJarFile = dexOutputFile
1332
Colin Cross3063b782018-08-15 11:19:12 -07001333 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001334
1335 if ctx.Failed() {
1336 return
1337 }
Colin Cross331a1212018-08-15 20:40:52 -07001338 } else {
1339 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001340 }
Colin Cross331a1212018-08-15 20:40:52 -07001341
Colin Crossb7a63242015-04-16 14:09:14 -07001342 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001343
1344 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1345 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001346}
1347
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001348// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1349// since some of these flags may be used internally.
1350func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1351 for _, flag := range flags {
1352 flag = strings.TrimSpace(flag)
1353
1354 if !strings.HasPrefix(flag, "-") {
1355 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1356 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1357 ctx.PropertyErrorf("kotlincflags",
1358 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1359 } else if inList(flag, config.KotlincIllegalFlags) {
1360 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1361 } else if flag == "-include-runtime" {
1362 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1363 } else {
1364 args := strings.Split(flag, " ")
1365 if args[0] == "-kotlin-home" {
1366 ctx.PropertyErrorf("kotlincflags",
1367 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1368 }
1369 }
1370 }
1371}
1372
Colin Cross8eadbf02017-10-24 17:46:00 -07001373func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001374 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001375
1376 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001377 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001378 // Compile java sources into turbine.jar.
1379 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1380 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1381 if ctx.Failed() {
1382 return nil
1383 }
1384 jars = append(jars, turbineJar)
1385 }
1386
Colin Cross55f63ea2018-08-27 12:37:09 -07001387 jars = append(jars, extraJars...)
1388
Nan Zhanged19fc32017-10-19 13:06:22 -07001389 // Combine any static header libraries into classes-header.jar. If there is only
1390 // one input jar this step will be skipped.
1391 var headerJar android.Path
1392 jars = append(jars, deps.staticHeaderJars...)
1393
Colin Cross5c6ecc12017-10-23 18:12:27 -07001394 // we cannot skip the combine step for now if there is only one jar
1395 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1396 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001397 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001398 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001399 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001400
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001401 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001402 // Transform classes.jar into classes-jarjar.jar
1403 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001404 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001405 headerJar = jarjarFile
1406 if ctx.Failed() {
1407 return nil
1408 }
1409 }
1410
1411 return headerJar
1412}
1413
Colin Crosscb933592017-11-22 13:49:43 -08001414func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001415 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001416
Colin Cross7a3139e2017-12-19 13:57:50 -08001417 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001418
Colin Cross84c38822018-01-03 15:59:46 -08001419 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001420 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1421
1422 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1423
1424 j.jacocoReportClassesFile = jacocoReportClassesFile
1425
1426 return instrumentedJar
1427}
1428
albaltai36ff7dc2018-12-25 14:35:23 +08001429var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001430
Nan Zhanged19fc32017-10-19 13:06:22 -07001431func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001432 if j.headerJarFile == nil {
1433 return nil
1434 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001435 return android.Paths{j.headerJarFile}
1436}
1437
1438func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001439 if j.implementationJarFile == nil {
1440 return nil
1441 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001442 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001443}
1444
Colin Crossf24a22a2019-01-31 14:12:44 -08001445func (j *Module) DexJar() android.Path {
1446 return j.dexJarFile
1447}
1448
Colin Cross331a1212018-08-15 20:40:52 -07001449func (j *Module) ResourceJars() android.Paths {
1450 if j.resourceJar == nil {
1451 return nil
1452 }
1453 return android.Paths{j.resourceJar}
1454}
1455
1456func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001457 if j.implementationAndResourcesJar == nil {
1458 return nil
1459 }
Colin Cross331a1212018-08-15 20:40:52 -07001460 return android.Paths{j.implementationAndResourcesJar}
1461}
1462
Colin Cross46c9b8b2017-06-22 16:51:17 -07001463func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001464 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001465 return j.exportAidlIncludeDirs
1466}
1467
Jiyong Park1be96912018-05-28 18:02:19 +09001468func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001469 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001470 return j.exportedSdkLibs
1471}
1472
Colin Cross0c4ce212019-05-03 15:28:19 -07001473func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1474 return j.srcJarArgs, j.srcJarDeps
1475}
1476
Colin Cross46c9b8b2017-06-22 16:51:17 -07001477var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001478
Colin Cross46c9b8b2017-06-22 16:51:17 -07001479func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001480 return j.logtagsSrcs
1481}
1482
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001483// Collect information for opening IDE project files in java/jdeps.go.
1484func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1485 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1486 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1487 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001488 if j.expandJarjarRules != nil {
1489 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001490 }
1491}
1492
1493func (j *Module) CompilerDeps() []string {
1494 jdeps := []string{}
1495 jdeps = append(jdeps, j.properties.Libs...)
1496 jdeps = append(jdeps, j.properties.Static_libs...)
1497 return jdeps
1498}
1499
Colin Cross2fe66872015-03-30 17:20:39 -07001500//
1501// Java libraries (.jar file)
1502//
1503
Colin Crossf506d872017-07-19 15:53:04 -07001504type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001505 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001506}
1507
Colin Cross42be7612019-02-21 18:12:14 -08001508func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001509 // Store uncompressed (and do not strip) dex files from boot class path jars.
1510 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1511 return true
1512 }
1513
1514 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001515 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001516 return true
1517 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001518 if ctx.Config().UncompressPrivAppDex() &&
1519 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1520 return true
1521 }
1522
Colin Cross2fc72f62018-12-21 12:59:54 -08001523 return false
1524}
1525
Colin Crossf506d872017-07-19 15:53:04 -07001526func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001527 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1528 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001529 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001530 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001531 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001532 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001533
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001534 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001535 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1536 ctx.ModuleName()+".jar", j.outputFile)
1537 }
Colin Crossb7a63242015-04-16 14:09:14 -07001538}
1539
Colin Crossf506d872017-07-19 15:53:04 -07001540func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001541 j.deps(ctx)
1542}
1543
Colin Cross1b16b0e2019-02-12 14:41:32 -08001544// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1545//
1546// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1547// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1548// as a `static_libs` dependency of another module.
1549//
1550// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1551// a device.
1552//
1553// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1554// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001555func LibraryFactory() android.Module {
1556 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001557
Colin Cross9ae1b922018-06-26 17:59:05 -07001558 module.AddProperties(
1559 &module.Module.properties,
1560 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001561 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001562 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001563
Colin Cross9ae1b922018-06-26 17:59:05 -07001564 InitJavaModule(module, android.HostAndDeviceSupported)
1565 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001566}
1567
Colin Cross1b16b0e2019-02-12 14:41:32 -08001568// java_library_static is an obsolete alias for java_library.
1569func LibraryStaticFactory() android.Module {
1570 return LibraryFactory()
1571}
1572
1573// java_library_host builds and links sources into a `.jar` file for the host.
1574//
1575// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1576// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001577func LibraryHostFactory() android.Module {
1578 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001579
Colin Cross6af17aa2017-09-20 12:59:05 -07001580 module.AddProperties(
1581 &module.Module.properties,
1582 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001583
Colin Cross9ae1b922018-06-26 17:59:05 -07001584 module.Module.properties.Installable = proptools.BoolPtr(true)
1585
Colin Cross89536d42017-07-07 14:35:50 -07001586 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001587 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001588}
1589
1590//
Colin Crossb628ea52018-08-14 16:42:33 -07001591// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001592//
1593
1594type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001595 // list of compatibility suites (for example "cts", "vts") that the module should be
1596 // installed into.
1597 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001598
1599 // the name of the test configuration (for example "AndroidTest.xml") that should be
1600 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001601 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001602
Jack He33338892018-09-19 02:21:28 -07001603 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1604 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001605 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001606
Colin Crossd96ca352018-08-10 16:06:24 -07001607 // list of files or filegroup modules that provide data that should be installed alongside
1608 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001609 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001610}
1611
Paul Duffin42df1442019-03-20 12:45:53 +00001612type testHelperLibraryProperties struct {
1613 // list of compatibility suites (for example "cts", "vts") that the module should be
1614 // installed into.
1615 Test_suites []string `android:"arch_variant"`
1616}
1617
Colin Cross05638fc2018-04-09 18:40:24 -07001618type Test struct {
1619 Library
1620
1621 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001622
1623 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001624 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001625}
1626
Paul Duffin42df1442019-03-20 12:45:53 +00001627type TestHelperLibrary struct {
1628 Library
1629
1630 testHelperLibraryProperties testHelperLibraryProperties
1631}
1632
Colin Cross303e21f2018-08-07 16:49:25 -07001633func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001634 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 -08001635 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001636
1637 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001638}
1639
Paul Duffin42df1442019-03-20 12:45:53 +00001640func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1641 j.Library.GenerateAndroidBuildActions(ctx)
1642}
1643
Colin Cross1b16b0e2019-02-12 14:41:32 -08001644// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1645// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1646//
1647// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1648// compiled against the device bootclasspath.
1649//
1650// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1651// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001652func TestFactory() android.Module {
1653 module := &Test{}
1654
1655 module.AddProperties(
1656 &module.Module.properties,
1657 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001658 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001659 &module.Module.protoProperties,
1660 &module.testProperties)
1661
Colin Cross9ae1b922018-06-26 17:59:05 -07001662 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001663 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001664
Colin Cross05638fc2018-04-09 18:40:24 -07001665 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001666 return module
1667}
1668
Paul Duffin42df1442019-03-20 12:45:53 +00001669// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1670func TestHelperLibraryFactory() android.Module {
1671 module := &TestHelperLibrary{}
1672
1673 module.AddProperties(
1674 &module.Module.properties,
1675 &module.Module.deviceProperties,
1676 &module.Module.dexpreoptProperties,
1677 &module.Module.protoProperties,
1678 &module.testHelperLibraryProperties)
1679
Colin Cross9a4abed2019-04-24 13:19:28 -07001680 module.Module.properties.Installable = proptools.BoolPtr(true)
1681 module.Module.dexpreopter.isTest = true
1682
Paul Duffin42df1442019-03-20 12:45:53 +00001683 InitJavaModule(module, android.HostAndDeviceSupported)
1684 return module
1685}
1686
Colin Cross1b16b0e2019-02-12 14:41:32 -08001687// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1688// allow running the test with `atest` or a `TEST_MAPPING` file.
1689//
1690// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1691// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001692func TestHostFactory() android.Module {
1693 module := &Test{}
1694
1695 module.AddProperties(
1696 &module.Module.properties,
1697 &module.Module.protoProperties,
1698 &module.testProperties)
1699
Colin Cross9ae1b922018-06-26 17:59:05 -07001700 module.Module.properties.Installable = proptools.BoolPtr(true)
1701
Colin Cross05638fc2018-04-09 18:40:24 -07001702 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001703 return module
1704}
1705
1706//
Colin Cross2fe66872015-03-30 17:20:39 -07001707// Java Binaries (.jar file plus wrapper script)
1708//
1709
Colin Crossf506d872017-07-19 15:53:04 -07001710type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001711 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001712 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001713
1714 // Name of the class containing main to be inserted into the manifest as Main-Class.
1715 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001716}
1717
Colin Crossf506d872017-07-19 15:53:04 -07001718type Binary struct {
1719 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001720
Colin Crossf506d872017-07-19 15:53:04 -07001721 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001722
Colin Cross6b4a32d2017-12-05 13:42:45 -08001723 isWrapperVariant bool
1724
Colin Crossc3315992017-12-08 19:12:36 -08001725 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001726 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001727}
1728
Alex Light24237172017-10-26 09:46:21 -07001729func (j *Binary) HostToolPath() android.OptionalPath {
1730 return android.OptionalPathForPath(j.binaryFile)
1731}
1732
Colin Crossf506d872017-07-19 15:53:04 -07001733func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001734 if ctx.Arch().ArchType == android.Common {
1735 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001736 if j.binaryProperties.Main_class != nil {
1737 if j.properties.Manifest != nil {
1738 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1739 }
1740 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1741 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1742 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1743 }
1744
Colin Cross6b4a32d2017-12-05 13:42:45 -08001745 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001746 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001747 // Handle the binary wrapper
1748 j.isWrapperVariant = true
1749
Colin Cross366938f2017-12-11 16:29:02 -08001750 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001751 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001752 } else {
1753 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1754 }
1755
1756 // Depend on the installed jar so that the wrapper doesn't get executed by
1757 // another build rule before the jar has been installed.
1758 jarFile := ctx.PrimaryModule().(*Binary).installFile
1759
1760 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1761 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001762 }
Colin Cross2fe66872015-03-30 17:20:39 -07001763}
1764
Colin Crossf506d872017-07-19 15:53:04 -07001765func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001766 if ctx.Arch().ArchType == android.Common {
1767 j.deps(ctx)
1768 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001769}
1770
Colin Cross1b16b0e2019-02-12 14:41:32 -08001771// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1772// as well.
1773//
1774// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1775// compiled against the device bootclasspath.
1776//
1777// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1778// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001779func BinaryFactory() android.Module {
1780 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001781
Colin Cross36242852017-06-23 15:06:31 -07001782 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001783 &module.Module.properties,
1784 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001785 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001786 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001787 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001788
Colin Cross9ae1b922018-06-26 17:59:05 -07001789 module.Module.properties.Installable = proptools.BoolPtr(true)
1790
Colin Cross6b4a32d2017-12-05 13:42:45 -08001791 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1792 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001793 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001794}
1795
Colin Cross1b16b0e2019-02-12 14:41:32 -08001796// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1797//
1798// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1799// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001800func BinaryHostFactory() android.Module {
1801 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001802
Colin Cross36242852017-06-23 15:06:31 -07001803 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001804 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001805 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001806 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001807
Colin Cross9ae1b922018-06-26 17:59:05 -07001808 module.Module.properties.Installable = proptools.BoolPtr(true)
1809
Colin Cross6b4a32d2017-12-05 13:42:45 -08001810 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1811 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001812 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001813}
1814
1815//
1816// Java prebuilts
1817//
1818
Colin Cross74d73e22017-08-02 11:05:49 -07001819type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001820 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001821
Nan Zhangea568a42017-11-08 21:20:04 -08001822 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001823
1824 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001825
1826 // List of shared java libs that this module has dependencies to
1827 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001828
1829 // List of files to remove from the jar file(s)
1830 Exclude_files []string
1831
1832 // List of directories to remove from the jar file(s)
1833 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001834
1835 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001836 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001837}
1838
1839type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001840 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001841 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001842 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001843
Colin Cross74d73e22017-08-02 11:05:49 -07001844 properties ImportProperties
1845
Colin Cross0a6e0072017-08-30 14:24:55 -07001846 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001847 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001848}
1849
Colin Cross83bb3162018-06-25 15:48:06 -07001850func (j *Import) sdkVersion() string {
1851 return String(j.properties.Sdk_version)
1852}
1853
1854func (j *Import) minSdkVersion() string {
1855 return j.sdkVersion()
1856}
1857
Colin Cross74d73e22017-08-02 11:05:49 -07001858func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001859 return &j.prebuilt
1860}
1861
Colin Cross74d73e22017-08-02 11:05:49 -07001862func (j *Import) PrebuiltSrcs() []string {
1863 return j.properties.Jars
1864}
1865
1866func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001867 return j.prebuilt.Name(j.ModuleBase.Name())
1868}
1869
Colin Cross74d73e22017-08-02 11:05:49 -07001870func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001871 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001872}
1873
Colin Cross74d73e22017-08-02 11:05:49 -07001874func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001875 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001876
Nan Zhang4c819fb2018-08-27 18:31:46 -07001877 jarName := ctx.ModuleName() + ".jar"
1878 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001879 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1880 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001881 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001882 inputFile := outputFile
1883 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1884 TransformJetifier(ctx, outputFile, inputFile)
1885 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001886 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001887
1888 ctx.VisitDirectDeps(func(module android.Module) {
1889 otherName := ctx.OtherModuleName(module)
1890 tag := ctx.OtherModuleDependencyTag(module)
1891
1892 switch dep := module.(type) {
1893 case Dependency:
1894 switch tag {
1895 case libTag, staticLibTag:
1896 // sdk lib names from dependencies are re-exported
1897 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1898 }
1899 case SdkLibraryDependency:
1900 switch tag {
1901 case libTag:
1902 // names of sdk libs that are directly depended are exported
1903 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1904 }
1905 }
1906 })
1907
1908 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001909 if Bool(j.properties.Installable) {
1910 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1911 ctx.ModuleName()+".jar", outputFile)
1912 }
Colin Cross2fe66872015-03-30 17:20:39 -07001913}
1914
Colin Cross74d73e22017-08-02 11:05:49 -07001915var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001916
Nan Zhanged19fc32017-10-19 13:06:22 -07001917func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001918 if j.combinedClasspathFile == nil {
1919 return nil
1920 }
Colin Cross37f6d792018-07-12 12:28:41 -07001921 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001922}
1923
1924func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001925 if j.combinedClasspathFile == nil {
1926 return nil
1927 }
Colin Cross37f6d792018-07-12 12:28:41 -07001928 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001929}
1930
Colin Cross331a1212018-08-15 20:40:52 -07001931func (j *Import) ResourceJars() android.Paths {
1932 return nil
1933}
1934
1935func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001936 if j.combinedClasspathFile == nil {
1937 return nil
1938 }
Colin Cross331a1212018-08-15 20:40:52 -07001939 return android.Paths{j.combinedClasspathFile}
1940}
1941
Colin Crossf24a22a2019-01-31 14:12:44 -08001942func (j *Import) DexJar() android.Path {
1943 return nil
1944}
1945
Colin Cross74d73e22017-08-02 11:05:49 -07001946func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001947 return nil
1948}
1949
Jiyong Park1be96912018-05-28 18:02:19 +09001950func (j *Import) ExportedSdkLibs() []string {
1951 return j.exportedSdkLibs
1952}
1953
Colin Cross0c4ce212019-05-03 15:28:19 -07001954func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1955 return nil, nil
1956}
1957
albaltai36ff7dc2018-12-25 14:35:23 +08001958// Add compile time check for interface implementation
1959var _ android.IDEInfo = (*Import)(nil)
1960var _ android.IDECustomizedModuleName = (*Import)(nil)
1961
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001962// Collect information for opening IDE project files in java/jdeps.go.
1963const (
1964 removedPrefix = "prebuilt_"
1965)
1966
1967func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1968 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1969}
1970
1971func (j *Import) IDECustomizedModuleName() string {
1972 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1973 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1974 // solution to get the Import name.
1975 name := j.Name()
1976 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001977 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001978 }
1979 return name
1980}
1981
Colin Cross74d73e22017-08-02 11:05:49 -07001982var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001983
Colin Cross1b16b0e2019-02-12 14:41:32 -08001984// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1985//
1986// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1987// compiled against an Android classpath.
1988//
1989// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1990// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001991func ImportFactory() android.Module {
1992 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001993
Colin Cross74d73e22017-08-02 11:05:49 -07001994 module.AddProperties(&module.properties)
1995
1996 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001997 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001998 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001999}
2000
Colin Cross1b16b0e2019-02-12 14:41:32 -08002001// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2002// module.
2003//
2004// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2005// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002006func ImportFactoryHost() android.Module {
2007 module := &Import{}
2008
2009 module.AddProperties(&module.properties)
2010
2011 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002012 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002013 return module
2014}
2015
Colin Cross42be7612019-02-21 18:12:14 -08002016// dex_import module
2017
2018type DexImportProperties struct {
2019 Jars []string
2020}
2021
2022type DexImport struct {
2023 android.ModuleBase
2024 android.DefaultableModuleBase
2025 prebuilt android.Prebuilt
2026
2027 properties DexImportProperties
2028
2029 dexJarFile android.Path
2030 maybeStrippedDexJarFile android.Path
2031
2032 dexpreopter
2033}
2034
2035func (j *DexImport) Prebuilt() *android.Prebuilt {
2036 return &j.prebuilt
2037}
2038
2039func (j *DexImport) PrebuiltSrcs() []string {
2040 return j.properties.Jars
2041}
2042
2043func (j *DexImport) Name() string {
2044 return j.prebuilt.Name(j.ModuleBase.Name())
2045}
2046
2047func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2048 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2049}
2050
2051func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2052 if len(j.properties.Jars) != 1 {
2053 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2054 }
2055
2056 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2057 j.dexpreopter.isInstallable = true
2058 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2059
2060 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2061 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2062
2063 if j.dexpreopter.uncompressedDex {
2064 rule := android.NewRuleBuilder()
2065
2066 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2067 rule.Temporary(temporary)
2068
2069 // use zip2zip to uncompress classes*.dex files
2070 rule.Command().
2071 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2072 FlagWithInput("-i ", inputJar).
2073 FlagWithOutput("-o ", temporary).
2074 FlagWithArg("-0 ", "'classes*.dex'")
2075
2076 // use zipalign to align uncompressed classes*.dex files
2077 rule.Command().
2078 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2079 Flag("-f").
2080 Text("4").
2081 Input(temporary).
2082 Output(dexOutputFile)
2083
2084 rule.DeleteTemporaryFiles()
2085
2086 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2087 } else {
2088 ctx.Build(pctx, android.BuildParams{
2089 Rule: android.Cp,
2090 Input: inputJar,
2091 Output: dexOutputFile,
2092 })
2093 }
2094
2095 j.dexJarFile = dexOutputFile
2096
2097 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2098
2099 j.maybeStrippedDexJarFile = dexOutputFile
2100
2101 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2102 ctx.ModuleName()+".jar", dexOutputFile)
2103}
2104
2105func (j *DexImport) DexJar() android.Path {
2106 return j.dexJarFile
2107}
2108
2109// dex_import imports a `.jar` file containing classes.dex files.
2110//
2111// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2112// to the device.
2113func DexImportFactory() android.Module {
2114 module := &DexImport{}
2115
2116 module.AddProperties(&module.properties)
2117
2118 android.InitPrebuiltModule(module, &module.properties.Jars)
2119 InitJavaModule(module, android.DeviceSupported)
2120 return module
2121}
2122
Colin Cross89536d42017-07-07 14:35:50 -07002123//
2124// Defaults
2125//
2126type Defaults struct {
2127 android.ModuleBase
2128 android.DefaultsModuleBase
2129}
2130
2131func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2132}
2133
Colin Cross1b16b0e2019-02-12 14:41:32 -08002134// java_defaults provides a set of properties that can be inherited by other java or android modules.
2135//
2136// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2137// property in the defaults module that exists in the depending module will be prepended to the depending module's
2138// value for that property.
2139//
2140// Example:
2141//
2142// java_defaults {
2143// name: "example_defaults",
2144// srcs: ["common/**/*.java"],
2145// javacflags: ["-Xlint:all"],
2146// aaptflags: ["--auto-add-overlay"],
2147// }
2148//
2149// java_library {
2150// name: "example",
2151// defaults: ["example_defaults"],
2152// srcs: ["example/**/*.java"],
2153// }
2154//
2155// is functionally identical to:
2156//
2157// java_library {
2158// name: "example",
2159// srcs: [
2160// "common/**/*.java",
2161// "example/**/*.java",
2162// ],
2163// javacflags: ["-Xlint:all"],
2164// }
Colin Cross89536d42017-07-07 14:35:50 -07002165func defaultsFactory() android.Module {
2166 return DefaultsFactory()
2167}
2168
2169func DefaultsFactory(props ...interface{}) android.Module {
2170 module := &Defaults{}
2171
2172 module.AddProperties(props...)
2173 module.AddProperties(
2174 &CompilerProperties{},
2175 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002176 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002177 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002178 &aaptProperties{},
2179 &androidLibraryProperties{},
2180 &appProperties{},
2181 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002182 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002183 &ImportProperties{},
2184 &AARImportProperties{},
2185 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002186 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002187 )
2188
2189 android.InitDefaultsModule(module)
2190
2191 return module
2192}
Nan Zhangea568a42017-11-08 21:20:04 -08002193
2194var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002195var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002196var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002197var inList = android.InList