blob: bfbd98c8fcd08be54e4c7b20a0b819cabf26353c [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Colin Cross2fe66872015-03-30 17:20:39 -070055// TODO:
56// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070057// Renderscript
58// Post-jar passes:
59// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070060// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070061// DroidDoc
62// Findbugs
63
Colin Cross89536d42017-07-07 14:35:50 -070064type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070065 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
66 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070068
69 // list of source files that should not be used to build the Java module.
70 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080071 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070072
73 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070074 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070075
Colin Cross86a63ff2017-09-27 17:33:10 -070076 // list of directories that should be excluded from java_resource_dirs
77 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070078
Colin Cross0f37af02017-09-27 17:42:05 -070079 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080080 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070081
Colin Crosscedd4762018-09-13 11:26:19 -070082 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080083 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070084
Paul Duffin2fbbfb82019-02-13 12:49:33 +000085 // don't build against the default libraries (bootclasspath, ext, and framework for device
86 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070087 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070088
Paul Duffin2fbbfb82019-02-13 12:49:33 +000089 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070090 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020095 // list of module-specific flags that will be used for kotlinc compiles
96 Kotlincflags []string `android:"arch_variant"`
97
Colin Cross7d5136f2015-05-11 13:39:40 -070098 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700102 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
104 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700106
Colin Cross540eff82017-06-22 17:01:52 -0700107 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700109
110 // If not blank, set the java version passed to javac as -source and -target
111 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700112
Colin Cross9ae1b922018-06-26 17:59:05 -0700113 // If set to true, allow this module to be dexed and installed on devices. Has no
114 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700115 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700116
Colin Cross0f37af02017-09-27 17:42:05 -0700117 // If set to true, include sources used to compile the module in to the final jar
118 Include_srcs *bool
119
Vladimir Marko0975ee02019-04-02 10:29:55 +0100120 // If not empty, classes are restricted to the specified packages and their sub-packages.
121 // This restriction is checked after applying jarjar rules and including static libs.
122 Permitted_packages []string
123
Colin Crossbe9cdb82019-01-21 21:37:16 -0800124 // List of modules to use as annotation processors
125 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700126
Nan Zhang61eaedb2017-11-02 13:28:15 -0700127 // The number of Java source entries each Javac instance can process
128 Javac_shard_size *int64
129
Nan Zhang5f8cb422018-02-06 10:34:32 -0800130 // Add host jdk tools.jar to bootclasspath
131 Use_tools_jar *bool
132
Colin Cross1369cdb2017-09-29 17:58:17 -0700133 Openjdk9 struct {
134 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800135 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700136
137 // List of javac flags that should only be used when passing -source 1.9
138 Javacflags []string
139 }
Colin Crosscb933592017-11-22 13:49:43 -0800140
Colin Cross81440082018-08-15 20:21:55 -0700141 // When compiling language level 9+ .java code in packages that are part of
142 // a system module, patch_module names the module that your sources and
143 // dependencies should be patched into. The Android runtime currently
144 // doesn't implement the JEP 261 module system so this option is only
145 // supported at compile time. It should only be needed to compile tests in
146 // packages that exist in libcore and which are inconvenient to move
147 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100148 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700149
Colin Crosscb933592017-11-22 13:49:43 -0800150 Jacoco struct {
151 // List of classes to include for instrumentation with jacoco to collect coverage
152 // information at runtime when building with coverage enabled. If unset defaults to all
153 // classes.
154 // Supports '*' as the last character of an entry in the list as a wildcard match.
155 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
156 // it matches classes in the package that have the class name as a prefix.
157 Include_filter []string
158
159 // List of classes to exclude from instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. Overrides classes selected
161 // by the include_filter property.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Exclude_filter []string
166 }
167
Andreas Gampef3e5b552018-01-22 21:27:21 -0800168 Errorprone struct {
169 // List of javac flags that should only be used when running errorprone.
170 Javacflags []string
171 }
172
Colin Cross0f2ee152017-12-14 15:22:43 -0800173 Proto struct {
174 // List of extra options that will be passed to the proto generator.
175 Output_params []string
176 }
177
Colin Crosscb933592017-11-22 13:49:43 -0800178 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800179
180 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800181 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700182}
183
Colin Cross89536d42017-07-07 14:35:50 -0700184type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700185 // list of module-specific flags that will be used for dex compiles
186 Dxflags []string `android:"arch_variant"`
187
Colin Cross83bb3162018-06-25 15:48:06 -0700188 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
189 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800190 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700191
Colin Cross83bb3162018-06-25 15:48:06 -0700192 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
193 // Defaults to sdk_version if not set.
194 Min_sdk_version *string
195
Dan Willemsen419290a2018-10-31 15:28:47 -0700196 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
197 // Defaults to sdk_version if not set.
198 Target_sdk_version *string
199
Colin Cross6af2e492018-05-22 11:12:33 -0700200 // if true, compile against the platform APIs instead of an SDK.
201 Platform_apis *bool
202
Colin Crossebe1a512017-11-14 13:12:14 -0800203 Aidl struct {
204 // Top level directories to pass to aidl tool
205 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700206
Colin Crossebe1a512017-11-14 13:12:14 -0800207 // Directories rooted at the Android.bp file to pass to aidl tool
208 Local_include_dirs []string
209
210 // directories that should be added as include directories for any aidl sources of modules
211 // that depend on this module, as well as to aidl for this module.
212 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100213
214 // whether to generate traces (for systrace) for this interface
215 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100216
217 // whether to generate Binder#GetTransaction name method.
218 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800219 }
Colin Cross92430102017-10-09 14:59:32 -0700220
221 // If true, export a copy of the module as a -hostdex module for host testing.
222 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700223
Colin Cross7f87f4f2019-04-24 13:41:45 -0700224 Target struct {
225 Hostdex struct {
226 // Additional required dependencies to add to -hostdex modules.
227 Required []string
228 }
229 }
230
David Brazdil17ef5632018-06-27 10:27:45 +0100231 // If set to true, compile dex regardless of installable. Defaults to false.
232 Compile_dex *bool
233
Colin Cross66dbc0b2017-12-28 12:23:20 -0800234 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700235 // If false, disable all optimization. Defaults to true for android_app and android_test
236 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800237 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700238 // True if the module containing this has it set by default.
239 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800240
241 // If true, optimize for size by removing unused code. Defaults to true for apps,
242 // false for libraries and tests.
243 Shrink *bool
244
245 // If true, optimize bytecode. Defaults to false.
246 Optimize *bool
247
248 // If true, obfuscate bytecode. Defaults to false.
249 Obfuscate *bool
250
251 // If true, do not use the flag files generated by aapt that automatically keep
252 // classes referenced by the app manifest. Defaults to false.
253 No_aapt_flags *bool
254
255 // Flags to pass to proguard.
256 Proguard_flags []string
257
258 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800259 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800260 }
261
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 // When targeting 1.9, override the modules to use with --system
263 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700264
265 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800266 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700267}
268
Sasha Smundak2057f822019-04-16 17:16:58 -0700269func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
270 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
271}
272
Colin Cross46c9b8b2017-06-22 16:51:17 -0700273// Module contains the properties and members used by all java module types
274type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700275 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700276 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700277
Colin Cross89536d42017-07-07 14:35:50 -0700278 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700279 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700280 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700281
Colin Cross331a1212018-08-15 20:40:52 -0700282 // jar file containing header classes including static library dependencies, suitable for
283 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 headerJarFile android.Path
285
Colin Cross331a1212018-08-15 20:40:52 -0700286 // jar file containing implementation classes including static library dependencies but no
287 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700289
Colin Cross331a1212018-08-15 20:40:52 -0700290 // jar file containing only resources including from static library dependencies
291 resourceJar android.Path
292
Colin Cross0c4ce212019-05-03 15:28:19 -0700293 // args and dependencies to package source files into a srcjar
294 srcJarArgs []string
295 srcJarDeps android.Paths
296
Colin Cross331a1212018-08-15 20:40:52 -0700297 // jar file containing implementation classes and resources including static library
298 // dependencies
299 implementationAndResourcesJar android.Path
300
301 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700302 dexJarFile android.Path
303
Colin Cross43f08db2018-11-12 10:13:39 -0800304 // output file that contains classes.dex if it should be in the output file
305 maybeStrippedDexJarFile android.Path
306
Colin Crosscb933592017-11-22 13:49:43 -0800307 // output file containing uninstrumented classes that will be instrumented by jacoco
308 jacocoReportClassesFile android.Path
309
Colin Cross66dbc0b2017-12-28 12:23:20 -0800310 // output file containing mapping of obfuscated names
311 proguardDictionary android.Path
312
Colin Cross331a1212018-08-15 20:40:52 -0700313 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700314 outputFile android.Path
315 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700316
Colin Cross635c3b02016-05-18 15:37:25 -0700317 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700318
Colin Cross635c3b02016-05-18 15:37:25 -0700319 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700320
Colin Cross2fe66872015-03-30 17:20:39 -0700321 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700322 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800323
324 // list of .java files and srcjars that was passed to javac
325 compiledJavaSrcs android.Paths
326 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800327
328 // list of extra progurad flag files
329 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900330
Colin Cross094054a2018-10-17 15:10:48 -0700331 // manifest file to use instead of properties.Manifest
332 overrideManifest android.OptionalPath
333
Jiyong Park1be96912018-05-28 18:02:19 +0900334 // list of SDK lib names that this java moudule is exporting
335 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700336
337 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
338 // filter out Exclude_srcs, will be used by android.IDEInfo struct
339 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800340
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800341 // expanded Jarjar_rules
342 expandJarjarRules android.Path
343
Vladimir Marko0975ee02019-04-02 10:29:55 +0100344 // list of additional targets for checkbuild
345 additionalCheckedModules android.Paths
346
Colin Cross988708c2019-05-06 14:04:11 -0700347 // Extra files generated by the module type to be added as java resources.
348 extraResources android.Paths
349
Colin Crossf24a22a2019-01-31 14:12:44 -0800350 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800351 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700352}
353
Colin Cross41955e82019-05-29 14:40:35 -0700354func (j *Module) OutputFiles(tag string) (android.Paths, error) {
355 switch tag {
356 case "":
357 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
358 default:
359 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
360 }
Colin Cross54250902017-12-05 09:28:08 -0800361}
362
Jiyong Park8fd61922018-11-08 02:50:25 +0900363func (j *Module) DexJarFile() android.Path {
364 return j.dexJarFile
365}
366
Colin Cross41955e82019-05-29 14:40:35 -0700367var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800368
Colin Crossf506d872017-07-19 15:53:04 -0700369type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700370 HeaderJars() android.Paths
371 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700372 ResourceJars() android.Paths
373 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800374 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700375 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900376 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700377 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700378}
379
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800381 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
382 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383}
384
Nan Zhangb2b33de2018-02-23 11:18:47 -0800385type SrcDependency interface {
386 CompiledSrcs() android.Paths
387 CompiledSrcJars() android.Paths
388}
389
390func (j *Module) CompiledSrcs() android.Paths {
391 return j.compiledJavaSrcs
392}
393
394func (j *Module) CompiledSrcJars() android.Paths {
395 return j.compiledSrcJars
396}
397
398var _ SrcDependency = (*Module)(nil)
399
Colin Cross89536d42017-07-07 14:35:50 -0700400func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
401 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
402 android.InitDefaultableModule(module)
403}
404
Colin Crossbe1da472017-07-07 15:59:46 -0700405type dependencyTag struct {
406 blueprint.BaseDependencyTag
407 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700408}
409
Colin Crossa4f08812018-10-02 22:03:40 -0700410type jniDependencyTag struct {
411 blueprint.BaseDependencyTag
412 target android.Target
413}
414
Colin Crossbe1da472017-07-07 15:59:46 -0700415var (
Colin Cross4b964c02018-10-15 16:18:06 -0700416 staticLibTag = dependencyTag{name: "staticlib"}
417 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800418 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700419 bootClasspathTag = dependencyTag{name: "bootclasspath"}
420 systemModulesTag = dependencyTag{name: "system modules"}
421 frameworkResTag = dependencyTag{name: "framework-res"}
422 frameworkApkTag = dependencyTag{name: "framework-apk"}
423 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800424 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700425 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
426 certificateTag = dependencyTag{name: "certificate"}
427 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700428 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700429)
Colin Cross2fe66872015-03-30 17:20:39 -0700430
Colin Crossfc3674a2017-09-18 17:41:52 -0700431type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700432 useModule, useFiles, useDefaultLibs, invalidVersion bool
433
Colin Cross86a60ae2018-05-29 14:44:55 -0700434 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700435 systemModules string
436
Colin Crossa97c5d32018-03-28 14:58:31 -0700437 frameworkResModule string
438
Colin Cross86a60ae2018-05-29 14:44:55 -0700439 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700440 aidl android.OptionalPath
Colin Cross1369cdb2017-09-29 17:58:17 -0700441}
442
Colin Crossa4f08812018-10-02 22:03:40 -0700443type jniLib struct {
444 name string
445 path android.Path
446 target android.Target
447}
448
Colin Cross3144dfc2018-01-03 15:06:47 -0800449func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
450 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
451}
452
453func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
454 return j.shouldInstrument(ctx) &&
455 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
456 ctx.Config().UnbundledBuild())
457}
458
Colin Cross83bb3162018-06-25 15:48:06 -0700459func (j *Module) sdkVersion() string {
460 return String(j.deviceProperties.Sdk_version)
461}
462
463func (j *Module) minSdkVersion() string {
464 if j.deviceProperties.Min_sdk_version != nil {
465 return *j.deviceProperties.Min_sdk_version
466 }
467 return j.sdkVersion()
468}
469
Dan Willemsen419290a2018-10-31 15:28:47 -0700470func (j *Module) targetSdkVersion() string {
471 if j.deviceProperties.Target_sdk_version != nil {
472 return *j.deviceProperties.Target_sdk_version
473 }
474 return j.sdkVersion()
475}
476
Colin Crossbe1da472017-07-07 15:59:46 -0700477func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700478 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700479 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700480 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700481 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700482 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100483 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700484 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700485 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700486 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700487 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100488 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700489 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700490 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700491 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
492 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800493 }
Colin Crossbe1da472017-07-07 15:59:46 -0700494 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700495 } else if j.deviceProperties.System_modules == nil {
496 ctx.PropertyErrorf("no_standard_libs",
497 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100498 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700499 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700500 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100501 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700502 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800503 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800504 if ctx.ModuleName() == "android_stubs_current" ||
505 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700506 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700507 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800508 }
Colin Cross2fe66872015-03-30 17:20:39 -0700509 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700510
Colin Cross42d48b72018-08-29 14:10:52 -0700511 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
512 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700513
Colin Crossbe9cdb82019-01-21 21:37:16 -0800514 ctx.AddFarVariationDependencies([]blueprint.Variation{
515 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
516 }, pluginTag, j.properties.Plugins...)
517
Colin Crossfe17f6f2019-03-28 19:30:56 -0700518 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700519 if j.hasSrcExt(".proto") {
520 protoDeps(ctx, &j.protoProperties)
521 }
Colin Cross93e85952017-08-15 13:34:18 -0700522
523 if j.hasSrcExt(".kt") {
524 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
525 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700526 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
527 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800528 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800529 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
530 }
Colin Cross93e85952017-08-15 13:34:18 -0700531 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800532
533 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700534 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800535 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700536}
537
538func hasSrcExt(srcs []string, ext string) bool {
539 for _, src := range srcs {
540 if filepath.Ext(src) == ext {
541 return true
542 }
543 }
544
545 return false
546}
547
Nan Zhang61eaedb2017-11-02 13:28:15 -0700548func shardPaths(paths android.Paths, shardSize int) []android.Paths {
549 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
550 for len(paths) > shardSize {
551 ret = append(ret, paths[0:shardSize])
552 paths = paths[shardSize:]
553 }
554 if len(paths) > 0 {
555 ret = append(ret, paths)
556 }
557 return ret
558}
559
Colin Cross6af17aa2017-09-20 12:59:05 -0700560func (j *Module) hasSrcExt(ext string) bool {
561 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700562}
563
Colin Cross46c9b8b2017-06-22 16:51:17 -0700564func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700565 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700566
Colin Crossebe1a512017-11-14 13:12:14 -0800567 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
568 aidlIncludes = append(aidlIncludes,
569 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
570 aidlIncludes = append(aidlIncludes,
571 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700572
Colin Cross3047fa22019-04-18 10:56:44 -0700573 var flags []string
574 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700575
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700576 if aidlPreprocess.Valid() {
577 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700578 deps = append(deps, aidlPreprocess.Path())
579 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700580 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700581 }
582
Colin Cross3047fa22019-04-18 10:56:44 -0700583 if len(j.exportAidlIncludeDirs) > 0 {
584 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
585 }
586
587 if len(aidlIncludes) > 0 {
588 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
589 }
590
Colin Cross635c3b02016-05-18 15:37:25 -0700591 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800592 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700593 flags = append(flags, "-I"+src.String())
594 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700595
Martijn Coeneneab15642018-03-09 09:29:59 +0100596 if Bool(j.deviceProperties.Aidl.Generate_traces) {
597 flags = append(flags, "-t")
598 }
599
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100600 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
601 flags = append(flags, "--transaction_names")
602 }
603
Colin Cross3047fa22019-04-18 10:56:44 -0700604 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700605}
606
Colin Cross32f676a2017-09-06 13:41:06 -0700607type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800608 classpath classpath
609 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700610 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800611 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700612 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700613 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700614 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700615 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800616 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700617 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700618 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700619 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700620 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800621 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800622
623 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700624}
Colin Cross2fe66872015-03-30 17:20:39 -0700625
Colin Cross54250902017-12-05 09:28:08 -0800626func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
627 for _, f := range dep.Srcs() {
628 if f.Ext() != ".jar" {
629 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
630 ctx.OtherModuleName(dep.(blueprint.Module)))
631 }
632 }
633}
634
Jiyong Park2d492942018-03-05 17:44:10 +0900635type linkType int
636
637const (
638 javaCore linkType = iota
639 javaSdk
640 javaSystem
641 javaPlatform
642)
643
Jiyong Park46f78fb2018-10-20 16:33:17 +0900644func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700645 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700646 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900647 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
648 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100649 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900650 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100651 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900652 return javaCore, false
653 case name == "android_system_stubs_current":
654 return javaSystem, true
655 case strings.HasPrefix(ver, "system_"):
656 return javaSystem, false
657 case name == "android_test_stubs_current":
658 return javaSystem, true
659 case strings.HasPrefix(ver, "test_"):
660 return javaPlatform, false
661 case name == "android_stubs_current":
662 return javaSdk, true
663 case ver == "current":
664 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700665 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900666 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700667 default:
668 if _, err := strconv.Atoi(ver); err != nil {
669 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
670 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900671 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900672 }
673}
674
Jiyong Park750e5572018-01-31 00:20:13 +0900675func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700676 if ctx.Host() {
677 return
678 }
679
Jiyong Park46f78fb2018-10-20 16:33:17 +0900680 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
681 if stubs {
682 return
683 }
684 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900685 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."
686
687 switch myLinkType {
688 case javaCore:
689 if otherLinkType != javaCore {
690 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 +0900691 ctx.OtherModuleName(to))
692 }
Jiyong Park2d492942018-03-05 17:44:10 +0900693 break
694 case javaSdk:
695 if otherLinkType != javaCore && otherLinkType != javaSdk {
696 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
697 ctx.OtherModuleName(to))
698 }
699 break
700 case javaSystem:
701 if otherLinkType == javaPlatform {
702 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
703 ctx.OtherModuleName(to))
704 }
705 break
706 case javaPlatform:
707 // no restriction on link-type
708 break
Jiyong Park750e5572018-01-31 00:20:13 +0900709 }
710}
711
Colin Cross32f676a2017-09-06 13:41:06 -0700712func (j *Module) collectDeps(ctx android.ModuleContext) deps {
713 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700714
Colin Cross300f0382018-03-06 13:11:51 -0800715 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700716 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800717 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700718 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800719 } else if sdkDep.useFiles {
720 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700721 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700722 deps.aidlPreprocess = sdkDep.aidl
723 } else {
724 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800725 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700726 }
727
Colin Crossd11fcda2017-10-23 17:59:01 -0700728 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700729 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700730 tag := ctx.OtherModuleDependencyTag(module)
731
Colin Crossa4f08812018-10-02 22:03:40 -0700732 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700733 // Handled by AndroidApp.collectAppDeps
734 return
735 }
736 if tag == certificateTag {
737 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700738 return
739 }
740
Jiyong Park750e5572018-01-31 00:20:13 +0900741 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700742 switch tag {
743 case bootClasspathTag, libTag, staticLibTag:
744 checkLinkType(ctx, j, to, tag.(dependencyTag))
745 }
Jiyong Park750e5572018-01-31 00:20:13 +0900746 }
Colin Cross54250902017-12-05 09:28:08 -0800747 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800748 case SdkLibraryDependency:
749 switch tag {
750 case libTag:
751 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
752 // names of sdk libs that are directly depended are exported
753 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700754 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800755 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
756 }
Colin Cross54250902017-12-05 09:28:08 -0800757 case Dependency:
758 switch tag {
759 case bootClasspathTag:
760 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700761 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800762 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900763 // sdk lib names from dependencies are re-exported
764 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700765 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800766 case staticLibTag:
767 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
768 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
769 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700770 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900771 // sdk lib names from dependencies are re-exported
772 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700773 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800774 case pluginTag:
775 if plugin, ok := dep.(*Plugin); ok {
776 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
777 if plugin.pluginProperties.Processor_class != nil {
778 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
779 }
780 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
781 } else {
782 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
783 }
Colin Cross54250902017-12-05 09:28:08 -0800784 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100785 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800786 // framework.jar has a one-off dependency on the R.java and Manifest.java files
787 // generated by framework-res.apk
788 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
789 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800790 case frameworkApkTag:
791 if ctx.ModuleName() == "android_stubs_current" ||
792 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700793 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800794 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
795 // resource files out of there for aapt.
796 //
797 // Normally the package rule runs aapt, which includes the resource,
798 // but we're not running that in our package rule so just copy in the
799 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700800 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800801 }
Colin Cross54250902017-12-05 09:28:08 -0800802 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700803 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800804 case kotlinAnnotationsTag:
805 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800806 }
807
Colin Cross54250902017-12-05 09:28:08 -0800808 case android.SourceFileProducer:
809 switch tag {
810 case libTag:
811 checkProducesJars(ctx, dep)
812 deps.classpath = append(deps.classpath, dep.Srcs()...)
813 case staticLibTag:
814 checkProducesJars(ctx, dep)
815 deps.classpath = append(deps.classpath, dep.Srcs()...)
816 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
817 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800818 }
819 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700820 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700821 case systemModulesTag:
822 if deps.systemModules != nil {
823 panic("Found two system module dependencies")
824 }
825 sm := module.(*SystemModules)
826 if sm.outputFile == nil {
827 panic("Missing directory for system module dependency")
828 }
829 deps.systemModules = sm.outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700830 }
Colin Crossec7a0422017-07-07 14:47:12 -0700831 }
Colin Cross2fe66872015-03-30 17:20:39 -0700832 })
833
Jiyong Park1be96912018-05-28 18:02:19 +0900834 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
835
Colin Cross32f676a2017-09-06 13:41:06 -0700836 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700837}
838
Colin Cross83bb3162018-06-25 15:48:06 -0700839func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700840 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800841 v := sdkContext.sdkVersion()
842 // For PDK builds, use the latest SDK version instead of "current"
843 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700844 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800845 latestSdkVersion := 0
846 if len(sdkVersions) > 0 {
847 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
848 }
849 v = strconv.Itoa(latestSdkVersion)
850 }
851
852 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700853 if err != nil {
854 ctx.PropertyErrorf("sdk_version", "%s", err)
855 }
Nan Zhang357466b2018-04-17 17:38:36 -0700856 if javaVersion != "" {
857 ret = javaVersion
858 } else if ctx.Device() && sdk <= 23 {
859 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100860 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700861 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700862 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700863 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
864 ret = "1.8"
865 } else {
866 ret = "1.9"
867 }
868
869 return ret
870}
871
Nan Zhanged19fc32017-10-19 13:06:22 -0700872func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700873
Colin Crossf03c82b2015-04-13 13:53:40 -0700874 var flags javaBuilderFlags
875
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100876 // javaVersion flag.
877 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
878
Nan Zhanged19fc32017-10-19 13:06:22 -0700879 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700880 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100881 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700882 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700883 }
Colin Cross6510f912017-11-29 00:27:14 -0800884 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700885 // Override the -g flag passed globally to remove local variable debug info to reduce
886 // disk and memory usage.
887 javacFlags = append(javacFlags, "-g:source,lines")
888 }
Colin Cross64162712017-08-08 13:17:59 -0700889
Colin Cross66548102018-06-19 22:47:35 -0700890 if ctx.Config().RunErrorProne() {
891 if config.ErrorProneClasspath == nil {
892 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
893 }
894
895 errorProneFlags := []string{
896 "-Xplugin:ErrorProne",
897 "${config.ErrorProneChecks}",
898 }
899 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
900
901 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
902 "'" + strings.Join(errorProneFlags, " ") + "'"
903 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800904 }
905
Nan Zhanged19fc32017-10-19 13:06:22 -0700906 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800907 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
908 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700909 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800910
Colin Crossbe9cdb82019-01-21 21:37:16 -0800911 flags.processor = strings.Join(deps.processorClasses, ",")
912
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100913 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800914 !Bool(j.properties.No_standard_libs) &&
915 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
916 // Give host-side tools a version of OpenJDK's standard libraries
917 // close to what they're targeting. As of Dec 2017, AOSP is only
918 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
919 //
920 // When building with OpenJDK 8, the following should have no
921 // effect since those jars would be available by default.
922 //
923 // When building with OpenJDK 9 but targeting a version < 1.8,
924 // putting them on the bootclasspath means that:
925 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
926 // b) references to existing APIs are not reinterpreted in an
927 // OpenJDK 9-specific way, eg. calls to subclasses of
928 // java.nio.Buffer as in http://b/70862583
929 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
930 flags.bootClasspath = append(flags.bootClasspath,
931 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
932 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800933 if Bool(j.properties.Use_tools_jar) {
934 flags.bootClasspath = append(flags.bootClasspath,
935 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
936 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800937 }
938
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100939 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800940 // Manually specify build directory in case it is not under the repo root.
941 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
942 // just adding a symlink under the root doesn't help.)
943 patchPaths := ".:" + ctx.Config().BuildDir()
944 classPath := flags.classpath.FormJavaClassPath("")
945 if classPath != "" {
946 patchPaths += ":" + classPath
947 }
948 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700949 }
950
Nan Zhanged19fc32017-10-19 13:06:22 -0700951 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700952 if deps.systemModules != nil {
953 flags.systemModules = append(flags.systemModules, deps.systemModules)
954 }
955
Nan Zhanged19fc32017-10-19 13:06:22 -0700956 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700957 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700958
Colin Cross81440082018-08-15 20:21:55 -0700959 if len(javacFlags) > 0 {
960 // optimization.
961 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
962 flags.javacFlags = "$javacFlags"
963 }
964
Nan Zhanged19fc32017-10-19 13:06:22 -0700965 return flags
966}
Colin Crossc0b06f12015-04-08 13:03:43 -0700967
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700968func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
969
970 hasSrcs := false
Nan Zhanged19fc32017-10-19 13:06:22 -0700971
Colin Crossebe1a512017-11-14 13:12:14 -0800972 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700973
974 deps := j.collectDeps(ctx)
975 flags := j.collectBuilderFlags(ctx, deps)
976
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100977 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700978 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
979 }
Colin Cross8a497952019-03-05 22:25:09 -0800980 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700981 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800982 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700983 }
984
Colin Crossaf050172017-11-15 23:01:59 -0800985 srcFiles = j.genSources(ctx, srcFiles, flags)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700986 if len(srcFiles) > 0 {
987 hasSrcs = true
988 }
Colin Crossaf050172017-11-15 23:01:59 -0800989
990 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700991 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700992 if aaptSrcJar != nil {
993 srcJars = append(srcJars, aaptSrcJar)
994 }
Colin Crossb7a63242015-04-16 14:09:14 -0700995
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700996 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
997 // that IDEInfo struct will use
998 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
999
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001000 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001001 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001002 }
1003
Colin Cross1ee23172017-10-18 14:44:18 -07001004 jarName := ctx.ModuleName() + ".jar"
1005
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001006 javaSrcFiles := srcFiles.FilterByExt(".java")
1007 var uniqueSrcFiles android.Paths
1008 set := make(map[string]bool)
1009 for _, v := range javaSrcFiles {
1010 if _, found := set[v.String()]; !found {
1011 set[v.String()] = true
1012 uniqueSrcFiles = append(uniqueSrcFiles, v)
1013 }
1014 }
1015
Colin Cross55f63ea2018-08-27 12:37:09 -07001016 var kotlinJars android.Paths
1017
Colin Cross93e85952017-08-15 13:34:18 -07001018 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001019 // user defined kotlin flags.
1020 kotlincFlags := j.properties.Kotlincflags
1021 CheckKotlincFlags(ctx, kotlincFlags)
1022
Colin Cross93e85952017-08-15 13:34:18 -07001023 // If there are kotlin files, compile them first but pass all the kotlin and java files
1024 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1025 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001026 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001027 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001028 kotlincFlags = append(kotlincFlags, "-no-jdk")
1029 }
1030 if len(kotlincFlags) > 0 {
1031 // optimization.
1032 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1033 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001034 }
1035
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001036 var kotlinSrcFiles android.Paths
1037 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1038 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1039
Colin Crossafbb1732019-01-17 15:42:52 -08001040 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1041 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1042
1043 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1044 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1045
1046 if len(flags.processorPath) > 0 {
1047 // Use kapt for annotation processing
1048 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1049 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1050 srcJars = append(srcJars, kaptSrcJar)
1051 // Disable annotation processing in javac, it's already been handled by kapt
1052 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001053 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001054 }
Colin Cross93e85952017-08-15 13:34:18 -07001055
Colin Cross1ee23172017-10-18 14:44:18 -07001056 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001057 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001058 if ctx.Failed() {
1059 return
1060 }
1061
1062 // Make javac rule depend on the kotlinc rule
1063 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001064
Colin Cross93e85952017-08-15 13:34:18 -07001065 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001066 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001067 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001068 }
1069
Colin Cross55f63ea2018-08-27 12:37:09 -07001070 jars := append(android.Paths(nil), kotlinJars...)
1071
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001072 // Store the list of .java files that was passed to javac
1073 j.compiledJavaSrcs = uniqueSrcFiles
1074 j.compiledSrcJars = srcJars
1075
Nan Zhang61eaedb2017-11-02 13:28:15 -07001076 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001077 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001078 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1079 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001080 // Formerly, there was a check here that prevented annotation processors
1081 // from being used when sharding was enabled, as some annotation processors
1082 // do not function correctly in sharded environments. It was removed to
1083 // allow for the use of annotation processors that do function correctly
1084 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001085 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001086 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001087 if ctx.Failed() {
1088 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001089 }
1090 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001091 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001092 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001093 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001094 // If error-prone is enabled, add an additional rule to compile the java files into
1095 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001096 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001097 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1098 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001099 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001100 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001101 extraJarDeps = append(extraJarDeps, errorprone)
1102 }
1103
Nan Zhang61eaedb2017-11-02 13:28:15 -07001104 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001105 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001106 shardSize := int(*(j.properties.Javac_shard_size))
1107 var shardSrcs []android.Paths
1108 if len(uniqueSrcFiles) > 0 {
1109 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1110 for idx, shardSrc := range shardSrcs {
1111 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1112 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1113 jars = append(jars, classes)
1114 }
1115 }
1116 if len(srcJars) > 0 {
1117 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1118 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1119 jars = append(jars, classes)
1120 }
1121 } else {
1122 classes := android.PathForModuleOut(ctx, "javac", jarName)
1123 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1124 jars = append(jars, classes)
1125 }
Colin Crossd6891432017-09-27 17:39:56 -07001126 if ctx.Failed() {
1127 return
1128 }
Colin Cross2fe66872015-03-30 17:20:39 -07001129 }
1130
Colin Cross0c4ce212019-05-03 15:28:19 -07001131 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1132
1133 var includeSrcJar android.WritablePath
1134 if Bool(j.properties.Include_srcs) {
1135 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1136 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1137 }
1138
Colin Crosscedd4762018-09-13 11:26:19 -07001139 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1140 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001141 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001142 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001143
1144 var resArgs []string
1145 var resDeps android.Paths
1146
1147 resArgs = append(resArgs, dirArgs...)
1148 resDeps = append(resDeps, dirDeps...)
1149
1150 resArgs = append(resArgs, fileArgs...)
1151 resDeps = append(resDeps, fileDeps...)
1152
Colin Cross988708c2019-05-06 14:04:11 -07001153 resArgs = append(resArgs, extraArgs...)
1154 resDeps = append(resDeps, extraDeps...)
1155
Colin Cross40a36712017-09-27 17:41:35 -07001156 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001157 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001158 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001159 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001160 if ctx.Failed() {
1161 return
1162 }
1163 }
1164
Colin Cross0c4ce212019-05-03 15:28:19 -07001165 var resourceJars android.Paths
1166 if j.resourceJar != nil {
1167 resourceJars = append(resourceJars, j.resourceJar)
1168 }
1169 if Bool(j.properties.Include_srcs) {
1170 resourceJars = append(resourceJars, includeSrcJar)
1171 }
1172 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001173
Colin Cross0c4ce212019-05-03 15:28:19 -07001174 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001175 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001176 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001177 false, nil, nil)
1178 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001179 } else if len(resourceJars) == 1 {
1180 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001181 }
1182
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001183 if len(deps.staticJars) > 0 {
1184 jars = append(jars, deps.staticJars...)
1185 hasSrcs = true
1186 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001187
Colin Cross094054a2018-10-17 15:10:48 -07001188 manifest := j.overrideManifest
1189 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001190 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001191 }
Colin Cross635acc92017-09-12 22:50:46 -07001192
Colin Cross8a497952019-03-05 22:25:09 -08001193 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001194 if len(services) > 0 {
1195 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1196 var zipargs []string
1197 for _, file := range services {
1198 serviceFile := file.String()
1199 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1200 }
1201 ctx.Build(pctx, android.BuildParams{
1202 Rule: zip,
1203 Output: servicesJar,
1204 Implicits: services,
1205 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001206 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001207 },
1208 })
1209 jars = append(jars, servicesJar)
1210 }
1211
Colin Cross0a6e0072017-08-30 14:24:55 -07001212 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001213 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001214 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001215
1216 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001217 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1218 // Optimization: skip the combine step if there is nothing to do
1219 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1220 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1221 // any if len(jars) == 1.
1222 outputFile = moduleOutPath
1223 } else {
1224 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1225 ctx.Build(pctx, android.BuildParams{
1226 Rule: android.Cp,
1227 Input: jars[0],
1228 Output: combinedJar,
1229 })
1230 outputFile = combinedJar
1231 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001232 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001233 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001234 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001235 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001236 outputFile = combinedJar
1237 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001238
Colin Cross331a1212018-08-15 20:40:52 -07001239 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001240 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001241 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001242 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001243 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001244 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001245
1246 // jarjar resource jar if necessary
1247 if j.resourceJar != nil {
1248 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001249 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001250 j.resourceJar = resourceJarJarFile
1251 }
1252
Colin Cross0a6e0072017-08-30 14:24:55 -07001253 if ctx.Failed() {
1254 return
1255 }
1256 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001257
1258 // Check package restrictions if necessary.
1259 if len(j.properties.Permitted_packages) > 0 {
1260 // Check packages and copy to package-checked file.
1261 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1262 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1263 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1264
1265 if ctx.Failed() {
1266 return
1267 }
1268 }
1269
Nan Zhanged19fc32017-10-19 13:06:22 -07001270 j.implementationJarFile = outputFile
1271 if j.headerJarFile == nil {
1272 j.headerJarFile = j.implementationJarFile
1273 }
Colin Cross2fe66872015-03-30 17:20:39 -07001274
Colin Cross6510f912017-11-29 00:27:14 -08001275 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001276 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1277 j.properties.Instrument = true
1278 }
1279 }
1280
Colin Cross3144dfc2018-01-03 15:06:47 -08001281 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001282 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1283 }
1284
Colin Cross331a1212018-08-15 20:40:52 -07001285 // merge implementation jar with resources if necessary
1286 implementationAndResourcesJar := outputFile
1287 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001288 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001289 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001290 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001291 false, nil, nil)
1292 implementationAndResourcesJar = combinedJar
1293 }
1294
1295 j.implementationAndResourcesJar = implementationAndResourcesJar
1296
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001297 if ctx.Device() && hasSrcs &&
1298 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001299 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001300 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001301 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001302 if ctx.Failed() {
1303 return
1304 }
Colin Cross331a1212018-08-15 20:40:52 -07001305
Colin Cross9c74a1e2019-05-28 14:06:17 -07001306 if !ctx.Config().UnbundledBuild() {
1307 // Hidden API CSV generation and dex encoding
1308 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1309 j.deviceProperties.UncompressDex)
1310 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001311
Colin Cross331a1212018-08-15 20:40:52 -07001312 // merge dex jar with resources if necessary
1313 if j.resourceJar != nil {
1314 jars := android.Paths{dexOutputFile, j.resourceJar}
1315 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1316 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1317 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001318 if j.deviceProperties.UncompressDex {
1319 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1320 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1321 dexOutputFile = combinedAlignedJar
1322 } else {
1323 dexOutputFile = combinedJar
1324 }
Colin Cross331a1212018-08-15 20:40:52 -07001325 }
1326
1327 j.dexJarFile = dexOutputFile
1328
Colin Cross8faf8fc2019-01-16 15:15:52 -08001329 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001330 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1331
1332 j.maybeStrippedDexJarFile = dexOutputFile
1333
Colin Cross3063b782018-08-15 11:19:12 -07001334 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001335
1336 if ctx.Failed() {
1337 return
1338 }
Colin Cross331a1212018-08-15 20:40:52 -07001339 } else {
1340 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001341 }
Colin Cross331a1212018-08-15 20:40:52 -07001342
Colin Crossb7a63242015-04-16 14:09:14 -07001343 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001344
1345 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1346 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001347}
1348
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001349// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1350// since some of these flags may be used internally.
1351func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1352 for _, flag := range flags {
1353 flag = strings.TrimSpace(flag)
1354
1355 if !strings.HasPrefix(flag, "-") {
1356 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1357 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1358 ctx.PropertyErrorf("kotlincflags",
1359 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1360 } else if inList(flag, config.KotlincIllegalFlags) {
1361 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1362 } else if flag == "-include-runtime" {
1363 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1364 } else {
1365 args := strings.Split(flag, " ")
1366 if args[0] == "-kotlin-home" {
1367 ctx.PropertyErrorf("kotlincflags",
1368 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1369 }
1370 }
1371 }
1372}
1373
Colin Cross8eadbf02017-10-24 17:46:00 -07001374func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001375 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001376
1377 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001378 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001379 // Compile java sources into turbine.jar.
1380 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1381 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1382 if ctx.Failed() {
1383 return nil
1384 }
1385 jars = append(jars, turbineJar)
1386 }
1387
Colin Cross55f63ea2018-08-27 12:37:09 -07001388 jars = append(jars, extraJars...)
1389
Nan Zhanged19fc32017-10-19 13:06:22 -07001390 // Combine any static header libraries into classes-header.jar. If there is only
1391 // one input jar this step will be skipped.
1392 var headerJar android.Path
1393 jars = append(jars, deps.staticHeaderJars...)
1394
Colin Cross5c6ecc12017-10-23 18:12:27 -07001395 // we cannot skip the combine step for now if there is only one jar
1396 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1397 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001398 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001399 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001400 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001401
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001402 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001403 // Transform classes.jar into classes-jarjar.jar
1404 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001405 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001406 headerJar = jarjarFile
1407 if ctx.Failed() {
1408 return nil
1409 }
1410 }
1411
1412 return headerJar
1413}
1414
Colin Crosscb933592017-11-22 13:49:43 -08001415func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001416 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001417
Colin Cross7a3139e2017-12-19 13:57:50 -08001418 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001419
Colin Cross84c38822018-01-03 15:59:46 -08001420 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001421 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1422
1423 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1424
1425 j.jacocoReportClassesFile = jacocoReportClassesFile
1426
1427 return instrumentedJar
1428}
1429
albaltai36ff7dc2018-12-25 14:35:23 +08001430var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001431
Nan Zhanged19fc32017-10-19 13:06:22 -07001432func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001433 if j.headerJarFile == nil {
1434 return nil
1435 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001436 return android.Paths{j.headerJarFile}
1437}
1438
1439func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001440 if j.implementationJarFile == nil {
1441 return nil
1442 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001443 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001444}
1445
Colin Crossf24a22a2019-01-31 14:12:44 -08001446func (j *Module) DexJar() android.Path {
1447 return j.dexJarFile
1448}
1449
Colin Cross331a1212018-08-15 20:40:52 -07001450func (j *Module) ResourceJars() android.Paths {
1451 if j.resourceJar == nil {
1452 return nil
1453 }
1454 return android.Paths{j.resourceJar}
1455}
1456
1457func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001458 if j.implementationAndResourcesJar == nil {
1459 return nil
1460 }
Colin Cross331a1212018-08-15 20:40:52 -07001461 return android.Paths{j.implementationAndResourcesJar}
1462}
1463
Colin Cross46c9b8b2017-06-22 16:51:17 -07001464func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001465 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001466 return j.exportAidlIncludeDirs
1467}
1468
Jiyong Park1be96912018-05-28 18:02:19 +09001469func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001470 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001471 return j.exportedSdkLibs
1472}
1473
Colin Cross0c4ce212019-05-03 15:28:19 -07001474func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1475 return j.srcJarArgs, j.srcJarDeps
1476}
1477
Colin Cross46c9b8b2017-06-22 16:51:17 -07001478var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001479
Colin Cross46c9b8b2017-06-22 16:51:17 -07001480func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001481 return j.logtagsSrcs
1482}
1483
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001484// Collect information for opening IDE project files in java/jdeps.go.
1485func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1486 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1487 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001488 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001489 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001490 if j.expandJarjarRules != nil {
1491 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001492 }
1493}
1494
1495func (j *Module) CompilerDeps() []string {
1496 jdeps := []string{}
1497 jdeps = append(jdeps, j.properties.Libs...)
1498 jdeps = append(jdeps, j.properties.Static_libs...)
1499 return jdeps
1500}
1501
Colin Cross2fe66872015-03-30 17:20:39 -07001502//
1503// Java libraries (.jar file)
1504//
1505
Colin Crossf506d872017-07-19 15:53:04 -07001506type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001507 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001508}
1509
Colin Cross42be7612019-02-21 18:12:14 -08001510func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001511 // Store uncompressed (and do not strip) dex files from boot class path jars.
1512 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1513 return true
1514 }
1515
1516 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001517 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001518 return true
1519 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001520 if ctx.Config().UncompressPrivAppDex() &&
1521 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1522 return true
1523 }
1524
Colin Cross2fc72f62018-12-21 12:59:54 -08001525 return false
1526}
1527
Colin Crossf506d872017-07-19 15:53:04 -07001528func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001529 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1530 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001531 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001532 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001533 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001534 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001535
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001536 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001537 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1538 ctx.ModuleName()+".jar", j.outputFile)
1539 }
Colin Crossb7a63242015-04-16 14:09:14 -07001540}
1541
Colin Crossf506d872017-07-19 15:53:04 -07001542func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001543 j.deps(ctx)
1544}
1545
Colin Cross1b16b0e2019-02-12 14:41:32 -08001546// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1547//
1548// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1549// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1550// as a `static_libs` dependency of another module.
1551//
1552// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1553// a device.
1554//
1555// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1556// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001557func LibraryFactory() android.Module {
1558 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001559
Colin Cross9ae1b922018-06-26 17:59:05 -07001560 module.AddProperties(
1561 &module.Module.properties,
1562 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001563 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001564 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001565
Colin Cross9ae1b922018-06-26 17:59:05 -07001566 InitJavaModule(module, android.HostAndDeviceSupported)
1567 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001568}
1569
Colin Cross1b16b0e2019-02-12 14:41:32 -08001570// java_library_static is an obsolete alias for java_library.
1571func LibraryStaticFactory() android.Module {
1572 return LibraryFactory()
1573}
1574
1575// java_library_host builds and links sources into a `.jar` file for the host.
1576//
1577// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1578// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001579func LibraryHostFactory() android.Module {
1580 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001581
Colin Cross6af17aa2017-09-20 12:59:05 -07001582 module.AddProperties(
1583 &module.Module.properties,
1584 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001585
Colin Cross9ae1b922018-06-26 17:59:05 -07001586 module.Module.properties.Installable = proptools.BoolPtr(true)
1587
Colin Cross89536d42017-07-07 14:35:50 -07001588 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001589 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001590}
1591
1592//
Colin Crossb628ea52018-08-14 16:42:33 -07001593// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001594//
1595
1596type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001597 // list of compatibility suites (for example "cts", "vts") that the module should be
1598 // installed into.
1599 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001600
1601 // the name of the test configuration (for example "AndroidTest.xml") that should be
1602 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001603 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001604
Jack He33338892018-09-19 02:21:28 -07001605 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1606 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001607 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001608
Colin Crossd96ca352018-08-10 16:06:24 -07001609 // list of files or filegroup modules that provide data that should be installed alongside
1610 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001611 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001612}
1613
Paul Duffin42df1442019-03-20 12:45:53 +00001614type testHelperLibraryProperties struct {
1615 // list of compatibility suites (for example "cts", "vts") that the module should be
1616 // installed into.
1617 Test_suites []string `android:"arch_variant"`
1618}
1619
Colin Cross05638fc2018-04-09 18:40:24 -07001620type Test struct {
1621 Library
1622
1623 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001624
1625 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001626 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001627}
1628
Paul Duffin42df1442019-03-20 12:45:53 +00001629type TestHelperLibrary struct {
1630 Library
1631
1632 testHelperLibraryProperties testHelperLibraryProperties
1633}
1634
Colin Cross303e21f2018-08-07 16:49:25 -07001635func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001636 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 -08001637 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001638
1639 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001640}
1641
Paul Duffin42df1442019-03-20 12:45:53 +00001642func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1643 j.Library.GenerateAndroidBuildActions(ctx)
1644}
1645
Colin Cross1b16b0e2019-02-12 14:41:32 -08001646// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1647// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1648//
1649// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1650// compiled against the device bootclasspath.
1651//
1652// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1653// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001654func TestFactory() android.Module {
1655 module := &Test{}
1656
1657 module.AddProperties(
1658 &module.Module.properties,
1659 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001660 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001661 &module.Module.protoProperties,
1662 &module.testProperties)
1663
Colin Cross9ae1b922018-06-26 17:59:05 -07001664 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001665 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001666
Colin Cross05638fc2018-04-09 18:40:24 -07001667 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001668 return module
1669}
1670
Paul Duffin42df1442019-03-20 12:45:53 +00001671// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1672func TestHelperLibraryFactory() android.Module {
1673 module := &TestHelperLibrary{}
1674
1675 module.AddProperties(
1676 &module.Module.properties,
1677 &module.Module.deviceProperties,
1678 &module.Module.dexpreoptProperties,
1679 &module.Module.protoProperties,
1680 &module.testHelperLibraryProperties)
1681
Colin Cross9a4abed2019-04-24 13:19:28 -07001682 module.Module.properties.Installable = proptools.BoolPtr(true)
1683 module.Module.dexpreopter.isTest = true
1684
Paul Duffin42df1442019-03-20 12:45:53 +00001685 InitJavaModule(module, android.HostAndDeviceSupported)
1686 return module
1687}
1688
Colin Cross1b16b0e2019-02-12 14:41:32 -08001689// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1690// allow running the test with `atest` or a `TEST_MAPPING` file.
1691//
1692// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1693// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001694func TestHostFactory() android.Module {
1695 module := &Test{}
1696
1697 module.AddProperties(
1698 &module.Module.properties,
1699 &module.Module.protoProperties,
1700 &module.testProperties)
1701
Colin Cross9ae1b922018-06-26 17:59:05 -07001702 module.Module.properties.Installable = proptools.BoolPtr(true)
1703
Colin Cross05638fc2018-04-09 18:40:24 -07001704 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001705 return module
1706}
1707
1708//
Colin Cross2fe66872015-03-30 17:20:39 -07001709// Java Binaries (.jar file plus wrapper script)
1710//
1711
Colin Crossf506d872017-07-19 15:53:04 -07001712type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001713 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001714 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001715
1716 // Name of the class containing main to be inserted into the manifest as Main-Class.
1717 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001718}
1719
Colin Crossf506d872017-07-19 15:53:04 -07001720type Binary struct {
1721 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001722
Colin Crossf506d872017-07-19 15:53:04 -07001723 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001724
Colin Cross6b4a32d2017-12-05 13:42:45 -08001725 isWrapperVariant bool
1726
Colin Crossc3315992017-12-08 19:12:36 -08001727 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001728 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001729}
1730
Alex Light24237172017-10-26 09:46:21 -07001731func (j *Binary) HostToolPath() android.OptionalPath {
1732 return android.OptionalPathForPath(j.binaryFile)
1733}
1734
Colin Crossf506d872017-07-19 15:53:04 -07001735func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001736 if ctx.Arch().ArchType == android.Common {
1737 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001738 if j.binaryProperties.Main_class != nil {
1739 if j.properties.Manifest != nil {
1740 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1741 }
1742 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1743 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1744 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1745 }
1746
Colin Cross6b4a32d2017-12-05 13:42:45 -08001747 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001748 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001749 // Handle the binary wrapper
1750 j.isWrapperVariant = true
1751
Colin Cross366938f2017-12-11 16:29:02 -08001752 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001753 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001754 } else {
1755 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1756 }
1757
1758 // Depend on the installed jar so that the wrapper doesn't get executed by
1759 // another build rule before the jar has been installed.
1760 jarFile := ctx.PrimaryModule().(*Binary).installFile
1761
1762 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1763 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001764 }
Colin Cross2fe66872015-03-30 17:20:39 -07001765}
1766
Colin Crossf506d872017-07-19 15:53:04 -07001767func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001768 if ctx.Arch().ArchType == android.Common {
1769 j.deps(ctx)
1770 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001771}
1772
Colin Cross1b16b0e2019-02-12 14:41:32 -08001773// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1774// as well.
1775//
1776// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1777// compiled against the device bootclasspath.
1778//
1779// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1780// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001781func BinaryFactory() android.Module {
1782 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001783
Colin Cross36242852017-06-23 15:06:31 -07001784 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001785 &module.Module.properties,
1786 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001787 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001788 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001789 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001790
Colin Cross9ae1b922018-06-26 17:59:05 -07001791 module.Module.properties.Installable = proptools.BoolPtr(true)
1792
Colin Cross6b4a32d2017-12-05 13:42:45 -08001793 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1794 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001795 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001796}
1797
Colin Cross1b16b0e2019-02-12 14:41:32 -08001798// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1799//
1800// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1801// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001802func BinaryHostFactory() android.Module {
1803 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001804
Colin Cross36242852017-06-23 15:06:31 -07001805 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001806 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001807 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001808 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001809
Colin Cross9ae1b922018-06-26 17:59:05 -07001810 module.Module.properties.Installable = proptools.BoolPtr(true)
1811
Colin Cross6b4a32d2017-12-05 13:42:45 -08001812 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1813 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001814 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001815}
1816
1817//
1818// Java prebuilts
1819//
1820
Colin Cross74d73e22017-08-02 11:05:49 -07001821type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001822 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001823
Nan Zhangea568a42017-11-08 21:20:04 -08001824 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001825
1826 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001827
1828 // List of shared java libs that this module has dependencies to
1829 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001830
1831 // List of files to remove from the jar file(s)
1832 Exclude_files []string
1833
1834 // List of directories to remove from the jar file(s)
1835 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001836
1837 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001838 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001839}
1840
1841type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001842 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001843 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001844 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001845
Colin Cross74d73e22017-08-02 11:05:49 -07001846 properties ImportProperties
1847
Colin Cross0a6e0072017-08-30 14:24:55 -07001848 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001849 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001850}
1851
Colin Cross83bb3162018-06-25 15:48:06 -07001852func (j *Import) sdkVersion() string {
1853 return String(j.properties.Sdk_version)
1854}
1855
1856func (j *Import) minSdkVersion() string {
1857 return j.sdkVersion()
1858}
1859
Colin Cross74d73e22017-08-02 11:05:49 -07001860func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001861 return &j.prebuilt
1862}
1863
Colin Cross74d73e22017-08-02 11:05:49 -07001864func (j *Import) PrebuiltSrcs() []string {
1865 return j.properties.Jars
1866}
1867
1868func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001869 return j.prebuilt.Name(j.ModuleBase.Name())
1870}
1871
Colin Cross74d73e22017-08-02 11:05:49 -07001872func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001873 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001874}
1875
Colin Cross74d73e22017-08-02 11:05:49 -07001876func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001877 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001878
Nan Zhang4c819fb2018-08-27 18:31:46 -07001879 jarName := ctx.ModuleName() + ".jar"
1880 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001881 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1882 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001883 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001884 inputFile := outputFile
1885 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1886 TransformJetifier(ctx, outputFile, inputFile)
1887 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001888 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001889
1890 ctx.VisitDirectDeps(func(module android.Module) {
1891 otherName := ctx.OtherModuleName(module)
1892 tag := ctx.OtherModuleDependencyTag(module)
1893
1894 switch dep := module.(type) {
1895 case Dependency:
1896 switch tag {
1897 case libTag, staticLibTag:
1898 // sdk lib names from dependencies are re-exported
1899 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1900 }
1901 case SdkLibraryDependency:
1902 switch tag {
1903 case libTag:
1904 // names of sdk libs that are directly depended are exported
1905 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1906 }
1907 }
1908 })
1909
1910 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001911 if Bool(j.properties.Installable) {
1912 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1913 ctx.ModuleName()+".jar", outputFile)
1914 }
Colin Cross2fe66872015-03-30 17:20:39 -07001915}
1916
Colin Cross74d73e22017-08-02 11:05:49 -07001917var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001918
Nan Zhanged19fc32017-10-19 13:06:22 -07001919func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001920 if j.combinedClasspathFile == nil {
1921 return nil
1922 }
Colin Cross37f6d792018-07-12 12:28:41 -07001923 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001924}
1925
1926func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001927 if j.combinedClasspathFile == nil {
1928 return nil
1929 }
Colin Cross37f6d792018-07-12 12:28:41 -07001930 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001931}
1932
Colin Cross331a1212018-08-15 20:40:52 -07001933func (j *Import) ResourceJars() android.Paths {
1934 return nil
1935}
1936
1937func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001938 if j.combinedClasspathFile == nil {
1939 return nil
1940 }
Colin Cross331a1212018-08-15 20:40:52 -07001941 return android.Paths{j.combinedClasspathFile}
1942}
1943
Colin Crossf24a22a2019-01-31 14:12:44 -08001944func (j *Import) DexJar() android.Path {
1945 return nil
1946}
1947
Colin Cross74d73e22017-08-02 11:05:49 -07001948func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001949 return nil
1950}
1951
Jiyong Park1be96912018-05-28 18:02:19 +09001952func (j *Import) ExportedSdkLibs() []string {
1953 return j.exportedSdkLibs
1954}
1955
Colin Cross0c4ce212019-05-03 15:28:19 -07001956func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1957 return nil, nil
1958}
1959
albaltai36ff7dc2018-12-25 14:35:23 +08001960// Add compile time check for interface implementation
1961var _ android.IDEInfo = (*Import)(nil)
1962var _ android.IDECustomizedModuleName = (*Import)(nil)
1963
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001964// Collect information for opening IDE project files in java/jdeps.go.
1965const (
1966 removedPrefix = "prebuilt_"
1967)
1968
1969func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1970 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1971}
1972
1973func (j *Import) IDECustomizedModuleName() string {
1974 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1975 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1976 // solution to get the Import name.
1977 name := j.Name()
1978 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001979 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001980 }
1981 return name
1982}
1983
Colin Cross74d73e22017-08-02 11:05:49 -07001984var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001985
Colin Cross1b16b0e2019-02-12 14:41:32 -08001986// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1987//
1988// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1989// compiled against an Android classpath.
1990//
1991// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1992// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001993func ImportFactory() android.Module {
1994 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001995
Colin Cross74d73e22017-08-02 11:05:49 -07001996 module.AddProperties(&module.properties)
1997
1998 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001999 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002000 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002001}
2002
Colin Cross1b16b0e2019-02-12 14:41:32 -08002003// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2004// module.
2005//
2006// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2007// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002008func ImportFactoryHost() android.Module {
2009 module := &Import{}
2010
2011 module.AddProperties(&module.properties)
2012
2013 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002014 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002015 return module
2016}
2017
Colin Cross42be7612019-02-21 18:12:14 -08002018// dex_import module
2019
2020type DexImportProperties struct {
2021 Jars []string
2022}
2023
2024type DexImport struct {
2025 android.ModuleBase
2026 android.DefaultableModuleBase
2027 prebuilt android.Prebuilt
2028
2029 properties DexImportProperties
2030
2031 dexJarFile android.Path
2032 maybeStrippedDexJarFile android.Path
2033
2034 dexpreopter
2035}
2036
2037func (j *DexImport) Prebuilt() *android.Prebuilt {
2038 return &j.prebuilt
2039}
2040
2041func (j *DexImport) PrebuiltSrcs() []string {
2042 return j.properties.Jars
2043}
2044
2045func (j *DexImport) Name() string {
2046 return j.prebuilt.Name(j.ModuleBase.Name())
2047}
2048
2049func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2050 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2051}
2052
2053func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2054 if len(j.properties.Jars) != 1 {
2055 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2056 }
2057
2058 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2059 j.dexpreopter.isInstallable = true
2060 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2061
2062 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2063 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2064
2065 if j.dexpreopter.uncompressedDex {
2066 rule := android.NewRuleBuilder()
2067
2068 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2069 rule.Temporary(temporary)
2070
2071 // use zip2zip to uncompress classes*.dex files
2072 rule.Command().
2073 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2074 FlagWithInput("-i ", inputJar).
2075 FlagWithOutput("-o ", temporary).
2076 FlagWithArg("-0 ", "'classes*.dex'")
2077
2078 // use zipalign to align uncompressed classes*.dex files
2079 rule.Command().
2080 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2081 Flag("-f").
2082 Text("4").
2083 Input(temporary).
2084 Output(dexOutputFile)
2085
2086 rule.DeleteTemporaryFiles()
2087
2088 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2089 } else {
2090 ctx.Build(pctx, android.BuildParams{
2091 Rule: android.Cp,
2092 Input: inputJar,
2093 Output: dexOutputFile,
2094 })
2095 }
2096
2097 j.dexJarFile = dexOutputFile
2098
2099 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2100
2101 j.maybeStrippedDexJarFile = dexOutputFile
2102
2103 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2104 ctx.ModuleName()+".jar", dexOutputFile)
2105}
2106
2107func (j *DexImport) DexJar() android.Path {
2108 return j.dexJarFile
2109}
2110
2111// dex_import imports a `.jar` file containing classes.dex files.
2112//
2113// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2114// to the device.
2115func DexImportFactory() android.Module {
2116 module := &DexImport{}
2117
2118 module.AddProperties(&module.properties)
2119
2120 android.InitPrebuiltModule(module, &module.properties.Jars)
2121 InitJavaModule(module, android.DeviceSupported)
2122 return module
2123}
2124
Colin Cross89536d42017-07-07 14:35:50 -07002125//
2126// Defaults
2127//
2128type Defaults struct {
2129 android.ModuleBase
2130 android.DefaultsModuleBase
2131}
2132
2133func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2134}
2135
Colin Cross1b16b0e2019-02-12 14:41:32 -08002136// java_defaults provides a set of properties that can be inherited by other java or android modules.
2137//
2138// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2139// property in the defaults module that exists in the depending module will be prepended to the depending module's
2140// value for that property.
2141//
2142// Example:
2143//
2144// java_defaults {
2145// name: "example_defaults",
2146// srcs: ["common/**/*.java"],
2147// javacflags: ["-Xlint:all"],
2148// aaptflags: ["--auto-add-overlay"],
2149// }
2150//
2151// java_library {
2152// name: "example",
2153// defaults: ["example_defaults"],
2154// srcs: ["example/**/*.java"],
2155// }
2156//
2157// is functionally identical to:
2158//
2159// java_library {
2160// name: "example",
2161// srcs: [
2162// "common/**/*.java",
2163// "example/**/*.java",
2164// ],
2165// javacflags: ["-Xlint:all"],
2166// }
Colin Cross89536d42017-07-07 14:35:50 -07002167func defaultsFactory() android.Module {
2168 return DefaultsFactory()
2169}
2170
2171func DefaultsFactory(props ...interface{}) android.Module {
2172 module := &Defaults{}
2173
2174 module.AddProperties(props...)
2175 module.AddProperties(
2176 &CompilerProperties{},
2177 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002178 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002179 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002180 &aaptProperties{},
2181 &androidLibraryProperties{},
2182 &appProperties{},
2183 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002184 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002185 &ImportProperties{},
2186 &AARImportProperties{},
2187 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002188 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002189 )
2190
2191 android.InitDefaultsModule(module)
2192
2193 return module
2194}
Nan Zhangea568a42017-11-08 21:20:04 -08002195
2196var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002197var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002198var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002199var inList = android.InList