blob: 4b3845161108fcb4a624eab4ebc816b0a2cd49d3 [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 framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070086 No_framework_libs *bool
87
Colin Cross7d5136f2015-05-11 13:39:40 -070088 // list of module-specific flags that will be used for javac compiles
89 Javacflags []string `android:"arch_variant"`
90
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020091 // list of module-specific flags that will be used for kotlinc compiles
92 Kotlincflags []string `android:"arch_variant"`
93
Colin Cross7d5136f2015-05-11 13:39:40 -070094 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070095 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070096
97 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070098 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070099
100 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800101 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700102
Colin Cross540eff82017-06-22 17:01:52 -0700103 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800104 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700105
106 // If not blank, set the java version passed to javac as -source and -target
107 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700108
Colin Cross9ae1b922018-06-26 17:59:05 -0700109 // If set to true, allow this module to be dexed and installed on devices. Has no
110 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700111 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700112
Colin Cross0f37af02017-09-27 17:42:05 -0700113 // If set to true, include sources used to compile the module in to the final jar
114 Include_srcs *bool
115
Vladimir Marko0975ee02019-04-02 10:29:55 +0100116 // If not empty, classes are restricted to the specified packages and their sub-packages.
117 // This restriction is checked after applying jarjar rules and including static libs.
118 Permitted_packages []string
119
Colin Crossbe9cdb82019-01-21 21:37:16 -0800120 // List of modules to use as annotation processors
121 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700122
Nan Zhang61eaedb2017-11-02 13:28:15 -0700123 // The number of Java source entries each Javac instance can process
124 Javac_shard_size *int64
125
Nan Zhang5f8cb422018-02-06 10:34:32 -0800126 // Add host jdk tools.jar to bootclasspath
127 Use_tools_jar *bool
128
Colin Cross1369cdb2017-09-29 17:58:17 -0700129 Openjdk9 struct {
130 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800131 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700132
133 // List of javac flags that should only be used when passing -source 1.9
134 Javacflags []string
135 }
Colin Crosscb933592017-11-22 13:49:43 -0800136
Colin Cross81440082018-08-15 20:21:55 -0700137 // When compiling language level 9+ .java code in packages that are part of
138 // a system module, patch_module names the module that your sources and
139 // dependencies should be patched into. The Android runtime currently
140 // doesn't implement the JEP 261 module system so this option is only
141 // supported at compile time. It should only be needed to compile tests in
142 // packages that exist in libcore and which are inconvenient to move
143 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100144 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700145
Colin Crosscb933592017-11-22 13:49:43 -0800146 Jacoco struct {
147 // List of classes to include for instrumentation with jacoco to collect coverage
148 // information at runtime when building with coverage enabled. If unset defaults to all
149 // classes.
150 // Supports '*' as the last character of an entry in the list as a wildcard match.
151 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
152 // it matches classes in the package that have the class name as a prefix.
153 Include_filter []string
154
155 // List of classes to exclude from instrumentation with jacoco to collect coverage
156 // information at runtime when building with coverage enabled. Overrides classes selected
157 // by the include_filter property.
158 // Supports '*' as the last character of an entry in the list as a wildcard match.
159 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
160 // it matches classes in the package that have the class name as a prefix.
161 Exclude_filter []string
162 }
163
Andreas Gampef3e5b552018-01-22 21:27:21 -0800164 Errorprone struct {
165 // List of javac flags that should only be used when running errorprone.
166 Javacflags []string
167 }
168
Colin Cross0f2ee152017-12-14 15:22:43 -0800169 Proto struct {
170 // List of extra options that will be passed to the proto generator.
171 Output_params []string
172 }
173
Colin Crosscb933592017-11-22 13:49:43 -0800174 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800175
176 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800177 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700178}
179
Colin Cross89536d42017-07-07 14:35:50 -0700180type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700181 // list of module-specific flags that will be used for dex compiles
182 Dxflags []string `android:"arch_variant"`
183
Colin Cross83bb3162018-06-25 15:48:06 -0700184 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
185 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800186 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700187
Colin Cross83bb3162018-06-25 15:48:06 -0700188 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
189 // Defaults to sdk_version if not set.
190 Min_sdk_version *string
191
Dan Willemsen419290a2018-10-31 15:28:47 -0700192 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
193 // Defaults to sdk_version if not set.
194 Target_sdk_version *string
195
Colin Cross6af2e492018-05-22 11:12:33 -0700196 // if true, compile against the platform APIs instead of an SDK.
197 Platform_apis *bool
198
Colin Crossebe1a512017-11-14 13:12:14 -0800199 Aidl struct {
200 // Top level directories to pass to aidl tool
201 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700202
Colin Crossebe1a512017-11-14 13:12:14 -0800203 // Directories rooted at the Android.bp file to pass to aidl tool
204 Local_include_dirs []string
205
206 // directories that should be added as include directories for any aidl sources of modules
207 // that depend on this module, as well as to aidl for this module.
208 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100209
210 // whether to generate traces (for systrace) for this interface
211 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100212
213 // whether to generate Binder#GetTransaction name method.
214 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800215 }
Colin Cross92430102017-10-09 14:59:32 -0700216
217 // If true, export a copy of the module as a -hostdex module for host testing.
218 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700219
Colin Cross7f87f4f2019-04-24 13:41:45 -0700220 Target struct {
221 Hostdex struct {
222 // Additional required dependencies to add to -hostdex modules.
223 Required []string
224 }
225 }
226
David Brazdil17ef5632018-06-27 10:27:45 +0100227 // If set to true, compile dex regardless of installable. Defaults to false.
228 Compile_dex *bool
229
Colin Cross66dbc0b2017-12-28 12:23:20 -0800230 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700231 // If false, disable all optimization. Defaults to true for android_app and android_test
232 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800233 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700234 // True if the module containing this has it set by default.
235 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800236
237 // If true, optimize for size by removing unused code. Defaults to true for apps,
238 // false for libraries and tests.
239 Shrink *bool
240
241 // If true, optimize bytecode. Defaults to false.
242 Optimize *bool
243
244 // If true, obfuscate bytecode. Defaults to false.
245 Obfuscate *bool
246
247 // If true, do not use the flag files generated by aapt that automatically keep
248 // classes referenced by the app manifest. Defaults to false.
249 No_aapt_flags *bool
250
251 // Flags to pass to proguard.
252 Proguard_flags []string
253
254 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800255 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800256 }
257
Colin Cross1369cdb2017-09-29 17:58:17 -0700258 // When targeting 1.9, override the modules to use with --system
259 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700260
261 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800262 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700263}
264
Sasha Smundak2057f822019-04-16 17:16:58 -0700265func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
266 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
267}
268
Colin Cross46c9b8b2017-06-22 16:51:17 -0700269// Module contains the properties and members used by all java module types
270type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700271 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700272 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700273
Colin Cross89536d42017-07-07 14:35:50 -0700274 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700275 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700276 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700277
Colin Cross331a1212018-08-15 20:40:52 -0700278 // jar file containing header classes including static library dependencies, suitable for
279 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700280 headerJarFile android.Path
281
Colin Cross331a1212018-08-15 20:40:52 -0700282 // jar file containing implementation classes including static library dependencies but no
283 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700285
Colin Cross331a1212018-08-15 20:40:52 -0700286 // jar file containing only resources including from static library dependencies
287 resourceJar android.Path
288
Colin Cross0c4ce212019-05-03 15:28:19 -0700289 // args and dependencies to package source files into a srcjar
290 srcJarArgs []string
291 srcJarDeps android.Paths
292
Colin Cross331a1212018-08-15 20:40:52 -0700293 // jar file containing implementation classes and resources including static library
294 // dependencies
295 implementationAndResourcesJar android.Path
296
297 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700298 dexJarFile android.Path
299
Colin Cross43f08db2018-11-12 10:13:39 -0800300 // output file that contains classes.dex if it should be in the output file
301 maybeStrippedDexJarFile android.Path
302
Colin Crosscb933592017-11-22 13:49:43 -0800303 // output file containing uninstrumented classes that will be instrumented by jacoco
304 jacocoReportClassesFile android.Path
305
Colin Cross66dbc0b2017-12-28 12:23:20 -0800306 // output file containing mapping of obfuscated names
307 proguardDictionary android.Path
308
Colin Cross331a1212018-08-15 20:40:52 -0700309 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700310 outputFile android.Path
311 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700312
Colin Cross635c3b02016-05-18 15:37:25 -0700313 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700314
Colin Cross635c3b02016-05-18 15:37:25 -0700315 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700316
Colin Cross2fe66872015-03-30 17:20:39 -0700317 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700318 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800319
320 // list of .java files and srcjars that was passed to javac
321 compiledJavaSrcs android.Paths
322 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800323
324 // list of extra progurad flag files
325 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900326
Colin Cross094054a2018-10-17 15:10:48 -0700327 // manifest file to use instead of properties.Manifest
328 overrideManifest android.OptionalPath
329
Jiyong Park1be96912018-05-28 18:02:19 +0900330 // list of SDK lib names that this java moudule is exporting
331 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700332
333 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
334 // filter out Exclude_srcs, will be used by android.IDEInfo struct
335 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800336
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800337 // expanded Jarjar_rules
338 expandJarjarRules android.Path
339
Vladimir Marko0975ee02019-04-02 10:29:55 +0100340 // list of additional targets for checkbuild
341 additionalCheckedModules android.Paths
342
Colin Cross988708c2019-05-06 14:04:11 -0700343 // Extra files generated by the module type to be added as java resources.
344 extraResources android.Paths
345
Colin Crossf24a22a2019-01-31 14:12:44 -0800346 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800347 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700348}
349
Colin Cross41955e82019-05-29 14:40:35 -0700350func (j *Module) OutputFiles(tag string) (android.Paths, error) {
351 switch tag {
352 case "":
353 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700354 case ".jar":
355 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700356 default:
357 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
358 }
Colin Cross54250902017-12-05 09:28:08 -0800359}
360
Jiyong Park8fd61922018-11-08 02:50:25 +0900361func (j *Module) DexJarFile() android.Path {
362 return j.dexJarFile
363}
364
Colin Cross41955e82019-05-29 14:40:35 -0700365var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800366
Colin Crossf506d872017-07-19 15:53:04 -0700367type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700368 HeaderJars() android.Paths
369 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700370 ResourceJars() android.Paths
371 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800372 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700373 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900374 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700375 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700376}
377
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700379 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
380 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900381}
382
Nan Zhangb2b33de2018-02-23 11:18:47 -0800383type SrcDependency interface {
384 CompiledSrcs() android.Paths
385 CompiledSrcJars() android.Paths
386}
387
388func (j *Module) CompiledSrcs() android.Paths {
389 return j.compiledJavaSrcs
390}
391
392func (j *Module) CompiledSrcJars() android.Paths {
393 return j.compiledSrcJars
394}
395
396var _ SrcDependency = (*Module)(nil)
397
Colin Cross89536d42017-07-07 14:35:50 -0700398func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
399 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
400 android.InitDefaultableModule(module)
401}
402
Colin Crossbe1da472017-07-07 15:59:46 -0700403type dependencyTag struct {
404 blueprint.BaseDependencyTag
405 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700406}
407
Colin Crossa4f08812018-10-02 22:03:40 -0700408type jniDependencyTag struct {
409 blueprint.BaseDependencyTag
410 target android.Target
411}
412
Colin Crossbe1da472017-07-07 15:59:46 -0700413var (
Colin Cross4b964c02018-10-15 16:18:06 -0700414 staticLibTag = dependencyTag{name: "staticlib"}
415 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800416 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700417 bootClasspathTag = dependencyTag{name: "bootclasspath"}
418 systemModulesTag = dependencyTag{name: "system modules"}
419 frameworkResTag = dependencyTag{name: "framework-res"}
420 frameworkApkTag = dependencyTag{name: "framework-apk"}
421 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800422 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700423 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
424 certificateTag = dependencyTag{name: "certificate"}
425 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700426 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700427)
Colin Cross2fe66872015-03-30 17:20:39 -0700428
Colin Crossfc3674a2017-09-18 17:41:52 -0700429type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700430 useModule, useFiles, useDefaultLibs, invalidVersion bool
431
Colin Cross86a60ae2018-05-29 14:44:55 -0700432 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700433 systemModules string
434
Colin Crossa97c5d32018-03-28 14:58:31 -0700435 frameworkResModule string
436
Colin Cross86a60ae2018-05-29 14:44:55 -0700437 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700438 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100439
440 noStandardLibs, noFrameworksLibs bool
441}
442
443func (s sdkDep) hasStandardLibs() bool {
444 return !s.noStandardLibs
445}
446
447func (s sdkDep) hasFrameworkLibs() bool {
448 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700449}
450
Colin Crossa4f08812018-10-02 22:03:40 -0700451type jniLib struct {
452 name string
453 path android.Path
454 target android.Target
455}
456
Colin Cross0ea8ba82019-06-06 14:33:29 -0700457func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800458 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
459}
460
Colin Cross0ea8ba82019-06-06 14:33:29 -0700461func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800462 return j.shouldInstrument(ctx) &&
463 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
464 ctx.Config().UnbundledBuild())
465}
466
Colin Cross83bb3162018-06-25 15:48:06 -0700467func (j *Module) sdkVersion() string {
468 return String(j.deviceProperties.Sdk_version)
469}
470
471func (j *Module) minSdkVersion() string {
472 if j.deviceProperties.Min_sdk_version != nil {
473 return *j.deviceProperties.Min_sdk_version
474 }
475 return j.sdkVersion()
476}
477
Dan Willemsen419290a2018-10-31 15:28:47 -0700478func (j *Module) targetSdkVersion() string {
479 if j.deviceProperties.Target_sdk_version != nil {
480 return *j.deviceProperties.Target_sdk_version
481 }
482 return j.sdkVersion()
483}
484
Paul Duffin250e6192019-06-07 10:44:37 +0100485func (j *Module) noFrameworkLibs() bool {
486 return Bool(j.properties.No_framework_libs)
487}
488
Colin Crossbe1da472017-07-07 15:59:46 -0700489func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700490 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100491 sdkDep := decodeSdkDep(ctx, sdkContext(j))
492 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700493 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700494 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100495 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100496 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700497 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700498 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700499 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100500 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700501 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700502 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700503 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
504 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800505 }
Colin Crossbe1da472017-07-07 15:59:46 -0700506 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700507 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100508 ctx.PropertyErrorf("sdk_version",
509 `system_modules is required to be set when sdk_version is "none", did you mean no_framework_libs?`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100510 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700511 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700512 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100513 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700514 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800515 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800516 if ctx.ModuleName() == "android_stubs_current" ||
517 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700518 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700519 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800520 }
Colin Cross2fe66872015-03-30 17:20:39 -0700521 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700522
Colin Cross42d48b72018-08-29 14:10:52 -0700523 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
524 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700525
Colin Crossbe9cdb82019-01-21 21:37:16 -0800526 ctx.AddFarVariationDependencies([]blueprint.Variation{
527 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
528 }, pluginTag, j.properties.Plugins...)
529
Colin Crossfe17f6f2019-03-28 19:30:56 -0700530 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700531 if j.hasSrcExt(".proto") {
532 protoDeps(ctx, &j.protoProperties)
533 }
Colin Cross93e85952017-08-15 13:34:18 -0700534
535 if j.hasSrcExt(".kt") {
536 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
537 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700538 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
539 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800540 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800541 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
542 }
Colin Cross93e85952017-08-15 13:34:18 -0700543 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800544
545 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700546 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800547 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700548}
549
550func hasSrcExt(srcs []string, ext string) bool {
551 for _, src := range srcs {
552 if filepath.Ext(src) == ext {
553 return true
554 }
555 }
556
557 return false
558}
559
Nan Zhang61eaedb2017-11-02 13:28:15 -0700560func shardPaths(paths android.Paths, shardSize int) []android.Paths {
561 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
562 for len(paths) > shardSize {
563 ret = append(ret, paths[0:shardSize])
564 paths = paths[shardSize:]
565 }
566 if len(paths) > 0 {
567 ret = append(ret, paths)
568 }
569 return ret
570}
571
Colin Cross6af17aa2017-09-20 12:59:05 -0700572func (j *Module) hasSrcExt(ext string) bool {
573 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700574}
575
Colin Cross46c9b8b2017-06-22 16:51:17 -0700576func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700577 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700578
Colin Crossebe1a512017-11-14 13:12:14 -0800579 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
580 aidlIncludes = append(aidlIncludes,
581 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
582 aidlIncludes = append(aidlIncludes,
583 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700584
Colin Cross3047fa22019-04-18 10:56:44 -0700585 var flags []string
586 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700587
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700588 if aidlPreprocess.Valid() {
589 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700590 deps = append(deps, aidlPreprocess.Path())
591 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700592 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700593 }
594
Colin Cross3047fa22019-04-18 10:56:44 -0700595 if len(j.exportAidlIncludeDirs) > 0 {
596 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
597 }
598
599 if len(aidlIncludes) > 0 {
600 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
601 }
602
Colin Cross635c3b02016-05-18 15:37:25 -0700603 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800604 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700605 flags = append(flags, "-I"+src.String())
606 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700607
Martijn Coeneneab15642018-03-09 09:29:59 +0100608 if Bool(j.deviceProperties.Aidl.Generate_traces) {
609 flags = append(flags, "-t")
610 }
611
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100612 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
613 flags = append(flags, "--transaction_names")
614 }
615
Colin Cross3047fa22019-04-18 10:56:44 -0700616 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700617}
618
Colin Cross32f676a2017-09-06 13:41:06 -0700619type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800620 classpath classpath
621 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700622 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800623 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700624 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700625 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700626 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700627 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800628 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700629 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700630 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700631 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700632 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800633 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800634
635 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700636}
Colin Cross2fe66872015-03-30 17:20:39 -0700637
Colin Cross54250902017-12-05 09:28:08 -0800638func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
639 for _, f := range dep.Srcs() {
640 if f.Ext() != ".jar" {
641 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
642 ctx.OtherModuleName(dep.(blueprint.Module)))
643 }
644 }
645}
646
Jiyong Park2d492942018-03-05 17:44:10 +0900647type linkType int
648
649const (
650 javaCore linkType = iota
651 javaSdk
652 javaSystem
653 javaPlatform
654)
655
Jiyong Park46f78fb2018-10-20 16:33:17 +0900656func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700657 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700658 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900659 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
660 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100661 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900662 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100663 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900664 return javaCore, false
665 case name == "android_system_stubs_current":
666 return javaSystem, true
667 case strings.HasPrefix(ver, "system_"):
668 return javaSystem, false
669 case name == "android_test_stubs_current":
670 return javaSystem, true
671 case strings.HasPrefix(ver, "test_"):
672 return javaPlatform, false
673 case name == "android_stubs_current":
674 return javaSdk, true
675 case ver == "current":
676 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100677 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900678 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700679 default:
680 if _, err := strconv.Atoi(ver); err != nil {
681 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
682 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900683 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900684 }
685}
686
Jiyong Park750e5572018-01-31 00:20:13 +0900687func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700688 if ctx.Host() {
689 return
690 }
691
Jiyong Park46f78fb2018-10-20 16:33:17 +0900692 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
693 if stubs {
694 return
695 }
696 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900697 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."
698
699 switch myLinkType {
700 case javaCore:
701 if otherLinkType != javaCore {
702 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 +0900703 ctx.OtherModuleName(to))
704 }
Jiyong Park2d492942018-03-05 17:44:10 +0900705 break
706 case javaSdk:
707 if otherLinkType != javaCore && otherLinkType != javaSdk {
708 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
709 ctx.OtherModuleName(to))
710 }
711 break
712 case javaSystem:
713 if otherLinkType == javaPlatform {
714 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
715 ctx.OtherModuleName(to))
716 }
717 break
718 case javaPlatform:
719 // no restriction on link-type
720 break
Jiyong Park750e5572018-01-31 00:20:13 +0900721 }
722}
723
Colin Cross32f676a2017-09-06 13:41:06 -0700724func (j *Module) collectDeps(ctx android.ModuleContext) deps {
725 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700726
Colin Cross300f0382018-03-06 13:11:51 -0800727 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700728 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800729 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700730 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800731 } else if sdkDep.useFiles {
732 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700733 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700734 deps.aidlPreprocess = sdkDep.aidl
735 } else {
736 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800737 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700738 }
739
Colin Crossd11fcda2017-10-23 17:59:01 -0700740 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700741 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700742 tag := ctx.OtherModuleDependencyTag(module)
743
Colin Crossa4f08812018-10-02 22:03:40 -0700744 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700745 // Handled by AndroidApp.collectAppDeps
746 return
747 }
748 if tag == certificateTag {
749 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700750 return
751 }
752
Jiyong Park750e5572018-01-31 00:20:13 +0900753 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700754 switch tag {
755 case bootClasspathTag, libTag, staticLibTag:
756 checkLinkType(ctx, j, to, tag.(dependencyTag))
757 }
Jiyong Park750e5572018-01-31 00:20:13 +0900758 }
Colin Cross54250902017-12-05 09:28:08 -0800759 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800760 case SdkLibraryDependency:
761 switch tag {
762 case libTag:
763 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
764 // names of sdk libs that are directly depended are exported
765 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700766 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800767 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
768 }
Colin Cross54250902017-12-05 09:28:08 -0800769 case Dependency:
770 switch tag {
771 case bootClasspathTag:
772 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700773 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800774 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900775 // sdk lib names from dependencies are re-exported
776 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700777 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800778 case staticLibTag:
779 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
780 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
781 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700782 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900783 // sdk lib names from dependencies are re-exported
784 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700785 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800786 case pluginTag:
787 if plugin, ok := dep.(*Plugin); ok {
788 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
789 if plugin.pluginProperties.Processor_class != nil {
790 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
791 }
792 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
793 } else {
794 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
795 }
Colin Cross54250902017-12-05 09:28:08 -0800796 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100797 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800798 // framework.jar has a one-off dependency on the R.java and Manifest.java files
799 // generated by framework-res.apk
800 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
801 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800802 case frameworkApkTag:
803 if ctx.ModuleName() == "android_stubs_current" ||
804 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700805 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800806 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
807 // resource files out of there for aapt.
808 //
809 // Normally the package rule runs aapt, which includes the resource,
810 // but we're not running that in our package rule so just copy in the
811 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700812 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800813 }
Colin Cross54250902017-12-05 09:28:08 -0800814 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700815 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800816 case kotlinAnnotationsTag:
817 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800818 }
819
Colin Cross54250902017-12-05 09:28:08 -0800820 case android.SourceFileProducer:
821 switch tag {
822 case libTag:
823 checkProducesJars(ctx, dep)
824 deps.classpath = append(deps.classpath, dep.Srcs()...)
825 case staticLibTag:
826 checkProducesJars(ctx, dep)
827 deps.classpath = append(deps.classpath, dep.Srcs()...)
828 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
829 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800830 }
831 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700832 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700833 case systemModulesTag:
834 if deps.systemModules != nil {
835 panic("Found two system module dependencies")
836 }
837 sm := module.(*SystemModules)
838 if sm.outputFile == nil {
839 panic("Missing directory for system module dependency")
840 }
841 deps.systemModules = sm.outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700842 }
Colin Crossec7a0422017-07-07 14:47:12 -0700843 }
Colin Cross2fe66872015-03-30 17:20:39 -0700844 })
845
Jiyong Park1be96912018-05-28 18:02:19 +0900846 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
847
Colin Cross32f676a2017-09-06 13:41:06 -0700848 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700849}
850
Colin Cross83bb3162018-06-25 15:48:06 -0700851func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700852 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800853 v := sdkContext.sdkVersion()
854 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100855 if ctx.Config().IsPdkBuild() &&
856 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700857 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800858 latestSdkVersion := 0
859 if len(sdkVersions) > 0 {
860 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
861 }
862 v = strconv.Itoa(latestSdkVersion)
863 }
864
865 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700866 if err != nil {
867 ctx.PropertyErrorf("sdk_version", "%s", err)
868 }
Nan Zhang357466b2018-04-17 17:38:36 -0700869 if javaVersion != "" {
870 ret = javaVersion
871 } else if ctx.Device() && sdk <= 23 {
872 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100873 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700874 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100875 } else if ctx.Device() &&
876 sdkContext.sdkVersion() != "" &&
877 sdkContext.sdkVersion() != "none" &&
878 sdkContext.sdkVersion() != "core_platform" &&
879 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700880 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
881 ret = "1.8"
882 } else {
883 ret = "1.9"
884 }
885
886 return ret
887}
888
Nan Zhanged19fc32017-10-19 13:06:22 -0700889func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700890
Colin Crossf03c82b2015-04-13 13:53:40 -0700891 var flags javaBuilderFlags
892
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100893 // javaVersion flag.
894 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
895
Nan Zhanged19fc32017-10-19 13:06:22 -0700896 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700897 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100898 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700899 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700900 }
Colin Cross6510f912017-11-29 00:27:14 -0800901 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700902 // Override the -g flag passed globally to remove local variable debug info to reduce
903 // disk and memory usage.
904 javacFlags = append(javacFlags, "-g:source,lines")
905 }
Colin Cross64162712017-08-08 13:17:59 -0700906
Colin Cross66548102018-06-19 22:47:35 -0700907 if ctx.Config().RunErrorProne() {
908 if config.ErrorProneClasspath == nil {
909 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
910 }
911
912 errorProneFlags := []string{
913 "-Xplugin:ErrorProne",
914 "${config.ErrorProneChecks}",
915 }
916 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
917
918 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
919 "'" + strings.Join(errorProneFlags, " ") + "'"
920 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800921 }
922
Nan Zhanged19fc32017-10-19 13:06:22 -0700923 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800924 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
925 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700926 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800927
Colin Crossbe9cdb82019-01-21 21:37:16 -0800928 flags.processor = strings.Join(deps.processorClasses, ",")
929
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100930 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100931 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800932 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
933 // Give host-side tools a version of OpenJDK's standard libraries
934 // close to what they're targeting. As of Dec 2017, AOSP is only
935 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
936 //
937 // When building with OpenJDK 8, the following should have no
938 // effect since those jars would be available by default.
939 //
940 // When building with OpenJDK 9 but targeting a version < 1.8,
941 // putting them on the bootclasspath means that:
942 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
943 // b) references to existing APIs are not reinterpreted in an
944 // OpenJDK 9-specific way, eg. calls to subclasses of
945 // java.nio.Buffer as in http://b/70862583
946 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
947 flags.bootClasspath = append(flags.bootClasspath,
948 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
949 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800950 if Bool(j.properties.Use_tools_jar) {
951 flags.bootClasspath = append(flags.bootClasspath,
952 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
953 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800954 }
955
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100956 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800957 // Manually specify build directory in case it is not under the repo root.
958 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
959 // just adding a symlink under the root doesn't help.)
960 patchPaths := ".:" + ctx.Config().BuildDir()
961 classPath := flags.classpath.FormJavaClassPath("")
962 if classPath != "" {
963 patchPaths += ":" + classPath
964 }
965 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700966 }
967
Nan Zhanged19fc32017-10-19 13:06:22 -0700968 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700969 if deps.systemModules != nil {
970 flags.systemModules = append(flags.systemModules, deps.systemModules)
971 }
972
Nan Zhanged19fc32017-10-19 13:06:22 -0700973 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700974 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700975
Colin Cross81440082018-08-15 20:21:55 -0700976 if len(javacFlags) > 0 {
977 // optimization.
978 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
979 flags.javacFlags = "$javacFlags"
980 }
981
Nan Zhanged19fc32017-10-19 13:06:22 -0700982 return flags
983}
Colin Crossc0b06f12015-04-08 13:03:43 -0700984
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700985func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
986
Colin Crossebe1a512017-11-14 13:12:14 -0800987 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700988
989 deps := j.collectDeps(ctx)
990 flags := j.collectBuilderFlags(ctx, deps)
991
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100992 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700993 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
994 }
Colin Cross8a497952019-03-05 22:25:09 -0800995 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700996 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800997 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700998 }
999
Colin Crossaf050172017-11-15 23:01:59 -08001000 srcFiles = j.genSources(ctx, srcFiles, flags)
1001
1002 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001003 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001004 if aaptSrcJar != nil {
1005 srcJars = append(srcJars, aaptSrcJar)
1006 }
Colin Crossb7a63242015-04-16 14:09:14 -07001007
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001008 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1009 // that IDEInfo struct will use
1010 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1011
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001012 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001013 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001014 }
1015
Colin Cross1ee23172017-10-18 14:44:18 -07001016 jarName := ctx.ModuleName() + ".jar"
1017
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001018 javaSrcFiles := srcFiles.FilterByExt(".java")
1019 var uniqueSrcFiles android.Paths
1020 set := make(map[string]bool)
1021 for _, v := range javaSrcFiles {
1022 if _, found := set[v.String()]; !found {
1023 set[v.String()] = true
1024 uniqueSrcFiles = append(uniqueSrcFiles, v)
1025 }
1026 }
1027
Colin Cross55f63ea2018-08-27 12:37:09 -07001028 var kotlinJars android.Paths
1029
Colin Cross93e85952017-08-15 13:34:18 -07001030 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001031 // user defined kotlin flags.
1032 kotlincFlags := j.properties.Kotlincflags
1033 CheckKotlincFlags(ctx, kotlincFlags)
1034
Colin Cross93e85952017-08-15 13:34:18 -07001035 // If there are kotlin files, compile them first but pass all the kotlin and java files
1036 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1037 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001038 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001039 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001040 kotlincFlags = append(kotlincFlags, "-no-jdk")
1041 }
1042 if len(kotlincFlags) > 0 {
1043 // optimization.
1044 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1045 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001046 }
1047
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001048 var kotlinSrcFiles android.Paths
1049 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1050 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1051
Colin Crossafbb1732019-01-17 15:42:52 -08001052 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1053 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1054
1055 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1056 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1057
1058 if len(flags.processorPath) > 0 {
1059 // Use kapt for annotation processing
1060 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1061 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1062 srcJars = append(srcJars, kaptSrcJar)
1063 // Disable annotation processing in javac, it's already been handled by kapt
1064 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001065 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001066 }
Colin Cross93e85952017-08-15 13:34:18 -07001067
Colin Cross1ee23172017-10-18 14:44:18 -07001068 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001069 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001070 if ctx.Failed() {
1071 return
1072 }
1073
1074 // Make javac rule depend on the kotlinc rule
1075 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001076
Colin Cross93e85952017-08-15 13:34:18 -07001077 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001078 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001079 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001080 }
1081
Colin Cross55f63ea2018-08-27 12:37:09 -07001082 jars := append(android.Paths(nil), kotlinJars...)
1083
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001084 // Store the list of .java files that was passed to javac
1085 j.compiledJavaSrcs = uniqueSrcFiles
1086 j.compiledSrcJars = srcJars
1087
Nan Zhang61eaedb2017-11-02 13:28:15 -07001088 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001089 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001090 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1091 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001092 // Formerly, there was a check here that prevented annotation processors
1093 // from being used when sharding was enabled, as some annotation processors
1094 // do not function correctly in sharded environments. It was removed to
1095 // allow for the use of annotation processors that do function correctly
1096 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001097 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001098 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001099 if ctx.Failed() {
1100 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001101 }
1102 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001103 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001104 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001105 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001106 // If error-prone is enabled, add an additional rule to compile the java files into
1107 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001108 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001109 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1110 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001111 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001112 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001113 extraJarDeps = append(extraJarDeps, errorprone)
1114 }
1115
Nan Zhang61eaedb2017-11-02 13:28:15 -07001116 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001117 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001118 shardSize := int(*(j.properties.Javac_shard_size))
1119 var shardSrcs []android.Paths
1120 if len(uniqueSrcFiles) > 0 {
1121 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1122 for idx, shardSrc := range shardSrcs {
1123 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1124 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1125 jars = append(jars, classes)
1126 }
1127 }
1128 if len(srcJars) > 0 {
1129 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1130 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1131 jars = append(jars, classes)
1132 }
1133 } else {
1134 classes := android.PathForModuleOut(ctx, "javac", jarName)
1135 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1136 jars = append(jars, classes)
1137 }
Colin Crossd6891432017-09-27 17:39:56 -07001138 if ctx.Failed() {
1139 return
1140 }
Colin Cross2fe66872015-03-30 17:20:39 -07001141 }
1142
Colin Cross0c4ce212019-05-03 15:28:19 -07001143 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1144
1145 var includeSrcJar android.WritablePath
1146 if Bool(j.properties.Include_srcs) {
1147 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1148 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1149 }
1150
Colin Crosscedd4762018-09-13 11:26:19 -07001151 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1152 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001153 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001154 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001155
1156 var resArgs []string
1157 var resDeps android.Paths
1158
1159 resArgs = append(resArgs, dirArgs...)
1160 resDeps = append(resDeps, dirDeps...)
1161
1162 resArgs = append(resArgs, fileArgs...)
1163 resDeps = append(resDeps, fileDeps...)
1164
Colin Cross988708c2019-05-06 14:04:11 -07001165 resArgs = append(resArgs, extraArgs...)
1166 resDeps = append(resDeps, extraDeps...)
1167
Colin Cross40a36712017-09-27 17:41:35 -07001168 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001169 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001170 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001171 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001172 if ctx.Failed() {
1173 return
1174 }
1175 }
1176
Colin Cross0c4ce212019-05-03 15:28:19 -07001177 var resourceJars android.Paths
1178 if j.resourceJar != nil {
1179 resourceJars = append(resourceJars, j.resourceJar)
1180 }
1181 if Bool(j.properties.Include_srcs) {
1182 resourceJars = append(resourceJars, includeSrcJar)
1183 }
1184 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001185
Colin Cross0c4ce212019-05-03 15:28:19 -07001186 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001187 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001188 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001189 false, nil, nil)
1190 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001191 } else if len(resourceJars) == 1 {
1192 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001193 }
1194
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001195 if len(deps.staticJars) > 0 {
1196 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001197 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001198
Colin Cross094054a2018-10-17 15:10:48 -07001199 manifest := j.overrideManifest
1200 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001201 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001202 }
Colin Cross635acc92017-09-12 22:50:46 -07001203
Colin Cross8a497952019-03-05 22:25:09 -08001204 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001205 if len(services) > 0 {
1206 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1207 var zipargs []string
1208 for _, file := range services {
1209 serviceFile := file.String()
1210 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1211 }
1212 ctx.Build(pctx, android.BuildParams{
1213 Rule: zip,
1214 Output: servicesJar,
1215 Implicits: services,
1216 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001217 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001218 },
1219 })
1220 jars = append(jars, servicesJar)
1221 }
1222
Colin Cross0a6e0072017-08-30 14:24:55 -07001223 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001224 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001225 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001226
1227 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001228 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1229 // Optimization: skip the combine step if there is nothing to do
1230 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1231 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1232 // any if len(jars) == 1.
1233 outputFile = moduleOutPath
1234 } else {
1235 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1236 ctx.Build(pctx, android.BuildParams{
1237 Rule: android.Cp,
1238 Input: jars[0],
1239 Output: combinedJar,
1240 })
1241 outputFile = combinedJar
1242 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001243 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001244 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001245 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001246 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001247 outputFile = combinedJar
1248 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001249
Colin Cross331a1212018-08-15 20:40:52 -07001250 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001251 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001252 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001253 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001254 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001255 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001256
1257 // jarjar resource jar if necessary
1258 if j.resourceJar != nil {
1259 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001260 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001261 j.resourceJar = resourceJarJarFile
1262 }
1263
Colin Cross0a6e0072017-08-30 14:24:55 -07001264 if ctx.Failed() {
1265 return
1266 }
1267 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001268
1269 // Check package restrictions if necessary.
1270 if len(j.properties.Permitted_packages) > 0 {
1271 // Check packages and copy to package-checked file.
1272 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1273 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1274 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1275
1276 if ctx.Failed() {
1277 return
1278 }
1279 }
1280
Nan Zhanged19fc32017-10-19 13:06:22 -07001281 j.implementationJarFile = outputFile
1282 if j.headerJarFile == nil {
1283 j.headerJarFile = j.implementationJarFile
1284 }
Colin Cross2fe66872015-03-30 17:20:39 -07001285
Colin Cross6510f912017-11-29 00:27:14 -08001286 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001287 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1288 j.properties.Instrument = true
1289 }
1290 }
1291
Colin Cross3144dfc2018-01-03 15:06:47 -08001292 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001293 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1294 }
1295
Colin Cross331a1212018-08-15 20:40:52 -07001296 // merge implementation jar with resources if necessary
1297 implementationAndResourcesJar := outputFile
1298 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001299 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001300 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001301 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001302 false, nil, nil)
1303 implementationAndResourcesJar = combinedJar
1304 }
1305
1306 j.implementationAndResourcesJar = implementationAndResourcesJar
1307
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001308 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001309 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001310 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001311 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001312 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001313 if ctx.Failed() {
1314 return
1315 }
Colin Cross331a1212018-08-15 20:40:52 -07001316
Colin Cross9c74a1e2019-05-28 14:06:17 -07001317 if !ctx.Config().UnbundledBuild() {
1318 // Hidden API CSV generation and dex encoding
1319 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1320 j.deviceProperties.UncompressDex)
1321 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001322
Colin Cross331a1212018-08-15 20:40:52 -07001323 // merge dex jar with resources if necessary
1324 if j.resourceJar != nil {
1325 jars := android.Paths{dexOutputFile, j.resourceJar}
1326 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1327 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1328 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001329 if j.deviceProperties.UncompressDex {
1330 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1331 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1332 dexOutputFile = combinedAlignedJar
1333 } else {
1334 dexOutputFile = combinedJar
1335 }
Colin Cross331a1212018-08-15 20:40:52 -07001336 }
1337
1338 j.dexJarFile = dexOutputFile
1339
Colin Cross8faf8fc2019-01-16 15:15:52 -08001340 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001341 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1342
1343 j.maybeStrippedDexJarFile = dexOutputFile
1344
Colin Cross3063b782018-08-15 11:19:12 -07001345 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001346
1347 if ctx.Failed() {
1348 return
1349 }
Colin Cross331a1212018-08-15 20:40:52 -07001350 } else {
1351 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001352 }
Colin Cross331a1212018-08-15 20:40:52 -07001353
Colin Crossb7a63242015-04-16 14:09:14 -07001354 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001355
1356 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1357 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001358}
1359
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001360// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1361// since some of these flags may be used internally.
1362func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1363 for _, flag := range flags {
1364 flag = strings.TrimSpace(flag)
1365
1366 if !strings.HasPrefix(flag, "-") {
1367 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1368 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1369 ctx.PropertyErrorf("kotlincflags",
1370 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1371 } else if inList(flag, config.KotlincIllegalFlags) {
1372 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1373 } else if flag == "-include-runtime" {
1374 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1375 } else {
1376 args := strings.Split(flag, " ")
1377 if args[0] == "-kotlin-home" {
1378 ctx.PropertyErrorf("kotlincflags",
1379 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1380 }
1381 }
1382 }
1383}
1384
Colin Cross8eadbf02017-10-24 17:46:00 -07001385func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001386 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001387
1388 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001389 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001390 // Compile java sources into turbine.jar.
1391 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1392 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1393 if ctx.Failed() {
1394 return nil
1395 }
1396 jars = append(jars, turbineJar)
1397 }
1398
Colin Cross55f63ea2018-08-27 12:37:09 -07001399 jars = append(jars, extraJars...)
1400
Nan Zhanged19fc32017-10-19 13:06:22 -07001401 // Combine any static header libraries into classes-header.jar. If there is only
1402 // one input jar this step will be skipped.
1403 var headerJar android.Path
1404 jars = append(jars, deps.staticHeaderJars...)
1405
Colin Cross5c6ecc12017-10-23 18:12:27 -07001406 // we cannot skip the combine step for now if there is only one jar
1407 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1408 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001409 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001410 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001411 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001412
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001413 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001414 // Transform classes.jar into classes-jarjar.jar
1415 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001416 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001417 headerJar = jarjarFile
1418 if ctx.Failed() {
1419 return nil
1420 }
1421 }
1422
1423 return headerJar
1424}
1425
Colin Crosscb933592017-11-22 13:49:43 -08001426func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001427 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001428
Colin Cross7a3139e2017-12-19 13:57:50 -08001429 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001430
Colin Cross84c38822018-01-03 15:59:46 -08001431 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001432 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1433
1434 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1435
1436 j.jacocoReportClassesFile = jacocoReportClassesFile
1437
1438 return instrumentedJar
1439}
1440
albaltai36ff7dc2018-12-25 14:35:23 +08001441var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001442
Nan Zhanged19fc32017-10-19 13:06:22 -07001443func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001444 if j.headerJarFile == nil {
1445 return nil
1446 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001447 return android.Paths{j.headerJarFile}
1448}
1449
1450func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001451 if j.implementationJarFile == nil {
1452 return nil
1453 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001454 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001455}
1456
Colin Crossf24a22a2019-01-31 14:12:44 -08001457func (j *Module) DexJar() android.Path {
1458 return j.dexJarFile
1459}
1460
Colin Cross331a1212018-08-15 20:40:52 -07001461func (j *Module) ResourceJars() android.Paths {
1462 if j.resourceJar == nil {
1463 return nil
1464 }
1465 return android.Paths{j.resourceJar}
1466}
1467
1468func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001469 if j.implementationAndResourcesJar == nil {
1470 return nil
1471 }
Colin Cross331a1212018-08-15 20:40:52 -07001472 return android.Paths{j.implementationAndResourcesJar}
1473}
1474
Colin Cross46c9b8b2017-06-22 16:51:17 -07001475func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001476 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001477 return j.exportAidlIncludeDirs
1478}
1479
Jiyong Park1be96912018-05-28 18:02:19 +09001480func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001481 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001482 return j.exportedSdkLibs
1483}
1484
Colin Cross0c4ce212019-05-03 15:28:19 -07001485func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1486 return j.srcJarArgs, j.srcJarDeps
1487}
1488
Colin Cross46c9b8b2017-06-22 16:51:17 -07001489var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001490
Colin Cross46c9b8b2017-06-22 16:51:17 -07001491func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001492 return j.logtagsSrcs
1493}
1494
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001495// Collect information for opening IDE project files in java/jdeps.go.
1496func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1497 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1498 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001499 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001500 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001501 if j.expandJarjarRules != nil {
1502 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001503 }
1504}
1505
1506func (j *Module) CompilerDeps() []string {
1507 jdeps := []string{}
1508 jdeps = append(jdeps, j.properties.Libs...)
1509 jdeps = append(jdeps, j.properties.Static_libs...)
1510 return jdeps
1511}
1512
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001513func (j *Module) hasCode(ctx android.ModuleContext) bool {
1514 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1515 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1516}
1517
Colin Cross2fe66872015-03-30 17:20:39 -07001518//
1519// Java libraries (.jar file)
1520//
1521
Colin Crossf506d872017-07-19 15:53:04 -07001522type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001523 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001524}
1525
Colin Cross42be7612019-02-21 18:12:14 -08001526func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001527 // Store uncompressed (and do not strip) dex files from boot class path jars.
1528 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1529 return true
1530 }
1531
1532 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001533 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001534 return true
1535 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001536 if ctx.Config().UncompressPrivAppDex() &&
1537 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1538 return true
1539 }
1540
Colin Cross2fc72f62018-12-21 12:59:54 -08001541 return false
1542}
1543
Colin Crossf506d872017-07-19 15:53:04 -07001544func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001545 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1546 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001547 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001548 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001549 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001550 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001551
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001552 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001553 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1554 ctx.ModuleName()+".jar", j.outputFile)
1555 }
Colin Crossb7a63242015-04-16 14:09:14 -07001556}
1557
Colin Crossf506d872017-07-19 15:53:04 -07001558func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001559 j.deps(ctx)
1560}
1561
Colin Cross1b16b0e2019-02-12 14:41:32 -08001562// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1563//
1564// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1565// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1566// as a `static_libs` dependency of another module.
1567//
1568// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1569// a device.
1570//
1571// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1572// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001573func LibraryFactory() android.Module {
1574 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001575
Colin Cross9ae1b922018-06-26 17:59:05 -07001576 module.AddProperties(
1577 &module.Module.properties,
1578 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001579 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001580 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001581
Colin Cross9ae1b922018-06-26 17:59:05 -07001582 InitJavaModule(module, android.HostAndDeviceSupported)
1583 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001584}
1585
Colin Cross1b16b0e2019-02-12 14:41:32 -08001586// java_library_static is an obsolete alias for java_library.
1587func LibraryStaticFactory() android.Module {
1588 return LibraryFactory()
1589}
1590
1591// java_library_host builds and links sources into a `.jar` file for the host.
1592//
1593// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1594// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001595func LibraryHostFactory() android.Module {
1596 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001597
Colin Cross6af17aa2017-09-20 12:59:05 -07001598 module.AddProperties(
1599 &module.Module.properties,
1600 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001601
Colin Cross9ae1b922018-06-26 17:59:05 -07001602 module.Module.properties.Installable = proptools.BoolPtr(true)
1603
Colin Cross89536d42017-07-07 14:35:50 -07001604 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001605 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001606}
1607
1608//
Colin Crossb628ea52018-08-14 16:42:33 -07001609// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001610//
1611
1612type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001613 // list of compatibility suites (for example "cts", "vts") that the module should be
1614 // installed into.
1615 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001616
1617 // the name of the test configuration (for example "AndroidTest.xml") that should be
1618 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001619 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001620
Jack He33338892018-09-19 02:21:28 -07001621 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1622 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001623 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001624
Colin Crossd96ca352018-08-10 16:06:24 -07001625 // list of files or filegroup modules that provide data that should be installed alongside
1626 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001627 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001628}
1629
Paul Duffin42df1442019-03-20 12:45:53 +00001630type testHelperLibraryProperties struct {
1631 // list of compatibility suites (for example "cts", "vts") that the module should be
1632 // installed into.
1633 Test_suites []string `android:"arch_variant"`
1634}
1635
Colin Cross05638fc2018-04-09 18:40:24 -07001636type Test struct {
1637 Library
1638
1639 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001640
1641 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001642 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001643}
1644
Paul Duffin42df1442019-03-20 12:45:53 +00001645type TestHelperLibrary struct {
1646 Library
1647
1648 testHelperLibraryProperties testHelperLibraryProperties
1649}
1650
Colin Cross303e21f2018-08-07 16:49:25 -07001651func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001652 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 -08001653 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001654
1655 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001656}
1657
Paul Duffin42df1442019-03-20 12:45:53 +00001658func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1659 j.Library.GenerateAndroidBuildActions(ctx)
1660}
1661
Colin Cross1b16b0e2019-02-12 14:41:32 -08001662// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1663// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1664//
1665// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1666// compiled against the device bootclasspath.
1667//
1668// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1669// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001670func TestFactory() android.Module {
1671 module := &Test{}
1672
1673 module.AddProperties(
1674 &module.Module.properties,
1675 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001676 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001677 &module.Module.protoProperties,
1678 &module.testProperties)
1679
Colin Cross9ae1b922018-06-26 17:59:05 -07001680 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001681 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001682
Colin Cross05638fc2018-04-09 18:40:24 -07001683 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001684 return module
1685}
1686
Paul Duffin42df1442019-03-20 12:45:53 +00001687// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1688func TestHelperLibraryFactory() android.Module {
1689 module := &TestHelperLibrary{}
1690
1691 module.AddProperties(
1692 &module.Module.properties,
1693 &module.Module.deviceProperties,
1694 &module.Module.dexpreoptProperties,
1695 &module.Module.protoProperties,
1696 &module.testHelperLibraryProperties)
1697
Colin Cross9a4abed2019-04-24 13:19:28 -07001698 module.Module.properties.Installable = proptools.BoolPtr(true)
1699 module.Module.dexpreopter.isTest = true
1700
Paul Duffin42df1442019-03-20 12:45:53 +00001701 InitJavaModule(module, android.HostAndDeviceSupported)
1702 return module
1703}
1704
Colin Cross1b16b0e2019-02-12 14:41:32 -08001705// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1706// allow running the test with `atest` or a `TEST_MAPPING` file.
1707//
1708// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1709// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001710func TestHostFactory() android.Module {
1711 module := &Test{}
1712
1713 module.AddProperties(
1714 &module.Module.properties,
1715 &module.Module.protoProperties,
1716 &module.testProperties)
1717
Colin Cross9ae1b922018-06-26 17:59:05 -07001718 module.Module.properties.Installable = proptools.BoolPtr(true)
1719
Colin Cross05638fc2018-04-09 18:40:24 -07001720 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001721 return module
1722}
1723
1724//
Colin Cross2fe66872015-03-30 17:20:39 -07001725// Java Binaries (.jar file plus wrapper script)
1726//
1727
Colin Crossf506d872017-07-19 15:53:04 -07001728type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001729 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001730 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001731
1732 // Name of the class containing main to be inserted into the manifest as Main-Class.
1733 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001734}
1735
Colin Crossf506d872017-07-19 15:53:04 -07001736type Binary struct {
1737 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001738
Colin Crossf506d872017-07-19 15:53:04 -07001739 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001740
Colin Cross6b4a32d2017-12-05 13:42:45 -08001741 isWrapperVariant bool
1742
Colin Crossc3315992017-12-08 19:12:36 -08001743 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001744 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001745}
1746
Alex Light24237172017-10-26 09:46:21 -07001747func (j *Binary) HostToolPath() android.OptionalPath {
1748 return android.OptionalPathForPath(j.binaryFile)
1749}
1750
Colin Crossf506d872017-07-19 15:53:04 -07001751func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001752 if ctx.Arch().ArchType == android.Common {
1753 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001754 if j.binaryProperties.Main_class != nil {
1755 if j.properties.Manifest != nil {
1756 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1757 }
1758 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1759 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1760 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1761 }
1762
Colin Cross6b4a32d2017-12-05 13:42:45 -08001763 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001764 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001765 // Handle the binary wrapper
1766 j.isWrapperVariant = true
1767
Colin Cross366938f2017-12-11 16:29:02 -08001768 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001769 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001770 } else {
1771 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1772 }
1773
1774 // Depend on the installed jar so that the wrapper doesn't get executed by
1775 // another build rule before the jar has been installed.
1776 jarFile := ctx.PrimaryModule().(*Binary).installFile
1777
1778 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1779 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001780 }
Colin Cross2fe66872015-03-30 17:20:39 -07001781}
1782
Colin Crossf506d872017-07-19 15:53:04 -07001783func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001784 if ctx.Arch().ArchType == android.Common {
1785 j.deps(ctx)
1786 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001787}
1788
Colin Cross1b16b0e2019-02-12 14:41:32 -08001789// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1790// as well.
1791//
1792// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1793// compiled against the device bootclasspath.
1794//
1795// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1796// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001797func BinaryFactory() android.Module {
1798 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001799
Colin Cross36242852017-06-23 15:06:31 -07001800 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001801 &module.Module.properties,
1802 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001803 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001804 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001805 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001806
Colin Cross9ae1b922018-06-26 17:59:05 -07001807 module.Module.properties.Installable = proptools.BoolPtr(true)
1808
Colin Cross6b4a32d2017-12-05 13:42:45 -08001809 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1810 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001811 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001812}
1813
Colin Cross1b16b0e2019-02-12 14:41:32 -08001814// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1815//
1816// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1817// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001818func BinaryHostFactory() android.Module {
1819 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001820
Colin Cross36242852017-06-23 15:06:31 -07001821 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001822 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001823 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001824 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001825
Colin Cross9ae1b922018-06-26 17:59:05 -07001826 module.Module.properties.Installable = proptools.BoolPtr(true)
1827
Colin Cross6b4a32d2017-12-05 13:42:45 -08001828 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1829 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001830 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001831}
1832
1833//
1834// Java prebuilts
1835//
1836
Colin Cross74d73e22017-08-02 11:05:49 -07001837type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001838 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001839
Nan Zhangea568a42017-11-08 21:20:04 -08001840 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001841
1842 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001843
1844 // List of shared java libs that this module has dependencies to
1845 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001846
1847 // List of files to remove from the jar file(s)
1848 Exclude_files []string
1849
1850 // List of directories to remove from the jar file(s)
1851 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001852
1853 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001854 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001855}
1856
1857type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001858 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001859 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001860 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001861
Colin Cross74d73e22017-08-02 11:05:49 -07001862 properties ImportProperties
1863
Colin Cross0a6e0072017-08-30 14:24:55 -07001864 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001865 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001866}
1867
Colin Cross83bb3162018-06-25 15:48:06 -07001868func (j *Import) sdkVersion() string {
1869 return String(j.properties.Sdk_version)
1870}
1871
1872func (j *Import) minSdkVersion() string {
1873 return j.sdkVersion()
1874}
1875
Colin Cross74d73e22017-08-02 11:05:49 -07001876func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001877 return &j.prebuilt
1878}
1879
Colin Cross74d73e22017-08-02 11:05:49 -07001880func (j *Import) PrebuiltSrcs() []string {
1881 return j.properties.Jars
1882}
1883
1884func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001885 return j.prebuilt.Name(j.ModuleBase.Name())
1886}
1887
Colin Cross74d73e22017-08-02 11:05:49 -07001888func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001889 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001890}
1891
Colin Cross74d73e22017-08-02 11:05:49 -07001892func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001893 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001894
Nan Zhang4c819fb2018-08-27 18:31:46 -07001895 jarName := ctx.ModuleName() + ".jar"
1896 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001897 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1898 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001899 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001900 inputFile := outputFile
1901 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1902 TransformJetifier(ctx, outputFile, inputFile)
1903 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001904 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001905
1906 ctx.VisitDirectDeps(func(module android.Module) {
1907 otherName := ctx.OtherModuleName(module)
1908 tag := ctx.OtherModuleDependencyTag(module)
1909
1910 switch dep := module.(type) {
1911 case Dependency:
1912 switch tag {
1913 case libTag, staticLibTag:
1914 // sdk lib names from dependencies are re-exported
1915 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1916 }
1917 case SdkLibraryDependency:
1918 switch tag {
1919 case libTag:
1920 // names of sdk libs that are directly depended are exported
1921 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1922 }
1923 }
1924 })
1925
1926 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001927 if Bool(j.properties.Installable) {
1928 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1929 ctx.ModuleName()+".jar", outputFile)
1930 }
Colin Cross2fe66872015-03-30 17:20:39 -07001931}
1932
Colin Cross74d73e22017-08-02 11:05:49 -07001933var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001934
Nan Zhanged19fc32017-10-19 13:06:22 -07001935func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001936 if j.combinedClasspathFile == nil {
1937 return nil
1938 }
Colin Cross37f6d792018-07-12 12:28:41 -07001939 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001940}
1941
1942func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001943 if j.combinedClasspathFile == nil {
1944 return nil
1945 }
Colin Cross37f6d792018-07-12 12:28:41 -07001946 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001947}
1948
Colin Cross331a1212018-08-15 20:40:52 -07001949func (j *Import) ResourceJars() android.Paths {
1950 return nil
1951}
1952
1953func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001954 if j.combinedClasspathFile == nil {
1955 return nil
1956 }
Colin Cross331a1212018-08-15 20:40:52 -07001957 return android.Paths{j.combinedClasspathFile}
1958}
1959
Colin Crossf24a22a2019-01-31 14:12:44 -08001960func (j *Import) DexJar() android.Path {
1961 return nil
1962}
1963
Colin Cross74d73e22017-08-02 11:05:49 -07001964func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001965 return nil
1966}
1967
Jiyong Park1be96912018-05-28 18:02:19 +09001968func (j *Import) ExportedSdkLibs() []string {
1969 return j.exportedSdkLibs
1970}
1971
Colin Cross0c4ce212019-05-03 15:28:19 -07001972func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1973 return nil, nil
1974}
1975
albaltai36ff7dc2018-12-25 14:35:23 +08001976// Add compile time check for interface implementation
1977var _ android.IDEInfo = (*Import)(nil)
1978var _ android.IDECustomizedModuleName = (*Import)(nil)
1979
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001980// Collect information for opening IDE project files in java/jdeps.go.
1981const (
1982 removedPrefix = "prebuilt_"
1983)
1984
1985func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1986 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1987}
1988
1989func (j *Import) IDECustomizedModuleName() string {
1990 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1991 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1992 // solution to get the Import name.
1993 name := j.Name()
1994 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001995 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001996 }
1997 return name
1998}
1999
Colin Cross74d73e22017-08-02 11:05:49 -07002000var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002001
Colin Cross1b16b0e2019-02-12 14:41:32 -08002002// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2003//
2004// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2005// compiled against an Android classpath.
2006//
2007// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2008// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002009func ImportFactory() android.Module {
2010 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002011
Colin Cross74d73e22017-08-02 11:05:49 -07002012 module.AddProperties(&module.properties)
2013
2014 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002015 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002016 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002017}
2018
Colin Cross1b16b0e2019-02-12 14:41:32 -08002019// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2020// module.
2021//
2022// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2023// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002024func ImportFactoryHost() android.Module {
2025 module := &Import{}
2026
2027 module.AddProperties(&module.properties)
2028
2029 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002030 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002031 return module
2032}
2033
Colin Cross42be7612019-02-21 18:12:14 -08002034// dex_import module
2035
2036type DexImportProperties struct {
2037 Jars []string
2038}
2039
2040type DexImport struct {
2041 android.ModuleBase
2042 android.DefaultableModuleBase
2043 prebuilt android.Prebuilt
2044
2045 properties DexImportProperties
2046
2047 dexJarFile android.Path
2048 maybeStrippedDexJarFile android.Path
2049
2050 dexpreopter
2051}
2052
2053func (j *DexImport) Prebuilt() *android.Prebuilt {
2054 return &j.prebuilt
2055}
2056
2057func (j *DexImport) PrebuiltSrcs() []string {
2058 return j.properties.Jars
2059}
2060
2061func (j *DexImport) Name() string {
2062 return j.prebuilt.Name(j.ModuleBase.Name())
2063}
2064
2065func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2066 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2067}
2068
2069func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2070 if len(j.properties.Jars) != 1 {
2071 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2072 }
2073
2074 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2075 j.dexpreopter.isInstallable = true
2076 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2077
2078 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2079 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2080
2081 if j.dexpreopter.uncompressedDex {
2082 rule := android.NewRuleBuilder()
2083
2084 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2085 rule.Temporary(temporary)
2086
2087 // use zip2zip to uncompress classes*.dex files
2088 rule.Command().
2089 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2090 FlagWithInput("-i ", inputJar).
2091 FlagWithOutput("-o ", temporary).
2092 FlagWithArg("-0 ", "'classes*.dex'")
2093
2094 // use zipalign to align uncompressed classes*.dex files
2095 rule.Command().
2096 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2097 Flag("-f").
2098 Text("4").
2099 Input(temporary).
2100 Output(dexOutputFile)
2101
2102 rule.DeleteTemporaryFiles()
2103
2104 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2105 } else {
2106 ctx.Build(pctx, android.BuildParams{
2107 Rule: android.Cp,
2108 Input: inputJar,
2109 Output: dexOutputFile,
2110 })
2111 }
2112
2113 j.dexJarFile = dexOutputFile
2114
2115 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2116
2117 j.maybeStrippedDexJarFile = dexOutputFile
2118
2119 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2120 ctx.ModuleName()+".jar", dexOutputFile)
2121}
2122
2123func (j *DexImport) DexJar() android.Path {
2124 return j.dexJarFile
2125}
2126
2127// dex_import imports a `.jar` file containing classes.dex files.
2128//
2129// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2130// to the device.
2131func DexImportFactory() android.Module {
2132 module := &DexImport{}
2133
2134 module.AddProperties(&module.properties)
2135
2136 android.InitPrebuiltModule(module, &module.properties.Jars)
2137 InitJavaModule(module, android.DeviceSupported)
2138 return module
2139}
2140
Colin Cross89536d42017-07-07 14:35:50 -07002141//
2142// Defaults
2143//
2144type Defaults struct {
2145 android.ModuleBase
2146 android.DefaultsModuleBase
2147}
2148
Colin Cross1b16b0e2019-02-12 14:41:32 -08002149// java_defaults provides a set of properties that can be inherited by other java or android modules.
2150//
2151// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2152// property in the defaults module that exists in the depending module will be prepended to the depending module's
2153// value for that property.
2154//
2155// Example:
2156//
2157// java_defaults {
2158// name: "example_defaults",
2159// srcs: ["common/**/*.java"],
2160// javacflags: ["-Xlint:all"],
2161// aaptflags: ["--auto-add-overlay"],
2162// }
2163//
2164// java_library {
2165// name: "example",
2166// defaults: ["example_defaults"],
2167// srcs: ["example/**/*.java"],
2168// }
2169//
2170// is functionally identical to:
2171//
2172// java_library {
2173// name: "example",
2174// srcs: [
2175// "common/**/*.java",
2176// "example/**/*.java",
2177// ],
2178// javacflags: ["-Xlint:all"],
2179// }
Colin Cross89536d42017-07-07 14:35:50 -07002180func defaultsFactory() android.Module {
2181 return DefaultsFactory()
2182}
2183
2184func DefaultsFactory(props ...interface{}) android.Module {
2185 module := &Defaults{}
2186
2187 module.AddProperties(props...)
2188 module.AddProperties(
2189 &CompilerProperties{},
2190 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002191 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002192 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002193 &aaptProperties{},
2194 &androidLibraryProperties{},
2195 &appProperties{},
2196 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002197 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002198 &ImportProperties{},
2199 &AARImportProperties{},
2200 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002201 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002202 )
2203
2204 android.InitDefaultsModule(module)
2205
2206 return module
2207}
Nan Zhangea568a42017-11-08 21:20:04 -08002208
2209var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002210var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002211var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002212var inList = android.InList