blob: 0bccb67e38a3e4d86d5f71fc20abe675fbeadb56 [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 Cross41955e82019-05-29 14:40:35 -0700354func (j *Module) OutputFiles(tag string) (android.Paths, error) {
355 switch tag {
356 case "":
357 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700358 case ".jar":
359 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700360 default:
361 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
362 }
Colin Cross54250902017-12-05 09:28:08 -0800363}
364
Jiyong Park8fd61922018-11-08 02:50:25 +0900365func (j *Module) DexJarFile() android.Path {
366 return j.dexJarFile
367}
368
Colin Cross41955e82019-05-29 14:40:35 -0700369var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800370
Colin Crossf506d872017-07-19 15:53:04 -0700371type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700372 HeaderJars() android.Paths
373 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700374 ResourceJars() android.Paths
375 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800376 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700377 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900378 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700379 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700380}
381
Jiyong Parkc678ad32018-04-10 13:07:10 +0900382type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800383 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
384 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900385}
386
Nan Zhangb2b33de2018-02-23 11:18:47 -0800387type SrcDependency interface {
388 CompiledSrcs() android.Paths
389 CompiledSrcJars() android.Paths
390}
391
392func (j *Module) CompiledSrcs() android.Paths {
393 return j.compiledJavaSrcs
394}
395
396func (j *Module) CompiledSrcJars() android.Paths {
397 return j.compiledSrcJars
398}
399
400var _ SrcDependency = (*Module)(nil)
401
Colin Cross89536d42017-07-07 14:35:50 -0700402func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
403 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
404 android.InitDefaultableModule(module)
405}
406
Colin Crossbe1da472017-07-07 15:59:46 -0700407type dependencyTag struct {
408 blueprint.BaseDependencyTag
409 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700410}
411
Colin Crossa4f08812018-10-02 22:03:40 -0700412type jniDependencyTag struct {
413 blueprint.BaseDependencyTag
414 target android.Target
415}
416
Colin Crossbe1da472017-07-07 15:59:46 -0700417var (
Colin Cross4b964c02018-10-15 16:18:06 -0700418 staticLibTag = dependencyTag{name: "staticlib"}
419 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800420 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700421 bootClasspathTag = dependencyTag{name: "bootclasspath"}
422 systemModulesTag = dependencyTag{name: "system modules"}
423 frameworkResTag = dependencyTag{name: "framework-res"}
424 frameworkApkTag = dependencyTag{name: "framework-apk"}
425 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800426 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700427 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
428 certificateTag = dependencyTag{name: "certificate"}
429 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700430 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700431)
Colin Cross2fe66872015-03-30 17:20:39 -0700432
Colin Crossfc3674a2017-09-18 17:41:52 -0700433type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700434 useModule, useFiles, useDefaultLibs, invalidVersion bool
435
Colin Cross86a60ae2018-05-29 14:44:55 -0700436 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700437 systemModules string
438
Colin Crossa97c5d32018-03-28 14:58:31 -0700439 frameworkResModule string
440
Colin Cross86a60ae2018-05-29 14:44:55 -0700441 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700442 aidl android.OptionalPath
Colin Cross1369cdb2017-09-29 17:58:17 -0700443}
444
Colin Crossa4f08812018-10-02 22:03:40 -0700445type jniLib struct {
446 name string
447 path android.Path
448 target android.Target
449}
450
Colin Cross3144dfc2018-01-03 15:06:47 -0800451func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
452 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
453}
454
455func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
456 return j.shouldInstrument(ctx) &&
457 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
458 ctx.Config().UnbundledBuild())
459}
460
Colin Cross83bb3162018-06-25 15:48:06 -0700461func (j *Module) sdkVersion() string {
462 return String(j.deviceProperties.Sdk_version)
463}
464
465func (j *Module) minSdkVersion() string {
466 if j.deviceProperties.Min_sdk_version != nil {
467 return *j.deviceProperties.Min_sdk_version
468 }
469 return j.sdkVersion()
470}
471
Dan Willemsen419290a2018-10-31 15:28:47 -0700472func (j *Module) targetSdkVersion() string {
473 if j.deviceProperties.Target_sdk_version != nil {
474 return *j.deviceProperties.Target_sdk_version
475 }
476 return j.sdkVersion()
477}
478
Colin Crossbe1da472017-07-07 15:59:46 -0700479func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700480 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700481 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700482 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700483 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700484 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100485 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700486 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700487 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700488 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700489 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100490 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700491 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700492 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700493 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
494 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800495 }
Colin Crossbe1da472017-07-07 15:59:46 -0700496 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700497 } else if j.deviceProperties.System_modules == nil {
498 ctx.PropertyErrorf("no_standard_libs",
499 "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 +0100500 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700501 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700502 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100503 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700504 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800505 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800506 if ctx.ModuleName() == "android_stubs_current" ||
507 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700508 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700509 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800510 }
Colin Cross2fe66872015-03-30 17:20:39 -0700511 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700512
Colin Cross42d48b72018-08-29 14:10:52 -0700513 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
514 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700515
Colin Crossbe9cdb82019-01-21 21:37:16 -0800516 ctx.AddFarVariationDependencies([]blueprint.Variation{
517 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
518 }, pluginTag, j.properties.Plugins...)
519
Colin Crossfe17f6f2019-03-28 19:30:56 -0700520 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700521 if j.hasSrcExt(".proto") {
522 protoDeps(ctx, &j.protoProperties)
523 }
Colin Cross93e85952017-08-15 13:34:18 -0700524
525 if j.hasSrcExt(".kt") {
526 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
527 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700528 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
529 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800530 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800531 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
532 }
Colin Cross93e85952017-08-15 13:34:18 -0700533 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800534
535 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700536 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800537 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700538}
539
540func hasSrcExt(srcs []string, ext string) bool {
541 for _, src := range srcs {
542 if filepath.Ext(src) == ext {
543 return true
544 }
545 }
546
547 return false
548}
549
Nan Zhang61eaedb2017-11-02 13:28:15 -0700550func shardPaths(paths android.Paths, shardSize int) []android.Paths {
551 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
552 for len(paths) > shardSize {
553 ret = append(ret, paths[0:shardSize])
554 paths = paths[shardSize:]
555 }
556 if len(paths) > 0 {
557 ret = append(ret, paths)
558 }
559 return ret
560}
561
Colin Cross6af17aa2017-09-20 12:59:05 -0700562func (j *Module) hasSrcExt(ext string) bool {
563 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700564}
565
Colin Cross46c9b8b2017-06-22 16:51:17 -0700566func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700567 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700568
Colin Crossebe1a512017-11-14 13:12:14 -0800569 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
570 aidlIncludes = append(aidlIncludes,
571 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
572 aidlIncludes = append(aidlIncludes,
573 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700574
Colin Cross3047fa22019-04-18 10:56:44 -0700575 var flags []string
576 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700577
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700578 if aidlPreprocess.Valid() {
579 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700580 deps = append(deps, aidlPreprocess.Path())
581 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700582 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700583 }
584
Colin Cross3047fa22019-04-18 10:56:44 -0700585 if len(j.exportAidlIncludeDirs) > 0 {
586 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
587 }
588
589 if len(aidlIncludes) > 0 {
590 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
591 }
592
Colin Cross635c3b02016-05-18 15:37:25 -0700593 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800594 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700595 flags = append(flags, "-I"+src.String())
596 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700597
Martijn Coeneneab15642018-03-09 09:29:59 +0100598 if Bool(j.deviceProperties.Aidl.Generate_traces) {
599 flags = append(flags, "-t")
600 }
601
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100602 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
603 flags = append(flags, "--transaction_names")
604 }
605
Colin Cross3047fa22019-04-18 10:56:44 -0700606 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700607}
608
Colin Cross32f676a2017-09-06 13:41:06 -0700609type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800610 classpath classpath
611 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700612 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800613 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700614 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700615 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700616 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700617 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800618 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700619 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700620 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700621 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700622 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800623 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800624
625 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700626}
Colin Cross2fe66872015-03-30 17:20:39 -0700627
Colin Cross54250902017-12-05 09:28:08 -0800628func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
629 for _, f := range dep.Srcs() {
630 if f.Ext() != ".jar" {
631 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
632 ctx.OtherModuleName(dep.(blueprint.Module)))
633 }
634 }
635}
636
Jiyong Park2d492942018-03-05 17:44:10 +0900637type linkType int
638
639const (
640 javaCore linkType = iota
641 javaSdk
642 javaSystem
643 javaPlatform
644)
645
Jiyong Park46f78fb2018-10-20 16:33:17 +0900646func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700647 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700648 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900649 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
650 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100651 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900652 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100653 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900654 return javaCore, false
655 case name == "android_system_stubs_current":
656 return javaSystem, true
657 case strings.HasPrefix(ver, "system_"):
658 return javaSystem, false
659 case name == "android_test_stubs_current":
660 return javaSystem, true
661 case strings.HasPrefix(ver, "test_"):
662 return javaPlatform, false
663 case name == "android_stubs_current":
664 return javaSdk, true
665 case ver == "current":
666 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700667 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900668 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700669 default:
670 if _, err := strconv.Atoi(ver); err != nil {
671 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
672 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900673 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900674 }
675}
676
Jiyong Park750e5572018-01-31 00:20:13 +0900677func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700678 if ctx.Host() {
679 return
680 }
681
Jiyong Park46f78fb2018-10-20 16:33:17 +0900682 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
683 if stubs {
684 return
685 }
686 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900687 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."
688
689 switch myLinkType {
690 case javaCore:
691 if otherLinkType != javaCore {
692 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 +0900693 ctx.OtherModuleName(to))
694 }
Jiyong Park2d492942018-03-05 17:44:10 +0900695 break
696 case javaSdk:
697 if otherLinkType != javaCore && otherLinkType != javaSdk {
698 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
699 ctx.OtherModuleName(to))
700 }
701 break
702 case javaSystem:
703 if otherLinkType == javaPlatform {
704 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
705 ctx.OtherModuleName(to))
706 }
707 break
708 case javaPlatform:
709 // no restriction on link-type
710 break
Jiyong Park750e5572018-01-31 00:20:13 +0900711 }
712}
713
Colin Cross32f676a2017-09-06 13:41:06 -0700714func (j *Module) collectDeps(ctx android.ModuleContext) deps {
715 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700716
Colin Cross300f0382018-03-06 13:11:51 -0800717 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700718 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800719 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700720 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800721 } else if sdkDep.useFiles {
722 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700723 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700724 deps.aidlPreprocess = sdkDep.aidl
725 } else {
726 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800727 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700728 }
729
Colin Crossd11fcda2017-10-23 17:59:01 -0700730 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700731 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700732 tag := ctx.OtherModuleDependencyTag(module)
733
Colin Crossa4f08812018-10-02 22:03:40 -0700734 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700735 // Handled by AndroidApp.collectAppDeps
736 return
737 }
738 if tag == certificateTag {
739 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700740 return
741 }
742
Jiyong Park750e5572018-01-31 00:20:13 +0900743 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700744 switch tag {
745 case bootClasspathTag, libTag, staticLibTag:
746 checkLinkType(ctx, j, to, tag.(dependencyTag))
747 }
Jiyong Park750e5572018-01-31 00:20:13 +0900748 }
Colin Cross54250902017-12-05 09:28:08 -0800749 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800750 case SdkLibraryDependency:
751 switch tag {
752 case libTag:
753 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
754 // names of sdk libs that are directly depended are exported
755 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700756 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800757 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
758 }
Colin Cross54250902017-12-05 09:28:08 -0800759 case Dependency:
760 switch tag {
761 case bootClasspathTag:
762 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700763 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800764 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900765 // sdk lib names from dependencies are re-exported
766 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700767 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800768 case staticLibTag:
769 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
770 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
771 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700772 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900773 // sdk lib names from dependencies are re-exported
774 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700775 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800776 case pluginTag:
777 if plugin, ok := dep.(*Plugin); ok {
778 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
779 if plugin.pluginProperties.Processor_class != nil {
780 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
781 }
782 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
783 } else {
784 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
785 }
Colin Cross54250902017-12-05 09:28:08 -0800786 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100787 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800788 // framework.jar has a one-off dependency on the R.java and Manifest.java files
789 // generated by framework-res.apk
790 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
791 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800792 case frameworkApkTag:
793 if ctx.ModuleName() == "android_stubs_current" ||
794 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700795 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800796 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
797 // resource files out of there for aapt.
798 //
799 // Normally the package rule runs aapt, which includes the resource,
800 // but we're not running that in our package rule so just copy in the
801 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700802 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800803 }
Colin Cross54250902017-12-05 09:28:08 -0800804 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700805 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800806 case kotlinAnnotationsTag:
807 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800808 }
809
Colin Cross54250902017-12-05 09:28:08 -0800810 case android.SourceFileProducer:
811 switch tag {
812 case libTag:
813 checkProducesJars(ctx, dep)
814 deps.classpath = append(deps.classpath, dep.Srcs()...)
815 case staticLibTag:
816 checkProducesJars(ctx, dep)
817 deps.classpath = append(deps.classpath, dep.Srcs()...)
818 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
819 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800820 }
821 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700822 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700823 case systemModulesTag:
824 if deps.systemModules != nil {
825 panic("Found two system module dependencies")
826 }
827 sm := module.(*SystemModules)
828 if sm.outputFile == nil {
829 panic("Missing directory for system module dependency")
830 }
831 deps.systemModules = sm.outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700832 }
Colin Crossec7a0422017-07-07 14:47:12 -0700833 }
Colin Cross2fe66872015-03-30 17:20:39 -0700834 })
835
Jiyong Park1be96912018-05-28 18:02:19 +0900836 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
837
Colin Cross32f676a2017-09-06 13:41:06 -0700838 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700839}
840
Colin Cross83bb3162018-06-25 15:48:06 -0700841func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700842 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800843 v := sdkContext.sdkVersion()
844 // For PDK builds, use the latest SDK version instead of "current"
845 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700846 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800847 latestSdkVersion := 0
848 if len(sdkVersions) > 0 {
849 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
850 }
851 v = strconv.Itoa(latestSdkVersion)
852 }
853
854 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700855 if err != nil {
856 ctx.PropertyErrorf("sdk_version", "%s", err)
857 }
Nan Zhang357466b2018-04-17 17:38:36 -0700858 if javaVersion != "" {
859 ret = javaVersion
860 } else if ctx.Device() && sdk <= 23 {
861 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100862 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700863 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700864 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700865 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
866 ret = "1.8"
867 } else {
868 ret = "1.9"
869 }
870
871 return ret
872}
873
Nan Zhanged19fc32017-10-19 13:06:22 -0700874func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700875
Colin Crossf03c82b2015-04-13 13:53:40 -0700876 var flags javaBuilderFlags
877
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100878 // javaVersion flag.
879 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
880
Nan Zhanged19fc32017-10-19 13:06:22 -0700881 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700882 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100883 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700884 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700885 }
Colin Cross6510f912017-11-29 00:27:14 -0800886 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700887 // Override the -g flag passed globally to remove local variable debug info to reduce
888 // disk and memory usage.
889 javacFlags = append(javacFlags, "-g:source,lines")
890 }
Colin Cross64162712017-08-08 13:17:59 -0700891
Colin Cross66548102018-06-19 22:47:35 -0700892 if ctx.Config().RunErrorProne() {
893 if config.ErrorProneClasspath == nil {
894 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
895 }
896
897 errorProneFlags := []string{
898 "-Xplugin:ErrorProne",
899 "${config.ErrorProneChecks}",
900 }
901 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
902
903 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
904 "'" + strings.Join(errorProneFlags, " ") + "'"
905 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800906 }
907
Nan Zhanged19fc32017-10-19 13:06:22 -0700908 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800909 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
910 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700911 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800912
Colin Crossbe9cdb82019-01-21 21:37:16 -0800913 flags.processor = strings.Join(deps.processorClasses, ",")
914
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100915 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800916 !Bool(j.properties.No_standard_libs) &&
917 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
918 // Give host-side tools a version of OpenJDK's standard libraries
919 // close to what they're targeting. As of Dec 2017, AOSP is only
920 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
921 //
922 // When building with OpenJDK 8, the following should have no
923 // effect since those jars would be available by default.
924 //
925 // When building with OpenJDK 9 but targeting a version < 1.8,
926 // putting them on the bootclasspath means that:
927 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
928 // b) references to existing APIs are not reinterpreted in an
929 // OpenJDK 9-specific way, eg. calls to subclasses of
930 // java.nio.Buffer as in http://b/70862583
931 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
932 flags.bootClasspath = append(flags.bootClasspath,
933 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
934 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800935 if Bool(j.properties.Use_tools_jar) {
936 flags.bootClasspath = append(flags.bootClasspath,
937 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
938 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800939 }
940
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100941 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800942 // Manually specify build directory in case it is not under the repo root.
943 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
944 // just adding a symlink under the root doesn't help.)
945 patchPaths := ".:" + ctx.Config().BuildDir()
946 classPath := flags.classpath.FormJavaClassPath("")
947 if classPath != "" {
948 patchPaths += ":" + classPath
949 }
950 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700951 }
952
Nan Zhanged19fc32017-10-19 13:06:22 -0700953 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700954 if deps.systemModules != nil {
955 flags.systemModules = append(flags.systemModules, deps.systemModules)
956 }
957
Nan Zhanged19fc32017-10-19 13:06:22 -0700958 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700959 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700960
Colin Cross81440082018-08-15 20:21:55 -0700961 if len(javacFlags) > 0 {
962 // optimization.
963 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
964 flags.javacFlags = "$javacFlags"
965 }
966
Nan Zhanged19fc32017-10-19 13:06:22 -0700967 return flags
968}
Colin Crossc0b06f12015-04-08 13:03:43 -0700969
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700970func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
971
972 hasSrcs := false
Nan Zhanged19fc32017-10-19 13:06:22 -0700973
Colin Crossebe1a512017-11-14 13:12:14 -0800974 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700975
976 deps := j.collectDeps(ctx)
977 flags := j.collectBuilderFlags(ctx, deps)
978
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100979 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700980 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
981 }
Colin Cross8a497952019-03-05 22:25:09 -0800982 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700983 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800984 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700985 }
986
Colin Crossaf050172017-11-15 23:01:59 -0800987 srcFiles = j.genSources(ctx, srcFiles, flags)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700988 if len(srcFiles) > 0 {
989 hasSrcs = true
990 }
Colin Crossaf050172017-11-15 23:01:59 -0800991
992 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700993 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700994 if aaptSrcJar != nil {
995 srcJars = append(srcJars, aaptSrcJar)
996 }
Colin Crossb7a63242015-04-16 14:09:14 -0700997
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700998 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
999 // that IDEInfo struct will use
1000 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1001
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001002 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001003 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001004 }
1005
Colin Cross1ee23172017-10-18 14:44:18 -07001006 jarName := ctx.ModuleName() + ".jar"
1007
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001008 javaSrcFiles := srcFiles.FilterByExt(".java")
1009 var uniqueSrcFiles android.Paths
1010 set := make(map[string]bool)
1011 for _, v := range javaSrcFiles {
1012 if _, found := set[v.String()]; !found {
1013 set[v.String()] = true
1014 uniqueSrcFiles = append(uniqueSrcFiles, v)
1015 }
1016 }
1017
Colin Cross55f63ea2018-08-27 12:37:09 -07001018 var kotlinJars android.Paths
1019
Colin Cross93e85952017-08-15 13:34:18 -07001020 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001021 // user defined kotlin flags.
1022 kotlincFlags := j.properties.Kotlincflags
1023 CheckKotlincFlags(ctx, kotlincFlags)
1024
Colin Cross93e85952017-08-15 13:34:18 -07001025 // If there are kotlin files, compile them first but pass all the kotlin and java files
1026 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1027 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001028 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001029 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001030 kotlincFlags = append(kotlincFlags, "-no-jdk")
1031 }
1032 if len(kotlincFlags) > 0 {
1033 // optimization.
1034 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1035 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001036 }
1037
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001038 var kotlinSrcFiles android.Paths
1039 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1040 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1041
Colin Crossafbb1732019-01-17 15:42:52 -08001042 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1043 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1044
1045 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1046 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1047
1048 if len(flags.processorPath) > 0 {
1049 // Use kapt for annotation processing
1050 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1051 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1052 srcJars = append(srcJars, kaptSrcJar)
1053 // Disable annotation processing in javac, it's already been handled by kapt
1054 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001055 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001056 }
Colin Cross93e85952017-08-15 13:34:18 -07001057
Colin Cross1ee23172017-10-18 14:44:18 -07001058 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001059 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001060 if ctx.Failed() {
1061 return
1062 }
1063
1064 // Make javac rule depend on the kotlinc rule
1065 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001066
Colin Cross93e85952017-08-15 13:34:18 -07001067 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001068 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001069 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001070 }
1071
Colin Cross55f63ea2018-08-27 12:37:09 -07001072 jars := append(android.Paths(nil), kotlinJars...)
1073
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001074 // Store the list of .java files that was passed to javac
1075 j.compiledJavaSrcs = uniqueSrcFiles
1076 j.compiledSrcJars = srcJars
1077
Nan Zhang61eaedb2017-11-02 13:28:15 -07001078 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001079 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001080 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1081 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001082 // Formerly, there was a check here that prevented annotation processors
1083 // from being used when sharding was enabled, as some annotation processors
1084 // do not function correctly in sharded environments. It was removed to
1085 // allow for the use of annotation processors that do function correctly
1086 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001087 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001088 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001089 if ctx.Failed() {
1090 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001091 }
1092 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001093 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001094 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001095 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001096 // If error-prone is enabled, add an additional rule to compile the java files into
1097 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001098 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001099 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1100 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001101 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001102 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001103 extraJarDeps = append(extraJarDeps, errorprone)
1104 }
1105
Nan Zhang61eaedb2017-11-02 13:28:15 -07001106 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001107 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001108 shardSize := int(*(j.properties.Javac_shard_size))
1109 var shardSrcs []android.Paths
1110 if len(uniqueSrcFiles) > 0 {
1111 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1112 for idx, shardSrc := range shardSrcs {
1113 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1114 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1115 jars = append(jars, classes)
1116 }
1117 }
1118 if len(srcJars) > 0 {
1119 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1120 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1121 jars = append(jars, classes)
1122 }
1123 } else {
1124 classes := android.PathForModuleOut(ctx, "javac", jarName)
1125 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1126 jars = append(jars, classes)
1127 }
Colin Crossd6891432017-09-27 17:39:56 -07001128 if ctx.Failed() {
1129 return
1130 }
Colin Cross2fe66872015-03-30 17:20:39 -07001131 }
1132
Colin Cross0c4ce212019-05-03 15:28:19 -07001133 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1134
1135 var includeSrcJar android.WritablePath
1136 if Bool(j.properties.Include_srcs) {
1137 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1138 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1139 }
1140
Colin Crosscedd4762018-09-13 11:26:19 -07001141 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1142 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001143 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001144 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001145
1146 var resArgs []string
1147 var resDeps android.Paths
1148
1149 resArgs = append(resArgs, dirArgs...)
1150 resDeps = append(resDeps, dirDeps...)
1151
1152 resArgs = append(resArgs, fileArgs...)
1153 resDeps = append(resDeps, fileDeps...)
1154
Colin Cross988708c2019-05-06 14:04:11 -07001155 resArgs = append(resArgs, extraArgs...)
1156 resDeps = append(resDeps, extraDeps...)
1157
Colin Cross40a36712017-09-27 17:41:35 -07001158 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001159 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001160 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001161 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001162 if ctx.Failed() {
1163 return
1164 }
1165 }
1166
Colin Cross0c4ce212019-05-03 15:28:19 -07001167 var resourceJars android.Paths
1168 if j.resourceJar != nil {
1169 resourceJars = append(resourceJars, j.resourceJar)
1170 }
1171 if Bool(j.properties.Include_srcs) {
1172 resourceJars = append(resourceJars, includeSrcJar)
1173 }
1174 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001175
Colin Cross0c4ce212019-05-03 15:28:19 -07001176 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001177 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001178 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001179 false, nil, nil)
1180 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001181 } else if len(resourceJars) == 1 {
1182 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001183 }
1184
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001185 if len(deps.staticJars) > 0 {
1186 jars = append(jars, deps.staticJars...)
1187 hasSrcs = true
1188 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001189
Colin Cross094054a2018-10-17 15:10:48 -07001190 manifest := j.overrideManifest
1191 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001192 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001193 }
Colin Cross635acc92017-09-12 22:50:46 -07001194
Colin Cross8a497952019-03-05 22:25:09 -08001195 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001196 if len(services) > 0 {
1197 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1198 var zipargs []string
1199 for _, file := range services {
1200 serviceFile := file.String()
1201 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1202 }
1203 ctx.Build(pctx, android.BuildParams{
1204 Rule: zip,
1205 Output: servicesJar,
1206 Implicits: services,
1207 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001208 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001209 },
1210 })
1211 jars = append(jars, servicesJar)
1212 }
1213
Colin Cross0a6e0072017-08-30 14:24:55 -07001214 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001215 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001216 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001217
1218 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001219 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1220 // Optimization: skip the combine step if there is nothing to do
1221 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1222 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1223 // any if len(jars) == 1.
1224 outputFile = moduleOutPath
1225 } else {
1226 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1227 ctx.Build(pctx, android.BuildParams{
1228 Rule: android.Cp,
1229 Input: jars[0],
1230 Output: combinedJar,
1231 })
1232 outputFile = combinedJar
1233 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001234 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001235 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001236 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001237 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001238 outputFile = combinedJar
1239 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001240
Colin Cross331a1212018-08-15 20:40:52 -07001241 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001242 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001243 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001244 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001245 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001246 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001247
1248 // jarjar resource jar if necessary
1249 if j.resourceJar != nil {
1250 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001251 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001252 j.resourceJar = resourceJarJarFile
1253 }
1254
Colin Cross0a6e0072017-08-30 14:24:55 -07001255 if ctx.Failed() {
1256 return
1257 }
1258 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001259
1260 // Check package restrictions if necessary.
1261 if len(j.properties.Permitted_packages) > 0 {
1262 // Check packages and copy to package-checked file.
1263 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1264 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1265 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1266
1267 if ctx.Failed() {
1268 return
1269 }
1270 }
1271
Nan Zhanged19fc32017-10-19 13:06:22 -07001272 j.implementationJarFile = outputFile
1273 if j.headerJarFile == nil {
1274 j.headerJarFile = j.implementationJarFile
1275 }
Colin Cross2fe66872015-03-30 17:20:39 -07001276
Colin Cross6510f912017-11-29 00:27:14 -08001277 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001278 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1279 j.properties.Instrument = true
1280 }
1281 }
1282
Colin Cross3144dfc2018-01-03 15:06:47 -08001283 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001284 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1285 }
1286
Colin Cross331a1212018-08-15 20:40:52 -07001287 // merge implementation jar with resources if necessary
1288 implementationAndResourcesJar := outputFile
1289 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001290 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001291 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001292 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001293 false, nil, nil)
1294 implementationAndResourcesJar = combinedJar
1295 }
1296
1297 j.implementationAndResourcesJar = implementationAndResourcesJar
1298
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001299 if ctx.Device() && hasSrcs &&
1300 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001301 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001302 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001303 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001304 if ctx.Failed() {
1305 return
1306 }
Colin Cross331a1212018-08-15 20:40:52 -07001307
Colin Cross9c74a1e2019-05-28 14:06:17 -07001308 if !ctx.Config().UnbundledBuild() {
1309 // Hidden API CSV generation and dex encoding
1310 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1311 j.deviceProperties.UncompressDex)
1312 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001313
Colin Cross331a1212018-08-15 20:40:52 -07001314 // merge dex jar with resources if necessary
1315 if j.resourceJar != nil {
1316 jars := android.Paths{dexOutputFile, j.resourceJar}
1317 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1318 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1319 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001320 if j.deviceProperties.UncompressDex {
1321 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1322 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1323 dexOutputFile = combinedAlignedJar
1324 } else {
1325 dexOutputFile = combinedJar
1326 }
Colin Cross331a1212018-08-15 20:40:52 -07001327 }
1328
1329 j.dexJarFile = dexOutputFile
1330
Colin Cross8faf8fc2019-01-16 15:15:52 -08001331 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001332 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1333
1334 j.maybeStrippedDexJarFile = dexOutputFile
1335
Colin Cross3063b782018-08-15 11:19:12 -07001336 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001337
1338 if ctx.Failed() {
1339 return
1340 }
Colin Cross331a1212018-08-15 20:40:52 -07001341 } else {
1342 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001343 }
Colin Cross331a1212018-08-15 20:40:52 -07001344
Colin Crossb7a63242015-04-16 14:09:14 -07001345 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001346
1347 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1348 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001349}
1350
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001351// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1352// since some of these flags may be used internally.
1353func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1354 for _, flag := range flags {
1355 flag = strings.TrimSpace(flag)
1356
1357 if !strings.HasPrefix(flag, "-") {
1358 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1359 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1360 ctx.PropertyErrorf("kotlincflags",
1361 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1362 } else if inList(flag, config.KotlincIllegalFlags) {
1363 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1364 } else if flag == "-include-runtime" {
1365 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1366 } else {
1367 args := strings.Split(flag, " ")
1368 if args[0] == "-kotlin-home" {
1369 ctx.PropertyErrorf("kotlincflags",
1370 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1371 }
1372 }
1373 }
1374}
1375
Colin Cross8eadbf02017-10-24 17:46:00 -07001376func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001377 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001378
1379 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001380 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001381 // Compile java sources into turbine.jar.
1382 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1383 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1384 if ctx.Failed() {
1385 return nil
1386 }
1387 jars = append(jars, turbineJar)
1388 }
1389
Colin Cross55f63ea2018-08-27 12:37:09 -07001390 jars = append(jars, extraJars...)
1391
Nan Zhanged19fc32017-10-19 13:06:22 -07001392 // Combine any static header libraries into classes-header.jar. If there is only
1393 // one input jar this step will be skipped.
1394 var headerJar android.Path
1395 jars = append(jars, deps.staticHeaderJars...)
1396
Colin Cross5c6ecc12017-10-23 18:12:27 -07001397 // we cannot skip the combine step for now if there is only one jar
1398 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1399 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001400 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001401 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001402 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001403
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001404 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001405 // Transform classes.jar into classes-jarjar.jar
1406 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001407 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001408 headerJar = jarjarFile
1409 if ctx.Failed() {
1410 return nil
1411 }
1412 }
1413
1414 return headerJar
1415}
1416
Colin Crosscb933592017-11-22 13:49:43 -08001417func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001418 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001419
Colin Cross7a3139e2017-12-19 13:57:50 -08001420 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001421
Colin Cross84c38822018-01-03 15:59:46 -08001422 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001423 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1424
1425 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1426
1427 j.jacocoReportClassesFile = jacocoReportClassesFile
1428
1429 return instrumentedJar
1430}
1431
albaltai36ff7dc2018-12-25 14:35:23 +08001432var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001433
Nan Zhanged19fc32017-10-19 13:06:22 -07001434func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001435 if j.headerJarFile == nil {
1436 return nil
1437 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001438 return android.Paths{j.headerJarFile}
1439}
1440
1441func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001442 if j.implementationJarFile == nil {
1443 return nil
1444 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001445 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001446}
1447
Colin Crossf24a22a2019-01-31 14:12:44 -08001448func (j *Module) DexJar() android.Path {
1449 return j.dexJarFile
1450}
1451
Colin Cross331a1212018-08-15 20:40:52 -07001452func (j *Module) ResourceJars() android.Paths {
1453 if j.resourceJar == nil {
1454 return nil
1455 }
1456 return android.Paths{j.resourceJar}
1457}
1458
1459func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001460 if j.implementationAndResourcesJar == nil {
1461 return nil
1462 }
Colin Cross331a1212018-08-15 20:40:52 -07001463 return android.Paths{j.implementationAndResourcesJar}
1464}
1465
Colin Cross46c9b8b2017-06-22 16:51:17 -07001466func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001467 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001468 return j.exportAidlIncludeDirs
1469}
1470
Jiyong Park1be96912018-05-28 18:02:19 +09001471func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001472 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001473 return j.exportedSdkLibs
1474}
1475
Colin Cross0c4ce212019-05-03 15:28:19 -07001476func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1477 return j.srcJarArgs, j.srcJarDeps
1478}
1479
Colin Cross46c9b8b2017-06-22 16:51:17 -07001480var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001481
Colin Cross46c9b8b2017-06-22 16:51:17 -07001482func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001483 return j.logtagsSrcs
1484}
1485
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001486// Collect information for opening IDE project files in java/jdeps.go.
1487func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1488 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1489 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001490 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001491 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001492 if j.expandJarjarRules != nil {
1493 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001494 }
1495}
1496
1497func (j *Module) CompilerDeps() []string {
1498 jdeps := []string{}
1499 jdeps = append(jdeps, j.properties.Libs...)
1500 jdeps = append(jdeps, j.properties.Static_libs...)
1501 return jdeps
1502}
1503
Colin Cross2fe66872015-03-30 17:20:39 -07001504//
1505// Java libraries (.jar file)
1506//
1507
Colin Crossf506d872017-07-19 15:53:04 -07001508type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001509 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001510}
1511
Colin Cross42be7612019-02-21 18:12:14 -08001512func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001513 // Store uncompressed (and do not strip) dex files from boot class path jars.
1514 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1515 return true
1516 }
1517
1518 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001519 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001520 return true
1521 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001522 if ctx.Config().UncompressPrivAppDex() &&
1523 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1524 return true
1525 }
1526
Colin Cross2fc72f62018-12-21 12:59:54 -08001527 return false
1528}
1529
Colin Crossf506d872017-07-19 15:53:04 -07001530func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001531 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1532 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001533 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001534 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001535 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001536 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001537
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001538 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001539 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1540 ctx.ModuleName()+".jar", j.outputFile)
1541 }
Colin Crossb7a63242015-04-16 14:09:14 -07001542}
1543
Colin Crossf506d872017-07-19 15:53:04 -07001544func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001545 j.deps(ctx)
1546}
1547
Colin Cross1b16b0e2019-02-12 14:41:32 -08001548// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1549//
1550// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1551// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1552// as a `static_libs` dependency of another module.
1553//
1554// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1555// a device.
1556//
1557// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1558// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001559func LibraryFactory() android.Module {
1560 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001561
Colin Cross9ae1b922018-06-26 17:59:05 -07001562 module.AddProperties(
1563 &module.Module.properties,
1564 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001565 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001566 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001567
Colin Cross9ae1b922018-06-26 17:59:05 -07001568 InitJavaModule(module, android.HostAndDeviceSupported)
1569 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001570}
1571
Colin Cross1b16b0e2019-02-12 14:41:32 -08001572// java_library_static is an obsolete alias for java_library.
1573func LibraryStaticFactory() android.Module {
1574 return LibraryFactory()
1575}
1576
1577// java_library_host builds and links sources into a `.jar` file for the host.
1578//
1579// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1580// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001581func LibraryHostFactory() android.Module {
1582 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001583
Colin Cross6af17aa2017-09-20 12:59:05 -07001584 module.AddProperties(
1585 &module.Module.properties,
1586 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001587
Colin Cross9ae1b922018-06-26 17:59:05 -07001588 module.Module.properties.Installable = proptools.BoolPtr(true)
1589
Colin Cross89536d42017-07-07 14:35:50 -07001590 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001591 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001592}
1593
1594//
Colin Crossb628ea52018-08-14 16:42:33 -07001595// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001596//
1597
1598type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001599 // list of compatibility suites (for example "cts", "vts") that the module should be
1600 // installed into.
1601 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001602
1603 // the name of the test configuration (for example "AndroidTest.xml") that should be
1604 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001605 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001606
Jack He33338892018-09-19 02:21:28 -07001607 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1608 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001609 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001610
Colin Crossd96ca352018-08-10 16:06:24 -07001611 // list of files or filegroup modules that provide data that should be installed alongside
1612 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001613 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001614}
1615
Paul Duffin42df1442019-03-20 12:45:53 +00001616type testHelperLibraryProperties struct {
1617 // list of compatibility suites (for example "cts", "vts") that the module should be
1618 // installed into.
1619 Test_suites []string `android:"arch_variant"`
1620}
1621
Colin Cross05638fc2018-04-09 18:40:24 -07001622type Test struct {
1623 Library
1624
1625 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001626
1627 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001628 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001629}
1630
Paul Duffin42df1442019-03-20 12:45:53 +00001631type TestHelperLibrary struct {
1632 Library
1633
1634 testHelperLibraryProperties testHelperLibraryProperties
1635}
1636
Colin Cross303e21f2018-08-07 16:49:25 -07001637func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001638 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 -08001639 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001640
1641 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001642}
1643
Paul Duffin42df1442019-03-20 12:45:53 +00001644func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1645 j.Library.GenerateAndroidBuildActions(ctx)
1646}
1647
Colin Cross1b16b0e2019-02-12 14:41:32 -08001648// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1649// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1650//
1651// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1652// compiled against the device bootclasspath.
1653//
1654// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1655// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001656func TestFactory() android.Module {
1657 module := &Test{}
1658
1659 module.AddProperties(
1660 &module.Module.properties,
1661 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001662 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001663 &module.Module.protoProperties,
1664 &module.testProperties)
1665
Colin Cross9ae1b922018-06-26 17:59:05 -07001666 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001667 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001668
Colin Cross05638fc2018-04-09 18:40:24 -07001669 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001670 return module
1671}
1672
Paul Duffin42df1442019-03-20 12:45:53 +00001673// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1674func TestHelperLibraryFactory() android.Module {
1675 module := &TestHelperLibrary{}
1676
1677 module.AddProperties(
1678 &module.Module.properties,
1679 &module.Module.deviceProperties,
1680 &module.Module.dexpreoptProperties,
1681 &module.Module.protoProperties,
1682 &module.testHelperLibraryProperties)
1683
Colin Cross9a4abed2019-04-24 13:19:28 -07001684 module.Module.properties.Installable = proptools.BoolPtr(true)
1685 module.Module.dexpreopter.isTest = true
1686
Paul Duffin42df1442019-03-20 12:45:53 +00001687 InitJavaModule(module, android.HostAndDeviceSupported)
1688 return module
1689}
1690
Colin Cross1b16b0e2019-02-12 14:41:32 -08001691// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1692// allow running the test with `atest` or a `TEST_MAPPING` file.
1693//
1694// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1695// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001696func TestHostFactory() android.Module {
1697 module := &Test{}
1698
1699 module.AddProperties(
1700 &module.Module.properties,
1701 &module.Module.protoProperties,
1702 &module.testProperties)
1703
Colin Cross9ae1b922018-06-26 17:59:05 -07001704 module.Module.properties.Installable = proptools.BoolPtr(true)
1705
Colin Cross05638fc2018-04-09 18:40:24 -07001706 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001707 return module
1708}
1709
1710//
Colin Cross2fe66872015-03-30 17:20:39 -07001711// Java Binaries (.jar file plus wrapper script)
1712//
1713
Colin Crossf506d872017-07-19 15:53:04 -07001714type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001715 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001716 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001717
1718 // Name of the class containing main to be inserted into the manifest as Main-Class.
1719 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001720}
1721
Colin Crossf506d872017-07-19 15:53:04 -07001722type Binary struct {
1723 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001724
Colin Crossf506d872017-07-19 15:53:04 -07001725 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001726
Colin Cross6b4a32d2017-12-05 13:42:45 -08001727 isWrapperVariant bool
1728
Colin Crossc3315992017-12-08 19:12:36 -08001729 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001730 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001731}
1732
Alex Light24237172017-10-26 09:46:21 -07001733func (j *Binary) HostToolPath() android.OptionalPath {
1734 return android.OptionalPathForPath(j.binaryFile)
1735}
1736
Colin Crossf506d872017-07-19 15:53:04 -07001737func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001738 if ctx.Arch().ArchType == android.Common {
1739 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001740 if j.binaryProperties.Main_class != nil {
1741 if j.properties.Manifest != nil {
1742 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1743 }
1744 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1745 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1746 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1747 }
1748
Colin Cross6b4a32d2017-12-05 13:42:45 -08001749 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001750 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001751 // Handle the binary wrapper
1752 j.isWrapperVariant = true
1753
Colin Cross366938f2017-12-11 16:29:02 -08001754 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001755 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001756 } else {
1757 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1758 }
1759
1760 // Depend on the installed jar so that the wrapper doesn't get executed by
1761 // another build rule before the jar has been installed.
1762 jarFile := ctx.PrimaryModule().(*Binary).installFile
1763
1764 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1765 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001766 }
Colin Cross2fe66872015-03-30 17:20:39 -07001767}
1768
Colin Crossf506d872017-07-19 15:53:04 -07001769func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001770 if ctx.Arch().ArchType == android.Common {
1771 j.deps(ctx)
1772 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001773}
1774
Colin Cross1b16b0e2019-02-12 14:41:32 -08001775// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1776// as well.
1777//
1778// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1779// compiled against the device bootclasspath.
1780//
1781// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1782// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001783func BinaryFactory() android.Module {
1784 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001785
Colin Cross36242852017-06-23 15:06:31 -07001786 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001787 &module.Module.properties,
1788 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001789 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001790 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001791 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001792
Colin Cross9ae1b922018-06-26 17:59:05 -07001793 module.Module.properties.Installable = proptools.BoolPtr(true)
1794
Colin Cross6b4a32d2017-12-05 13:42:45 -08001795 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1796 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001797 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001798}
1799
Colin Cross1b16b0e2019-02-12 14:41:32 -08001800// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1801//
1802// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1803// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001804func BinaryHostFactory() android.Module {
1805 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001806
Colin Cross36242852017-06-23 15:06:31 -07001807 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001808 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001809 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001810 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001811
Colin Cross9ae1b922018-06-26 17:59:05 -07001812 module.Module.properties.Installable = proptools.BoolPtr(true)
1813
Colin Cross6b4a32d2017-12-05 13:42:45 -08001814 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1815 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001816 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001817}
1818
1819//
1820// Java prebuilts
1821//
1822
Colin Cross74d73e22017-08-02 11:05:49 -07001823type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001824 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001825
Nan Zhangea568a42017-11-08 21:20:04 -08001826 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001827
1828 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001829
1830 // List of shared java libs that this module has dependencies to
1831 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001832
1833 // List of files to remove from the jar file(s)
1834 Exclude_files []string
1835
1836 // List of directories to remove from the jar file(s)
1837 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001838
1839 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001840 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001841}
1842
1843type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001844 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001845 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001846 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001847
Colin Cross74d73e22017-08-02 11:05:49 -07001848 properties ImportProperties
1849
Colin Cross0a6e0072017-08-30 14:24:55 -07001850 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001851 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001852}
1853
Colin Cross83bb3162018-06-25 15:48:06 -07001854func (j *Import) sdkVersion() string {
1855 return String(j.properties.Sdk_version)
1856}
1857
1858func (j *Import) minSdkVersion() string {
1859 return j.sdkVersion()
1860}
1861
Colin Cross74d73e22017-08-02 11:05:49 -07001862func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001863 return &j.prebuilt
1864}
1865
Colin Cross74d73e22017-08-02 11:05:49 -07001866func (j *Import) PrebuiltSrcs() []string {
1867 return j.properties.Jars
1868}
1869
1870func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001871 return j.prebuilt.Name(j.ModuleBase.Name())
1872}
1873
Colin Cross74d73e22017-08-02 11:05:49 -07001874func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001875 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001876}
1877
Colin Cross74d73e22017-08-02 11:05:49 -07001878func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001879 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001880
Nan Zhang4c819fb2018-08-27 18:31:46 -07001881 jarName := ctx.ModuleName() + ".jar"
1882 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001883 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1884 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001885 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001886 inputFile := outputFile
1887 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1888 TransformJetifier(ctx, outputFile, inputFile)
1889 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001890 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001891
1892 ctx.VisitDirectDeps(func(module android.Module) {
1893 otherName := ctx.OtherModuleName(module)
1894 tag := ctx.OtherModuleDependencyTag(module)
1895
1896 switch dep := module.(type) {
1897 case Dependency:
1898 switch tag {
1899 case libTag, staticLibTag:
1900 // sdk lib names from dependencies are re-exported
1901 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1902 }
1903 case SdkLibraryDependency:
1904 switch tag {
1905 case libTag:
1906 // names of sdk libs that are directly depended are exported
1907 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1908 }
1909 }
1910 })
1911
1912 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001913 if Bool(j.properties.Installable) {
1914 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1915 ctx.ModuleName()+".jar", outputFile)
1916 }
Colin Cross2fe66872015-03-30 17:20:39 -07001917}
1918
Colin Cross74d73e22017-08-02 11:05:49 -07001919var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001920
Nan Zhanged19fc32017-10-19 13:06:22 -07001921func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001922 if j.combinedClasspathFile == nil {
1923 return nil
1924 }
Colin Cross37f6d792018-07-12 12:28:41 -07001925 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001926}
1927
1928func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001929 if j.combinedClasspathFile == nil {
1930 return nil
1931 }
Colin Cross37f6d792018-07-12 12:28:41 -07001932 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001933}
1934
Colin Cross331a1212018-08-15 20:40:52 -07001935func (j *Import) ResourceJars() android.Paths {
1936 return nil
1937}
1938
1939func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001940 if j.combinedClasspathFile == nil {
1941 return nil
1942 }
Colin Cross331a1212018-08-15 20:40:52 -07001943 return android.Paths{j.combinedClasspathFile}
1944}
1945
Colin Crossf24a22a2019-01-31 14:12:44 -08001946func (j *Import) DexJar() android.Path {
1947 return nil
1948}
1949
Colin Cross74d73e22017-08-02 11:05:49 -07001950func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001951 return nil
1952}
1953
Jiyong Park1be96912018-05-28 18:02:19 +09001954func (j *Import) ExportedSdkLibs() []string {
1955 return j.exportedSdkLibs
1956}
1957
Colin Cross0c4ce212019-05-03 15:28:19 -07001958func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1959 return nil, nil
1960}
1961
albaltai36ff7dc2018-12-25 14:35:23 +08001962// Add compile time check for interface implementation
1963var _ android.IDEInfo = (*Import)(nil)
1964var _ android.IDECustomizedModuleName = (*Import)(nil)
1965
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001966// Collect information for opening IDE project files in java/jdeps.go.
1967const (
1968 removedPrefix = "prebuilt_"
1969)
1970
1971func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1972 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1973}
1974
1975func (j *Import) IDECustomizedModuleName() string {
1976 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1977 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1978 // solution to get the Import name.
1979 name := j.Name()
1980 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001981 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001982 }
1983 return name
1984}
1985
Colin Cross74d73e22017-08-02 11:05:49 -07001986var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001987
Colin Cross1b16b0e2019-02-12 14:41:32 -08001988// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1989//
1990// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1991// compiled against an Android classpath.
1992//
1993// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1994// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001995func ImportFactory() android.Module {
1996 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001997
Colin Cross74d73e22017-08-02 11:05:49 -07001998 module.AddProperties(&module.properties)
1999
2000 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002001 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002002 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002003}
2004
Colin Cross1b16b0e2019-02-12 14:41:32 -08002005// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2006// module.
2007//
2008// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2009// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002010func ImportFactoryHost() android.Module {
2011 module := &Import{}
2012
2013 module.AddProperties(&module.properties)
2014
2015 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002016 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002017 return module
2018}
2019
Colin Cross42be7612019-02-21 18:12:14 -08002020// dex_import module
2021
2022type DexImportProperties struct {
2023 Jars []string
2024}
2025
2026type DexImport struct {
2027 android.ModuleBase
2028 android.DefaultableModuleBase
2029 prebuilt android.Prebuilt
2030
2031 properties DexImportProperties
2032
2033 dexJarFile android.Path
2034 maybeStrippedDexJarFile android.Path
2035
2036 dexpreopter
2037}
2038
2039func (j *DexImport) Prebuilt() *android.Prebuilt {
2040 return &j.prebuilt
2041}
2042
2043func (j *DexImport) PrebuiltSrcs() []string {
2044 return j.properties.Jars
2045}
2046
2047func (j *DexImport) Name() string {
2048 return j.prebuilt.Name(j.ModuleBase.Name())
2049}
2050
2051func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2052 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2053}
2054
2055func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2056 if len(j.properties.Jars) != 1 {
2057 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2058 }
2059
2060 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2061 j.dexpreopter.isInstallable = true
2062 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2063
2064 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2065 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2066
2067 if j.dexpreopter.uncompressedDex {
2068 rule := android.NewRuleBuilder()
2069
2070 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2071 rule.Temporary(temporary)
2072
2073 // use zip2zip to uncompress classes*.dex files
2074 rule.Command().
2075 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2076 FlagWithInput("-i ", inputJar).
2077 FlagWithOutput("-o ", temporary).
2078 FlagWithArg("-0 ", "'classes*.dex'")
2079
2080 // use zipalign to align uncompressed classes*.dex files
2081 rule.Command().
2082 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2083 Flag("-f").
2084 Text("4").
2085 Input(temporary).
2086 Output(dexOutputFile)
2087
2088 rule.DeleteTemporaryFiles()
2089
2090 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2091 } else {
2092 ctx.Build(pctx, android.BuildParams{
2093 Rule: android.Cp,
2094 Input: inputJar,
2095 Output: dexOutputFile,
2096 })
2097 }
2098
2099 j.dexJarFile = dexOutputFile
2100
2101 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2102
2103 j.maybeStrippedDexJarFile = dexOutputFile
2104
2105 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2106 ctx.ModuleName()+".jar", dexOutputFile)
2107}
2108
2109func (j *DexImport) DexJar() android.Path {
2110 return j.dexJarFile
2111}
2112
2113// dex_import imports a `.jar` file containing classes.dex files.
2114//
2115// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2116// to the device.
2117func DexImportFactory() android.Module {
2118 module := &DexImport{}
2119
2120 module.AddProperties(&module.properties)
2121
2122 android.InitPrebuiltModule(module, &module.properties.Jars)
2123 InitJavaModule(module, android.DeviceSupported)
2124 return module
2125}
2126
Colin Cross89536d42017-07-07 14:35:50 -07002127//
2128// Defaults
2129//
2130type Defaults struct {
2131 android.ModuleBase
2132 android.DefaultsModuleBase
2133}
2134
2135func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2136}
2137
Colin Cross1b16b0e2019-02-12 14:41:32 -08002138// java_defaults provides a set of properties that can be inherited by other java or android modules.
2139//
2140// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2141// property in the defaults module that exists in the depending module will be prepended to the depending module's
2142// value for that property.
2143//
2144// Example:
2145//
2146// java_defaults {
2147// name: "example_defaults",
2148// srcs: ["common/**/*.java"],
2149// javacflags: ["-Xlint:all"],
2150// aaptflags: ["--auto-add-overlay"],
2151// }
2152//
2153// java_library {
2154// name: "example",
2155// defaults: ["example_defaults"],
2156// srcs: ["example/**/*.java"],
2157// }
2158//
2159// is functionally identical to:
2160//
2161// java_library {
2162// name: "example",
2163// srcs: [
2164// "common/**/*.java",
2165// "example/**/*.java",
2166// ],
2167// javacflags: ["-Xlint:all"],
2168// }
Colin Cross89536d42017-07-07 14:35:50 -07002169func defaultsFactory() android.Module {
2170 return DefaultsFactory()
2171}
2172
2173func DefaultsFactory(props ...interface{}) android.Module {
2174 module := &Defaults{}
2175
2176 module.AddProperties(props...)
2177 module.AddProperties(
2178 &CompilerProperties{},
2179 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002180 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002181 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002182 &aaptProperties{},
2183 &androidLibraryProperties{},
2184 &appProperties{},
2185 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002186 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002187 &ImportProperties{},
2188 &AARImportProperties{},
2189 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002190 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002191 )
2192
2193 android.InitDefaultsModule(module)
2194
2195 return module
2196}
Nan Zhangea568a42017-11-08 21:20:04 -08002197
2198var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002199var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002200var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002201var inList = android.InList