blob: bf62578f8279a3c2c4f15e1558bd5d6aa01fa4bb [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Colin Cross2fe66872015-03-30 17:20:39 -070055// TODO:
56// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070057// Renderscript
58// Post-jar passes:
59// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070060// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070061// DroidDoc
62// Findbugs
63
Colin Cross89536d42017-07-07 14:35:50 -070064type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070065 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
66 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070068
69 // list of source files that should not be used to build the Java module.
70 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080071 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070072
73 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070074 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070075
Colin Cross86a63ff2017-09-27 17:33:10 -070076 // list of directories that should be excluded from java_resource_dirs
77 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070078
Colin Cross0f37af02017-09-27 17:42:05 -070079 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080080 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070081
Colin Crosscedd4762018-09-13 11:26:19 -070082 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080083 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070084
Paul Duffin2fbbfb82019-02-13 12:49:33 +000085 // don't build against the default libraries (bootclasspath, ext, and framework for device
86 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070087 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070088
Paul Duffin2fbbfb82019-02-13 12:49:33 +000089 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070090 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020095 // list of module-specific flags that will be used for kotlinc compiles
96 Kotlincflags []string `android:"arch_variant"`
97
Colin Cross7d5136f2015-05-11 13:39:40 -070098 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700102 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
104 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700106
Colin Cross540eff82017-06-22 17:01:52 -0700107 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700109
110 // If not blank, set the java version passed to javac as -source and -target
111 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700112
Colin Cross9ae1b922018-06-26 17:59:05 -0700113 // If set to true, allow this module to be dexed and installed on devices. Has no
114 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700115 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700116
Colin Cross0f37af02017-09-27 17:42:05 -0700117 // If set to true, include sources used to compile the module in to the final jar
118 Include_srcs *bool
119
Vladimir Marko0975ee02019-04-02 10:29:55 +0100120 // If not empty, classes are restricted to the specified packages and their sub-packages.
121 // This restriction is checked after applying jarjar rules and including static libs.
122 Permitted_packages []string
123
Colin Crossbe9cdb82019-01-21 21:37:16 -0800124 // List of modules to use as annotation processors
125 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700126
Nan Zhang61eaedb2017-11-02 13:28:15 -0700127 // The number of Java source entries each Javac instance can process
128 Javac_shard_size *int64
129
Nan Zhang5f8cb422018-02-06 10:34:32 -0800130 // Add host jdk tools.jar to bootclasspath
131 Use_tools_jar *bool
132
Colin Cross1369cdb2017-09-29 17:58:17 -0700133 Openjdk9 struct {
134 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800135 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700136
137 // List of javac flags that should only be used when passing -source 1.9
138 Javacflags []string
139 }
Colin Crosscb933592017-11-22 13:49:43 -0800140
Colin Cross81440082018-08-15 20:21:55 -0700141 // When compiling language level 9+ .java code in packages that are part of
142 // a system module, patch_module names the module that your sources and
143 // dependencies should be patched into. The Android runtime currently
144 // doesn't implement the JEP 261 module system so this option is only
145 // supported at compile time. It should only be needed to compile tests in
146 // packages that exist in libcore and which are inconvenient to move
147 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100148 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700149
Colin Crosscb933592017-11-22 13:49:43 -0800150 Jacoco struct {
151 // List of classes to include for instrumentation with jacoco to collect coverage
152 // information at runtime when building with coverage enabled. If unset defaults to all
153 // classes.
154 // Supports '*' as the last character of an entry in the list as a wildcard match.
155 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
156 // it matches classes in the package that have the class name as a prefix.
157 Include_filter []string
158
159 // List of classes to exclude from instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. Overrides classes selected
161 // by the include_filter property.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Exclude_filter []string
166 }
167
Andreas Gampef3e5b552018-01-22 21:27:21 -0800168 Errorprone struct {
169 // List of javac flags that should only be used when running errorprone.
170 Javacflags []string
171 }
172
Colin Cross0f2ee152017-12-14 15:22:43 -0800173 Proto struct {
174 // List of extra options that will be passed to the proto generator.
175 Output_params []string
176 }
177
Colin Crosscb933592017-11-22 13:49:43 -0800178 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800179
180 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800181 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700182}
183
Colin Cross89536d42017-07-07 14:35:50 -0700184type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700185 // list of module-specific flags that will be used for dex compiles
186 Dxflags []string `android:"arch_variant"`
187
Colin Cross83bb3162018-06-25 15:48:06 -0700188 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
189 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800190 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700191
Colin Cross83bb3162018-06-25 15:48:06 -0700192 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
193 // Defaults to sdk_version if not set.
194 Min_sdk_version *string
195
Dan Willemsen419290a2018-10-31 15:28:47 -0700196 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
197 // Defaults to sdk_version if not set.
198 Target_sdk_version *string
199
Colin Cross6af2e492018-05-22 11:12:33 -0700200 // if true, compile against the platform APIs instead of an SDK.
201 Platform_apis *bool
202
Colin Crossebe1a512017-11-14 13:12:14 -0800203 Aidl struct {
204 // Top level directories to pass to aidl tool
205 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700206
Colin Crossebe1a512017-11-14 13:12:14 -0800207 // Directories rooted at the Android.bp file to pass to aidl tool
208 Local_include_dirs []string
209
210 // directories that should be added as include directories for any aidl sources of modules
211 // that depend on this module, as well as to aidl for this module.
212 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100213
214 // whether to generate traces (for systrace) for this interface
215 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100216
217 // whether to generate Binder#GetTransaction name method.
218 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800219 }
Colin Cross92430102017-10-09 14:59:32 -0700220
221 // If true, export a copy of the module as a -hostdex module for host testing.
222 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700223
David Brazdil17ef5632018-06-27 10:27:45 +0100224 // If set to true, compile dex regardless of installable. Defaults to false.
225 Compile_dex *bool
226
Colin Cross66dbc0b2017-12-28 12:23:20 -0800227 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700228 // If false, disable all optimization. Defaults to true for android_app and android_test
229 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800230 Enabled *bool
231
232 // If true, optimize for size by removing unused code. Defaults to true for apps,
233 // false for libraries and tests.
234 Shrink *bool
235
236 // If true, optimize bytecode. Defaults to false.
237 Optimize *bool
238
239 // If true, obfuscate bytecode. Defaults to false.
240 Obfuscate *bool
241
242 // If true, do not use the flag files generated by aapt that automatically keep
243 // classes referenced by the app manifest. Defaults to false.
244 No_aapt_flags *bool
245
246 // Flags to pass to proguard.
247 Proguard_flags []string
248
249 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800250 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800251 }
252
Colin Cross1369cdb2017-09-29 17:58:17 -0700253 // When targeting 1.9, override the modules to use with --system
254 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700255
256 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800257 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700258}
259
Colin Cross46c9b8b2017-06-22 16:51:17 -0700260// Module contains the properties and members used by all java module types
261type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700262 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700263 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700264
Colin Cross89536d42017-07-07 14:35:50 -0700265 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700266 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700267 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700268
Colin Cross331a1212018-08-15 20:40:52 -0700269 // jar file containing header classes including static library dependencies, suitable for
270 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700271 headerJarFile android.Path
272
Colin Cross331a1212018-08-15 20:40:52 -0700273 // jar file containing implementation classes including static library dependencies but no
274 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700275 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700276
Colin Cross331a1212018-08-15 20:40:52 -0700277 // jar file containing only resources including from static library dependencies
278 resourceJar android.Path
279
280 // jar file containing implementation classes and resources including static library
281 // dependencies
282 implementationAndResourcesJar android.Path
283
284 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700285 dexJarFile android.Path
286
Colin Cross43f08db2018-11-12 10:13:39 -0800287 // output file that contains classes.dex if it should be in the output file
288 maybeStrippedDexJarFile android.Path
289
Colin Crosscb933592017-11-22 13:49:43 -0800290 // output file containing uninstrumented classes that will be instrumented by jacoco
291 jacocoReportClassesFile android.Path
292
Colin Cross66dbc0b2017-12-28 12:23:20 -0800293 // output file containing mapping of obfuscated names
294 proguardDictionary android.Path
295
Colin Cross331a1212018-08-15 20:40:52 -0700296 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700297 outputFile android.Path
298 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700299
Colin Cross635c3b02016-05-18 15:37:25 -0700300 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700301
Colin Cross635c3b02016-05-18 15:37:25 -0700302 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700303
Colin Cross2fe66872015-03-30 17:20:39 -0700304 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700305 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800306
307 // list of .java files and srcjars that was passed to javac
308 compiledJavaSrcs android.Paths
309 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800310
311 // list of extra progurad flag files
312 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900313
Colin Cross094054a2018-10-17 15:10:48 -0700314 // manifest file to use instead of properties.Manifest
315 overrideManifest android.OptionalPath
316
Jiyong Park1be96912018-05-28 18:02:19 +0900317 // list of SDK lib names that this java moudule is exporting
318 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700319
320 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
321 // filter out Exclude_srcs, will be used by android.IDEInfo struct
322 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800323
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800324 // expanded Jarjar_rules
325 expandJarjarRules android.Path
326
Vladimir Marko0975ee02019-04-02 10:29:55 +0100327 // list of additional targets for checkbuild
328 additionalCheckedModules android.Paths
329
Colin Crossf24a22a2019-01-31 14:12:44 -0800330 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800331 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700332}
333
Colin Cross54250902017-12-05 09:28:08 -0800334func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700335 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800336}
337
Jiyong Park8fd61922018-11-08 02:50:25 +0900338func (j *Module) DexJarFile() android.Path {
339 return j.dexJarFile
340}
341
Colin Cross54250902017-12-05 09:28:08 -0800342var _ android.SourceFileProducer = (*Module)(nil)
343
Colin Crossf506d872017-07-19 15:53:04 -0700344type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700345 HeaderJars() android.Paths
346 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700347 ResourceJars() android.Paths
348 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800349 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700350 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900351 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700352}
353
Jiyong Parkc678ad32018-04-10 13:07:10 +0900354type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800355 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
356 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900357}
358
Nan Zhangb2b33de2018-02-23 11:18:47 -0800359type SrcDependency interface {
360 CompiledSrcs() android.Paths
361 CompiledSrcJars() android.Paths
362}
363
364func (j *Module) CompiledSrcs() android.Paths {
365 return j.compiledJavaSrcs
366}
367
368func (j *Module) CompiledSrcJars() android.Paths {
369 return j.compiledSrcJars
370}
371
372var _ SrcDependency = (*Module)(nil)
373
Colin Cross89536d42017-07-07 14:35:50 -0700374func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
375 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
376 android.InitDefaultableModule(module)
377}
378
Colin Crossbe1da472017-07-07 15:59:46 -0700379type dependencyTag struct {
380 blueprint.BaseDependencyTag
381 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700382}
383
Colin Crossa4f08812018-10-02 22:03:40 -0700384type jniDependencyTag struct {
385 blueprint.BaseDependencyTag
386 target android.Target
387}
388
Colin Crossbe1da472017-07-07 15:59:46 -0700389var (
Colin Cross4b964c02018-10-15 16:18:06 -0700390 staticLibTag = dependencyTag{name: "staticlib"}
391 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800392 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700393 bootClasspathTag = dependencyTag{name: "bootclasspath"}
394 systemModulesTag = dependencyTag{name: "system modules"}
395 frameworkResTag = dependencyTag{name: "framework-res"}
396 frameworkApkTag = dependencyTag{name: "framework-apk"}
397 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800398 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700399 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
400 certificateTag = dependencyTag{name: "certificate"}
401 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700402)
Colin Cross2fe66872015-03-30 17:20:39 -0700403
Colin Crossfc3674a2017-09-18 17:41:52 -0700404type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700405 useModule, useFiles, useDefaultLibs, invalidVersion bool
406
Colin Cross86a60ae2018-05-29 14:44:55 -0700407 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700408 systemModules string
409
Colin Crossa97c5d32018-03-28 14:58:31 -0700410 frameworkResModule string
411
Colin Cross86a60ae2018-05-29 14:44:55 -0700412 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700413 aidl android.Path
414}
415
Colin Crossa4f08812018-10-02 22:03:40 -0700416type jniLib struct {
417 name string
418 path android.Path
419 target android.Target
420}
421
Colin Cross3144dfc2018-01-03 15:06:47 -0800422func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
423 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
424}
425
426func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
427 return j.shouldInstrument(ctx) &&
428 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
429 ctx.Config().UnbundledBuild())
430}
431
Colin Cross83bb3162018-06-25 15:48:06 -0700432func (j *Module) sdkVersion() string {
433 return String(j.deviceProperties.Sdk_version)
434}
435
436func (j *Module) minSdkVersion() string {
437 if j.deviceProperties.Min_sdk_version != nil {
438 return *j.deviceProperties.Min_sdk_version
439 }
440 return j.sdkVersion()
441}
442
Dan Willemsen419290a2018-10-31 15:28:47 -0700443func (j *Module) targetSdkVersion() string {
444 if j.deviceProperties.Target_sdk_version != nil {
445 return *j.deviceProperties.Target_sdk_version
446 }
447 return j.sdkVersion()
448}
449
Colin Crossbe1da472017-07-07 15:59:46 -0700450func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700451 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700452 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700453 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700454 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700455 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100456 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700457 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700458 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700459 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700460 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100461 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700462 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800463 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700464 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
465 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800466 }
Colin Crossbe1da472017-07-07 15:59:46 -0700467 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700468 } else if j.deviceProperties.System_modules == nil {
469 ctx.PropertyErrorf("no_standard_libs",
470 "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 +0100471 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700472 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700473 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100474 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700475 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800476 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800477 if ctx.ModuleName() == "android_stubs_current" ||
478 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700479 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700480 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800481 }
Colin Cross2fe66872015-03-30 17:20:39 -0700482 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700483
Colin Cross42d48b72018-08-29 14:10:52 -0700484 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
485 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700486
Colin Crossbe9cdb82019-01-21 21:37:16 -0800487 ctx.AddFarVariationDependencies([]blueprint.Variation{
488 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
489 }, pluginTag, j.properties.Plugins...)
490
Colin Crossfe17f6f2019-03-28 19:30:56 -0700491 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700492 if j.hasSrcExt(".proto") {
493 protoDeps(ctx, &j.protoProperties)
494 }
Colin Cross93e85952017-08-15 13:34:18 -0700495
496 if j.hasSrcExt(".kt") {
497 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
498 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700499 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800500 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800501 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
502 }
Colin Cross93e85952017-08-15 13:34:18 -0700503 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800504
505 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700506 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800507 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700508}
509
510func hasSrcExt(srcs []string, ext string) bool {
511 for _, src := range srcs {
512 if filepath.Ext(src) == ext {
513 return true
514 }
515 }
516
517 return false
518}
519
Nan Zhang61eaedb2017-11-02 13:28:15 -0700520func shardPaths(paths android.Paths, shardSize int) []android.Paths {
521 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
522 for len(paths) > shardSize {
523 ret = append(ret, paths[0:shardSize])
524 paths = paths[shardSize:]
525 }
526 if len(paths) > 0 {
527 ret = append(ret, paths)
528 }
529 return ret
530}
531
Colin Cross6af17aa2017-09-20 12:59:05 -0700532func (j *Module) hasSrcExt(ext string) bool {
533 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700534}
535
Colin Cross46c9b8b2017-06-22 16:51:17 -0700536func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700537 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700538
Colin Crossebe1a512017-11-14 13:12:14 -0800539 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
540 aidlIncludes = append(aidlIncludes,
541 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
542 aidlIncludes = append(aidlIncludes,
543 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700544
Steven Moreland36b130f2019-02-05 17:02:55 -0800545 flags := []string{}
Steven Moreland667f6882018-07-26 12:55:08 -0700546
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700547 if aidlPreprocess.Valid() {
548 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700549 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700550 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700551 }
552
Colin Cross635c3b02016-05-18 15:37:25 -0700553 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800554 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700555 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800556 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700557 flags = append(flags, "-I"+src.String())
558 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700559
Martijn Coeneneab15642018-03-09 09:29:59 +0100560 if Bool(j.deviceProperties.Aidl.Generate_traces) {
561 flags = append(flags, "-t")
562 }
563
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100564 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
565 flags = append(flags, "--transaction_names")
566 }
567
Colin Crossf03c82b2015-04-13 13:53:40 -0700568 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700569}
570
Colin Cross32f676a2017-09-06 13:41:06 -0700571type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800572 classpath classpath
573 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700574 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800575 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700576 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700577 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700578 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700579 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800580 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700581 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700582 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700583 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700584 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800585 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800586
587 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700588}
Colin Cross2fe66872015-03-30 17:20:39 -0700589
Colin Cross54250902017-12-05 09:28:08 -0800590func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
591 for _, f := range dep.Srcs() {
592 if f.Ext() != ".jar" {
593 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
594 ctx.OtherModuleName(dep.(blueprint.Module)))
595 }
596 }
597}
598
Jiyong Park2d492942018-03-05 17:44:10 +0900599type linkType int
600
601const (
602 javaCore linkType = iota
603 javaSdk
604 javaSystem
605 javaPlatform
606)
607
Jiyong Park46f78fb2018-10-20 16:33:17 +0900608func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700609 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700610 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900611 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
612 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
613 name == "core-lambda-stubs":
614 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100615 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900616 return javaCore, false
617 case name == "android_system_stubs_current":
618 return javaSystem, true
619 case strings.HasPrefix(ver, "system_"):
620 return javaSystem, false
621 case name == "android_test_stubs_current":
622 return javaSystem, true
623 case strings.HasPrefix(ver, "test_"):
624 return javaPlatform, false
625 case name == "android_stubs_current":
626 return javaSdk, true
627 case ver == "current":
628 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700629 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900630 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700631 default:
632 if _, err := strconv.Atoi(ver); err != nil {
633 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
634 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900635 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900636 }
637}
638
Jiyong Park750e5572018-01-31 00:20:13 +0900639func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700640 if ctx.Host() {
641 return
642 }
643
Jiyong Park46f78fb2018-10-20 16:33:17 +0900644 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
645 if stubs {
646 return
647 }
648 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900649 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."
650
651 switch myLinkType {
652 case javaCore:
653 if otherLinkType != javaCore {
654 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 +0900655 ctx.OtherModuleName(to))
656 }
Jiyong Park2d492942018-03-05 17:44:10 +0900657 break
658 case javaSdk:
659 if otherLinkType != javaCore && otherLinkType != javaSdk {
660 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
661 ctx.OtherModuleName(to))
662 }
663 break
664 case javaSystem:
665 if otherLinkType == javaPlatform {
666 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
667 ctx.OtherModuleName(to))
668 }
669 break
670 case javaPlatform:
671 // no restriction on link-type
672 break
Jiyong Park750e5572018-01-31 00:20:13 +0900673 }
674}
675
Colin Cross32f676a2017-09-06 13:41:06 -0700676func (j *Module) collectDeps(ctx android.ModuleContext) deps {
677 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700678
Colin Cross300f0382018-03-06 13:11:51 -0800679 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700680 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800681 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700682 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800683 } else if sdkDep.useFiles {
684 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700685 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800686 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
687 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700688 }
689
Colin Crossd11fcda2017-10-23 17:59:01 -0700690 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700691 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700692 tag := ctx.OtherModuleDependencyTag(module)
693
Colin Crossa4f08812018-10-02 22:03:40 -0700694 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700695 // Handled by AndroidApp.collectAppDeps
696 return
697 }
698 if tag == certificateTag {
699 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700700 return
701 }
702
Jiyong Park750e5572018-01-31 00:20:13 +0900703 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700704 switch tag {
705 case bootClasspathTag, libTag, staticLibTag:
706 checkLinkType(ctx, j, to, tag.(dependencyTag))
707 }
Jiyong Park750e5572018-01-31 00:20:13 +0900708 }
Colin Cross54250902017-12-05 09:28:08 -0800709 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800710 case SdkLibraryDependency:
711 switch tag {
712 case libTag:
713 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
714 // names of sdk libs that are directly depended are exported
715 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
716 default:
717 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
718 }
Colin Cross54250902017-12-05 09:28:08 -0800719 case Dependency:
720 switch tag {
721 case bootClasspathTag:
722 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700723 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800724 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900725 // sdk lib names from dependencies are re-exported
726 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800727 case staticLibTag:
728 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
729 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
730 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700731 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900732 // sdk lib names from dependencies are re-exported
733 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800734 case pluginTag:
735 if plugin, ok := dep.(*Plugin); ok {
736 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
737 if plugin.pluginProperties.Processor_class != nil {
738 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
739 }
740 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
741 } else {
742 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
743 }
Colin Cross54250902017-12-05 09:28:08 -0800744 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100745 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800746 // framework.jar has a one-off dependency on the R.java and Manifest.java files
747 // generated by framework-res.apk
748 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
749 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800750 case frameworkApkTag:
751 if ctx.ModuleName() == "android_stubs_current" ||
752 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700753 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800754 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
755 // resource files out of there for aapt.
756 //
757 // Normally the package rule runs aapt, which includes the resource,
758 // but we're not running that in our package rule so just copy in the
759 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700760 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800761 }
Colin Cross54250902017-12-05 09:28:08 -0800762 case kotlinStdlibTag:
763 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800764 case kotlinAnnotationsTag:
765 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800766 }
767
768 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
769 case android.SourceFileProducer:
770 switch tag {
771 case libTag:
772 checkProducesJars(ctx, dep)
773 deps.classpath = append(deps.classpath, dep.Srcs()...)
774 case staticLibTag:
775 checkProducesJars(ctx, dep)
776 deps.classpath = append(deps.classpath, dep.Srcs()...)
777 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
778 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800779 }
780 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700781 switch tag {
782 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700783 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700784 case systemModulesTag:
785 if deps.systemModules != nil {
786 panic("Found two system module dependencies")
787 }
788 sm := module.(*SystemModules)
789 if sm.outputFile == nil {
790 panic("Missing directory for system module dependency")
791 }
792 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700793 default:
794 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700795 }
Colin Crossec7a0422017-07-07 14:47:12 -0700796 }
Colin Cross2fe66872015-03-30 17:20:39 -0700797 })
798
Jiyong Park1be96912018-05-28 18:02:19 +0900799 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
800
Colin Cross32f676a2017-09-06 13:41:06 -0700801 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700802}
803
Colin Cross83bb3162018-06-25 15:48:06 -0700804func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700805 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800806 v := sdkContext.sdkVersion()
807 // For PDK builds, use the latest SDK version instead of "current"
808 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
809 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
810 latestSdkVersion := 0
811 if len(sdkVersions) > 0 {
812 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
813 }
814 v = strconv.Itoa(latestSdkVersion)
815 }
816
817 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700818 if err != nil {
819 ctx.PropertyErrorf("sdk_version", "%s", err)
820 }
Nan Zhang357466b2018-04-17 17:38:36 -0700821 if javaVersion != "" {
822 ret = javaVersion
823 } else if ctx.Device() && sdk <= 23 {
824 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900825 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700826 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700827 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700828 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
829 ret = "1.8"
830 } else {
831 ret = "1.9"
832 }
833
834 return ret
835}
836
Nan Zhanged19fc32017-10-19 13:06:22 -0700837func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700838
Colin Crossf03c82b2015-04-13 13:53:40 -0700839 var flags javaBuilderFlags
840
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100841 // javaVersion flag.
842 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
843
Nan Zhanged19fc32017-10-19 13:06:22 -0700844 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700845 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100846 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700847 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700848 }
Colin Cross6510f912017-11-29 00:27:14 -0800849 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700850 // Override the -g flag passed globally to remove local variable debug info to reduce
851 // disk and memory usage.
852 javacFlags = append(javacFlags, "-g:source,lines")
853 }
Colin Cross64162712017-08-08 13:17:59 -0700854
Colin Cross66548102018-06-19 22:47:35 -0700855 if ctx.Config().RunErrorProne() {
856 if config.ErrorProneClasspath == nil {
857 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
858 }
859
860 errorProneFlags := []string{
861 "-Xplugin:ErrorProne",
862 "${config.ErrorProneChecks}",
863 }
864 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
865
866 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
867 "'" + strings.Join(errorProneFlags, " ") + "'"
868 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800869 }
870
Nan Zhanged19fc32017-10-19 13:06:22 -0700871 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800872 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
873 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700874 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800875
Colin Crossbe9cdb82019-01-21 21:37:16 -0800876 flags.processor = strings.Join(deps.processorClasses, ",")
877
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100878 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800879 !Bool(j.properties.No_standard_libs) &&
880 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
881 // Give host-side tools a version of OpenJDK's standard libraries
882 // close to what they're targeting. As of Dec 2017, AOSP is only
883 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
884 //
885 // When building with OpenJDK 8, the following should have no
886 // effect since those jars would be available by default.
887 //
888 // When building with OpenJDK 9 but targeting a version < 1.8,
889 // putting them on the bootclasspath means that:
890 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
891 // b) references to existing APIs are not reinterpreted in an
892 // OpenJDK 9-specific way, eg. calls to subclasses of
893 // java.nio.Buffer as in http://b/70862583
894 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
895 flags.bootClasspath = append(flags.bootClasspath,
896 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
897 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800898 if Bool(j.properties.Use_tools_jar) {
899 flags.bootClasspath = append(flags.bootClasspath,
900 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
901 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800902 }
903
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100904 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800905 // Manually specify build directory in case it is not under the repo root.
906 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
907 // just adding a symlink under the root doesn't help.)
908 patchPaths := ".:" + ctx.Config().BuildDir()
909 classPath := flags.classpath.FormJavaClassPath("")
910 if classPath != "" {
911 patchPaths += ":" + classPath
912 }
913 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700914 }
915
Nan Zhanged19fc32017-10-19 13:06:22 -0700916 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700917 if deps.systemModules != nil {
918 flags.systemModules = append(flags.systemModules, deps.systemModules)
919 }
920
Nan Zhanged19fc32017-10-19 13:06:22 -0700921 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700922 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700923 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700924 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700925 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
926 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700927 }
928
Colin Cross81440082018-08-15 20:21:55 -0700929 if len(javacFlags) > 0 {
930 // optimization.
931 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
932 flags.javacFlags = "$javacFlags"
933 }
934
Nan Zhanged19fc32017-10-19 13:06:22 -0700935 return flags
936}
Colin Crossc0b06f12015-04-08 13:03:43 -0700937
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800938func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700939
Colin Crossebe1a512017-11-14 13:12:14 -0800940 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700941
942 deps := j.collectDeps(ctx)
943 flags := j.collectBuilderFlags(ctx, deps)
944
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100945 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700946 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
947 }
Colin Cross8a497952019-03-05 22:25:09 -0800948 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700949 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800950 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700951 }
952
Colin Crossaf050172017-11-15 23:01:59 -0800953 srcFiles = j.genSources(ctx, srcFiles, flags)
954
955 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700956 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800957 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700958
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700959 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
960 // that IDEInfo struct will use
961 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
962
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800963 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800964 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800965 }
966
Colin Cross1ee23172017-10-18 14:44:18 -0700967 jarName := ctx.ModuleName() + ".jar"
968
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000969 javaSrcFiles := srcFiles.FilterByExt(".java")
970 var uniqueSrcFiles android.Paths
971 set := make(map[string]bool)
972 for _, v := range javaSrcFiles {
973 if _, found := set[v.String()]; !found {
974 set[v.String()] = true
975 uniqueSrcFiles = append(uniqueSrcFiles, v)
976 }
977 }
978
Colin Cross55f63ea2018-08-27 12:37:09 -0700979 var kotlinJars android.Paths
980
Colin Cross93e85952017-08-15 13:34:18 -0700981 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200982 // user defined kotlin flags.
983 kotlincFlags := j.properties.Kotlincflags
984 CheckKotlincFlags(ctx, kotlincFlags)
985
Colin Cross93e85952017-08-15 13:34:18 -0700986 // If there are kotlin files, compile them first but pass all the kotlin and java files
987 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
988 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200989 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700990 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200991 kotlincFlags = append(kotlincFlags, "-no-jdk")
992 }
993 if len(kotlincFlags) > 0 {
994 // optimization.
995 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
996 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700997 }
998
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000999 var kotlinSrcFiles android.Paths
1000 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1001 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1002
Colin Crossafbb1732019-01-17 15:42:52 -08001003 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1004 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1005
1006 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1007 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1008
1009 if len(flags.processorPath) > 0 {
1010 // Use kapt for annotation processing
1011 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1012 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1013 srcJars = append(srcJars, kaptSrcJar)
1014 // Disable annotation processing in javac, it's already been handled by kapt
1015 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001016 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001017 }
Colin Cross93e85952017-08-15 13:34:18 -07001018
Colin Cross1ee23172017-10-18 14:44:18 -07001019 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001020 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001021 if ctx.Failed() {
1022 return
1023 }
1024
1025 // Make javac rule depend on the kotlinc rule
1026 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001027
Colin Cross93e85952017-08-15 13:34:18 -07001028 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001029 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001030 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001031 }
1032
Colin Cross55f63ea2018-08-27 12:37:09 -07001033 jars := append(android.Paths(nil), kotlinJars...)
1034
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001035 // Store the list of .java files that was passed to javac
1036 j.compiledJavaSrcs = uniqueSrcFiles
1037 j.compiledSrcJars = srcJars
1038
Nan Zhang61eaedb2017-11-02 13:28:15 -07001039 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001040 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001041 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1042 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001043 // Formerly, there was a check here that prevented annotation processors
1044 // from being used when sharding was enabled, as some annotation processors
1045 // do not function correctly in sharded environments. It was removed to
1046 // allow for the use of annotation processors that do function correctly
1047 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001048 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001049 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001050 if ctx.Failed() {
1051 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001052 }
1053 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001054 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001055 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001056 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001057 // If error-prone is enabled, add an additional rule to compile the java files into
1058 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001059 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001060 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1061 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001062 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001063 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001064 extraJarDeps = append(extraJarDeps, errorprone)
1065 }
1066
Nan Zhang61eaedb2017-11-02 13:28:15 -07001067 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001068 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001069 shardSize := int(*(j.properties.Javac_shard_size))
1070 var shardSrcs []android.Paths
1071 if len(uniqueSrcFiles) > 0 {
1072 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1073 for idx, shardSrc := range shardSrcs {
1074 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1075 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1076 jars = append(jars, classes)
1077 }
1078 }
1079 if len(srcJars) > 0 {
1080 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1081 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1082 jars = append(jars, classes)
1083 }
1084 } else {
1085 classes := android.PathForModuleOut(ctx, "javac", jarName)
1086 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1087 jars = append(jars, classes)
1088 }
Colin Crossd6891432017-09-27 17:39:56 -07001089 if ctx.Failed() {
1090 return
1091 }
Colin Cross2fe66872015-03-30 17:20:39 -07001092 }
1093
Colin Crosscedd4762018-09-13 11:26:19 -07001094 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1095 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001096 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1097
1098 var resArgs []string
1099 var resDeps android.Paths
1100
1101 resArgs = append(resArgs, dirArgs...)
1102 resDeps = append(resDeps, dirDeps...)
1103
1104 resArgs = append(resArgs, fileArgs...)
1105 resDeps = append(resDeps, fileDeps...)
1106
Colin Crossff3ae9d2018-04-10 16:15:18 -07001107 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001108 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001109 resArgs = append(resArgs, srcArgs...)
1110 resDeps = append(resDeps, srcDeps...)
1111 }
Colin Cross40a36712017-09-27 17:41:35 -07001112
1113 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001114 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001115 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001116 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001117 if ctx.Failed() {
1118 return
1119 }
1120 }
1121
Colin Cross331a1212018-08-15 20:40:52 -07001122 if len(deps.staticResourceJars) > 0 {
1123 var jars android.Paths
1124 if j.resourceJar != nil {
1125 jars = append(jars, j.resourceJar)
1126 }
1127 jars = append(jars, deps.staticResourceJars...)
1128
1129 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1130 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1131 false, nil, nil)
1132 j.resourceJar = combinedJar
1133 }
1134
Colin Cross32f676a2017-09-06 13:41:06 -07001135 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001136
Colin Cross094054a2018-10-17 15:10:48 -07001137 manifest := j.overrideManifest
1138 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001139 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001140 }
Colin Cross635acc92017-09-12 22:50:46 -07001141
Colin Cross8a497952019-03-05 22:25:09 -08001142 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001143 if len(services) > 0 {
1144 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1145 var zipargs []string
1146 for _, file := range services {
1147 serviceFile := file.String()
1148 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1149 }
1150 ctx.Build(pctx, android.BuildParams{
1151 Rule: zip,
1152 Output: servicesJar,
1153 Implicits: services,
1154 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001155 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001156 },
1157 })
1158 jars = append(jars, servicesJar)
1159 }
1160
Colin Cross0a6e0072017-08-30 14:24:55 -07001161 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001162 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001163 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001164
1165 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001166 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1167 // Optimization: skip the combine step if there is nothing to do
1168 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1169 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1170 // any if len(jars) == 1.
1171 outputFile = moduleOutPath
1172 } else {
1173 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1174 ctx.Build(pctx, android.BuildParams{
1175 Rule: android.Cp,
1176 Input: jars[0],
1177 Output: combinedJar,
1178 })
1179 outputFile = combinedJar
1180 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001181 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001182 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001183 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001184 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001185 outputFile = combinedJar
1186 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001187
Colin Cross331a1212018-08-15 20:40:52 -07001188 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001189 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001190 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001191 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001192 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001193 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001194
1195 // jarjar resource jar if necessary
1196 if j.resourceJar != nil {
1197 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001198 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001199 j.resourceJar = resourceJarJarFile
1200 }
1201
Colin Cross0a6e0072017-08-30 14:24:55 -07001202 if ctx.Failed() {
1203 return
1204 }
1205 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001206
1207 // Check package restrictions if necessary.
1208 if len(j.properties.Permitted_packages) > 0 {
1209 // Check packages and copy to package-checked file.
1210 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1211 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1212 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1213
1214 if ctx.Failed() {
1215 return
1216 }
1217 }
1218
Nan Zhanged19fc32017-10-19 13:06:22 -07001219 j.implementationJarFile = outputFile
1220 if j.headerJarFile == nil {
1221 j.headerJarFile = j.implementationJarFile
1222 }
Colin Cross2fe66872015-03-30 17:20:39 -07001223
Colin Cross6510f912017-11-29 00:27:14 -08001224 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001225 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1226 j.properties.Instrument = true
1227 }
1228 }
1229
Colin Cross3144dfc2018-01-03 15:06:47 -08001230 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001231 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1232 }
1233
Colin Cross331a1212018-08-15 20:40:52 -07001234 // merge implementation jar with resources if necessary
1235 implementationAndResourcesJar := outputFile
1236 if j.resourceJar != nil {
1237 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1238 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1239 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1240 false, nil, nil)
1241 implementationAndResourcesJar = combinedJar
1242 }
1243
1244 j.implementationAndResourcesJar = implementationAndResourcesJar
1245
Colin Cross9ae1b922018-06-26 17:59:05 -07001246 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001247 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001248 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001249 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001250 if ctx.Failed() {
1251 return
1252 }
Colin Cross331a1212018-08-15 20:40:52 -07001253
Colin Cross8faf8fc2019-01-16 15:15:52 -08001254 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001255 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1256 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001257
Colin Cross331a1212018-08-15 20:40:52 -07001258 // merge dex jar with resources if necessary
1259 if j.resourceJar != nil {
1260 jars := android.Paths{dexOutputFile, j.resourceJar}
1261 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1262 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1263 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001264 if j.deviceProperties.UncompressDex {
1265 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1266 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1267 dexOutputFile = combinedAlignedJar
1268 } else {
1269 dexOutputFile = combinedJar
1270 }
Colin Cross331a1212018-08-15 20:40:52 -07001271 }
1272
1273 j.dexJarFile = dexOutputFile
1274
Colin Cross8faf8fc2019-01-16 15:15:52 -08001275 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001276 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1277
1278 j.maybeStrippedDexJarFile = dexOutputFile
1279
Colin Cross3063b782018-08-15 11:19:12 -07001280 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001281
1282 if ctx.Failed() {
1283 return
1284 }
Colin Cross331a1212018-08-15 20:40:52 -07001285 } else {
1286 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001287 }
Colin Cross331a1212018-08-15 20:40:52 -07001288
Colin Crossb7a63242015-04-16 14:09:14 -07001289 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001290
1291 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1292 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001293}
1294
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001295// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1296// since some of these flags may be used internally.
1297func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1298 for _, flag := range flags {
1299 flag = strings.TrimSpace(flag)
1300
1301 if !strings.HasPrefix(flag, "-") {
1302 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1303 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1304 ctx.PropertyErrorf("kotlincflags",
1305 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1306 } else if inList(flag, config.KotlincIllegalFlags) {
1307 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1308 } else if flag == "-include-runtime" {
1309 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1310 } else {
1311 args := strings.Split(flag, " ")
1312 if args[0] == "-kotlin-home" {
1313 ctx.PropertyErrorf("kotlincflags",
1314 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1315 }
1316 }
1317 }
1318}
1319
Colin Cross8eadbf02017-10-24 17:46:00 -07001320func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001321 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001322
1323 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001324 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001325 // Compile java sources into turbine.jar.
1326 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1327 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1328 if ctx.Failed() {
1329 return nil
1330 }
1331 jars = append(jars, turbineJar)
1332 }
1333
Colin Cross55f63ea2018-08-27 12:37:09 -07001334 jars = append(jars, extraJars...)
1335
Nan Zhanged19fc32017-10-19 13:06:22 -07001336 // Combine any static header libraries into classes-header.jar. If there is only
1337 // one input jar this step will be skipped.
1338 var headerJar android.Path
1339 jars = append(jars, deps.staticHeaderJars...)
1340
Colin Cross5c6ecc12017-10-23 18:12:27 -07001341 // we cannot skip the combine step for now if there is only one jar
1342 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1343 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001344 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1345 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001346 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001347
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001348 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001349 // Transform classes.jar into classes-jarjar.jar
1350 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001351 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001352 headerJar = jarjarFile
1353 if ctx.Failed() {
1354 return nil
1355 }
1356 }
1357
1358 return headerJar
1359}
1360
Colin Crosscb933592017-11-22 13:49:43 -08001361func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001362 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001363
Colin Cross7a3139e2017-12-19 13:57:50 -08001364 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001365
Colin Cross84c38822018-01-03 15:59:46 -08001366 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001367 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1368
1369 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1370
1371 j.jacocoReportClassesFile = jacocoReportClassesFile
1372
1373 return instrumentedJar
1374}
1375
albaltai36ff7dc2018-12-25 14:35:23 +08001376var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001377
Nan Zhanged19fc32017-10-19 13:06:22 -07001378func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001379 if j.headerJarFile == nil {
1380 return nil
1381 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001382 return android.Paths{j.headerJarFile}
1383}
1384
1385func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001386 if j.implementationJarFile == nil {
1387 return nil
1388 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001389 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001390}
1391
Colin Crossf24a22a2019-01-31 14:12:44 -08001392func (j *Module) DexJar() android.Path {
1393 return j.dexJarFile
1394}
1395
Colin Cross331a1212018-08-15 20:40:52 -07001396func (j *Module) ResourceJars() android.Paths {
1397 if j.resourceJar == nil {
1398 return nil
1399 }
1400 return android.Paths{j.resourceJar}
1401}
1402
1403func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001404 if j.implementationAndResourcesJar == nil {
1405 return nil
1406 }
Colin Cross331a1212018-08-15 20:40:52 -07001407 return android.Paths{j.implementationAndResourcesJar}
1408}
1409
Colin Cross46c9b8b2017-06-22 16:51:17 -07001410func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001411 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001412 return j.exportAidlIncludeDirs
1413}
1414
Jiyong Park1be96912018-05-28 18:02:19 +09001415func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001416 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001417 return j.exportedSdkLibs
1418}
1419
Colin Cross46c9b8b2017-06-22 16:51:17 -07001420var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001421
Colin Cross46c9b8b2017-06-22 16:51:17 -07001422func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001423 return j.logtagsSrcs
1424}
1425
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001426// Collect information for opening IDE project files in java/jdeps.go.
1427func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1428 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1429 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1430 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001431 if j.expandJarjarRules != nil {
1432 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001433 }
1434}
1435
1436func (j *Module) CompilerDeps() []string {
1437 jdeps := []string{}
1438 jdeps = append(jdeps, j.properties.Libs...)
1439 jdeps = append(jdeps, j.properties.Static_libs...)
1440 return jdeps
1441}
1442
Colin Cross2fe66872015-03-30 17:20:39 -07001443//
1444// Java libraries (.jar file)
1445//
1446
Colin Crossf506d872017-07-19 15:53:04 -07001447type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001448 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001449}
1450
Colin Cross42be7612019-02-21 18:12:14 -08001451func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001452 // Store uncompressed (and do not strip) dex files from boot class path jars.
1453 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1454 return true
1455 }
1456
1457 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001458 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001459 return true
1460 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001461 if ctx.Config().UncompressPrivAppDex() &&
1462 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1463 return true
1464 }
1465
Colin Cross2fc72f62018-12-21 12:59:54 -08001466 return false
1467}
1468
Colin Crossf506d872017-07-19 15:53:04 -07001469func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001470 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1471 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001472 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001473 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001474 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001475 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001476
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001477 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001478 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1479 ctx.ModuleName()+".jar", j.outputFile)
1480 }
Colin Crossb7a63242015-04-16 14:09:14 -07001481}
1482
Colin Crossf506d872017-07-19 15:53:04 -07001483func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001484 j.deps(ctx)
1485}
1486
Colin Cross1b16b0e2019-02-12 14:41:32 -08001487// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1488//
1489// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1490// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1491// as a `static_libs` dependency of another module.
1492//
1493// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1494// a device.
1495//
1496// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1497// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001498func LibraryFactory() android.Module {
1499 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001500
Colin Cross9ae1b922018-06-26 17:59:05 -07001501 module.AddProperties(
1502 &module.Module.properties,
1503 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001504 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001505 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001506
Colin Cross9ae1b922018-06-26 17:59:05 -07001507 InitJavaModule(module, android.HostAndDeviceSupported)
1508 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001509}
1510
Colin Cross1b16b0e2019-02-12 14:41:32 -08001511// java_library_static is an obsolete alias for java_library.
1512func LibraryStaticFactory() android.Module {
1513 return LibraryFactory()
1514}
1515
1516// java_library_host builds and links sources into a `.jar` file for the host.
1517//
1518// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1519// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001520func LibraryHostFactory() android.Module {
1521 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001522
Colin Cross6af17aa2017-09-20 12:59:05 -07001523 module.AddProperties(
1524 &module.Module.properties,
1525 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001526
Colin Cross9ae1b922018-06-26 17:59:05 -07001527 module.Module.properties.Installable = proptools.BoolPtr(true)
1528
Colin Cross89536d42017-07-07 14:35:50 -07001529 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001530 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001531}
1532
1533//
Colin Crossb628ea52018-08-14 16:42:33 -07001534// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001535//
1536
1537type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001538 // list of compatibility suites (for example "cts", "vts") that the module should be
1539 // installed into.
1540 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001541
1542 // the name of the test configuration (for example "AndroidTest.xml") that should be
1543 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001544 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001545
Jack He33338892018-09-19 02:21:28 -07001546 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1547 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001548 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001549
Colin Crossd96ca352018-08-10 16:06:24 -07001550 // list of files or filegroup modules that provide data that should be installed alongside
1551 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001552 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001553}
1554
Paul Duffin42df1442019-03-20 12:45:53 +00001555type testHelperLibraryProperties struct {
1556 // list of compatibility suites (for example "cts", "vts") that the module should be
1557 // installed into.
1558 Test_suites []string `android:"arch_variant"`
1559}
1560
Colin Cross05638fc2018-04-09 18:40:24 -07001561type Test struct {
1562 Library
1563
1564 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001565
1566 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001567 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001568}
1569
Paul Duffin42df1442019-03-20 12:45:53 +00001570type TestHelperLibrary struct {
1571 Library
1572
1573 testHelperLibraryProperties testHelperLibraryProperties
1574}
1575
Colin Cross303e21f2018-08-07 16:49:25 -07001576func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001577 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 -08001578 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001579
1580 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001581}
1582
Paul Duffin42df1442019-03-20 12:45:53 +00001583func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1584 j.Library.GenerateAndroidBuildActions(ctx)
1585}
1586
Colin Cross1b16b0e2019-02-12 14:41:32 -08001587// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1588// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1589//
1590// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1591// compiled against the device bootclasspath.
1592//
1593// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1594// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001595func TestFactory() android.Module {
1596 module := &Test{}
1597
1598 module.AddProperties(
1599 &module.Module.properties,
1600 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001601 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001602 &module.Module.protoProperties,
1603 &module.testProperties)
1604
Colin Cross9ae1b922018-06-26 17:59:05 -07001605 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001606 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001607
Colin Cross05638fc2018-04-09 18:40:24 -07001608 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001609 return module
1610}
1611
Paul Duffin42df1442019-03-20 12:45:53 +00001612// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1613func TestHelperLibraryFactory() android.Module {
1614 module := &TestHelperLibrary{}
1615
1616 module.AddProperties(
1617 &module.Module.properties,
1618 &module.Module.deviceProperties,
1619 &module.Module.dexpreoptProperties,
1620 &module.Module.protoProperties,
1621 &module.testHelperLibraryProperties)
1622
1623 InitJavaModule(module, android.HostAndDeviceSupported)
1624 return module
1625}
1626
Colin Cross1b16b0e2019-02-12 14:41:32 -08001627// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1628// allow running the test with `atest` or a `TEST_MAPPING` file.
1629//
1630// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1631// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001632func TestHostFactory() android.Module {
1633 module := &Test{}
1634
1635 module.AddProperties(
1636 &module.Module.properties,
1637 &module.Module.protoProperties,
1638 &module.testProperties)
1639
Colin Cross9ae1b922018-06-26 17:59:05 -07001640 module.Module.properties.Installable = proptools.BoolPtr(true)
1641
Colin Cross05638fc2018-04-09 18:40:24 -07001642 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001643 return module
1644}
1645
1646//
Colin Cross2fe66872015-03-30 17:20:39 -07001647// Java Binaries (.jar file plus wrapper script)
1648//
1649
Colin Crossf506d872017-07-19 15:53:04 -07001650type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001651 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001652 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001653
1654 // Name of the class containing main to be inserted into the manifest as Main-Class.
1655 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001656}
1657
Colin Crossf506d872017-07-19 15:53:04 -07001658type Binary struct {
1659 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001660
Colin Crossf506d872017-07-19 15:53:04 -07001661 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001662
Colin Cross6b4a32d2017-12-05 13:42:45 -08001663 isWrapperVariant bool
1664
Colin Crossc3315992017-12-08 19:12:36 -08001665 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001666 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001667}
1668
Alex Light24237172017-10-26 09:46:21 -07001669func (j *Binary) HostToolPath() android.OptionalPath {
1670 return android.OptionalPathForPath(j.binaryFile)
1671}
1672
Colin Crossf506d872017-07-19 15:53:04 -07001673func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001674 if ctx.Arch().ArchType == android.Common {
1675 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001676 if j.binaryProperties.Main_class != nil {
1677 if j.properties.Manifest != nil {
1678 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1679 }
1680 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1681 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1682 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1683 }
1684
Colin Cross6b4a32d2017-12-05 13:42:45 -08001685 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001686 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001687 // Handle the binary wrapper
1688 j.isWrapperVariant = true
1689
Colin Cross366938f2017-12-11 16:29:02 -08001690 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001691 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001692 } else {
1693 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1694 }
1695
1696 // Depend on the installed jar so that the wrapper doesn't get executed by
1697 // another build rule before the jar has been installed.
1698 jarFile := ctx.PrimaryModule().(*Binary).installFile
1699
1700 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1701 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001702 }
Colin Cross2fe66872015-03-30 17:20:39 -07001703}
1704
Colin Crossf506d872017-07-19 15:53:04 -07001705func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001706 if ctx.Arch().ArchType == android.Common {
1707 j.deps(ctx)
1708 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001709}
1710
Colin Cross1b16b0e2019-02-12 14:41:32 -08001711// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1712// as well.
1713//
1714// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1715// compiled against the device bootclasspath.
1716//
1717// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1718// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001719func BinaryFactory() android.Module {
1720 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001721
Colin Cross36242852017-06-23 15:06:31 -07001722 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001723 &module.Module.properties,
1724 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001725 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001726 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001727 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001728
Colin Cross9ae1b922018-06-26 17:59:05 -07001729 module.Module.properties.Installable = proptools.BoolPtr(true)
1730
Colin Cross6b4a32d2017-12-05 13:42:45 -08001731 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1732 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001733 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001734}
1735
Colin Cross1b16b0e2019-02-12 14:41:32 -08001736// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1737//
1738// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1739// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001740func BinaryHostFactory() android.Module {
1741 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001742
Colin Cross36242852017-06-23 15:06:31 -07001743 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001744 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001745 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001746 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001747
Colin Cross9ae1b922018-06-26 17:59:05 -07001748 module.Module.properties.Installable = proptools.BoolPtr(true)
1749
Colin Cross6b4a32d2017-12-05 13:42:45 -08001750 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1751 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001752 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001753}
1754
1755//
1756// Java prebuilts
1757//
1758
Colin Cross74d73e22017-08-02 11:05:49 -07001759type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001760 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001761
Nan Zhangea568a42017-11-08 21:20:04 -08001762 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001763
1764 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001765
1766 // List of shared java libs that this module has dependencies to
1767 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001768
1769 // List of files to remove from the jar file(s)
1770 Exclude_files []string
1771
1772 // List of directories to remove from the jar file(s)
1773 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001774
1775 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001776 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001777}
1778
1779type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001780 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001781 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001782 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001783
Colin Cross74d73e22017-08-02 11:05:49 -07001784 properties ImportProperties
1785
Colin Cross0a6e0072017-08-30 14:24:55 -07001786 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001787 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001788}
1789
Colin Cross83bb3162018-06-25 15:48:06 -07001790func (j *Import) sdkVersion() string {
1791 return String(j.properties.Sdk_version)
1792}
1793
1794func (j *Import) minSdkVersion() string {
1795 return j.sdkVersion()
1796}
1797
Colin Cross74d73e22017-08-02 11:05:49 -07001798func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001799 return &j.prebuilt
1800}
1801
Colin Cross74d73e22017-08-02 11:05:49 -07001802func (j *Import) PrebuiltSrcs() []string {
1803 return j.properties.Jars
1804}
1805
1806func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001807 return j.prebuilt.Name(j.ModuleBase.Name())
1808}
1809
Colin Cross74d73e22017-08-02 11:05:49 -07001810func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001811 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001812}
1813
Colin Cross74d73e22017-08-02 11:05:49 -07001814func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001815 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001816
Nan Zhang4c819fb2018-08-27 18:31:46 -07001817 jarName := ctx.ModuleName() + ".jar"
1818 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001819 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1820 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001821 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001822 inputFile := outputFile
1823 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1824 TransformJetifier(ctx, outputFile, inputFile)
1825 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001826 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001827
1828 ctx.VisitDirectDeps(func(module android.Module) {
1829 otherName := ctx.OtherModuleName(module)
1830 tag := ctx.OtherModuleDependencyTag(module)
1831
1832 switch dep := module.(type) {
1833 case Dependency:
1834 switch tag {
1835 case libTag, staticLibTag:
1836 // sdk lib names from dependencies are re-exported
1837 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1838 }
1839 case SdkLibraryDependency:
1840 switch tag {
1841 case libTag:
1842 // names of sdk libs that are directly depended are exported
1843 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1844 }
1845 }
1846 })
1847
1848 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001849 if Bool(j.properties.Installable) {
1850 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1851 ctx.ModuleName()+".jar", outputFile)
1852 }
Colin Cross2fe66872015-03-30 17:20:39 -07001853}
1854
Colin Cross74d73e22017-08-02 11:05:49 -07001855var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001856
Nan Zhanged19fc32017-10-19 13:06:22 -07001857func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001858 if j.combinedClasspathFile == nil {
1859 return nil
1860 }
Colin Cross37f6d792018-07-12 12:28:41 -07001861 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001862}
1863
1864func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001865 if j.combinedClasspathFile == nil {
1866 return nil
1867 }
Colin Cross37f6d792018-07-12 12:28:41 -07001868 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001869}
1870
Colin Cross331a1212018-08-15 20:40:52 -07001871func (j *Import) ResourceJars() android.Paths {
1872 return nil
1873}
1874
1875func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001876 if j.combinedClasspathFile == nil {
1877 return nil
1878 }
Colin Cross331a1212018-08-15 20:40:52 -07001879 return android.Paths{j.combinedClasspathFile}
1880}
1881
Colin Crossf24a22a2019-01-31 14:12:44 -08001882func (j *Import) DexJar() android.Path {
1883 return nil
1884}
1885
Colin Cross74d73e22017-08-02 11:05:49 -07001886func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001887 return nil
1888}
1889
Jiyong Park1be96912018-05-28 18:02:19 +09001890func (j *Import) ExportedSdkLibs() []string {
1891 return j.exportedSdkLibs
1892}
1893
albaltai36ff7dc2018-12-25 14:35:23 +08001894// Add compile time check for interface implementation
1895var _ android.IDEInfo = (*Import)(nil)
1896var _ android.IDECustomizedModuleName = (*Import)(nil)
1897
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001898// Collect information for opening IDE project files in java/jdeps.go.
1899const (
1900 removedPrefix = "prebuilt_"
1901)
1902
1903func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1904 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1905}
1906
1907func (j *Import) IDECustomizedModuleName() string {
1908 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1909 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1910 // solution to get the Import name.
1911 name := j.Name()
1912 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001913 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001914 }
1915 return name
1916}
1917
Colin Cross74d73e22017-08-02 11:05:49 -07001918var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001919
Colin Cross1b16b0e2019-02-12 14:41:32 -08001920// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1921//
1922// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1923// compiled against an Android classpath.
1924//
1925// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1926// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001927func ImportFactory() android.Module {
1928 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001929
Colin Cross74d73e22017-08-02 11:05:49 -07001930 module.AddProperties(&module.properties)
1931
1932 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001933 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001934 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001935}
1936
Colin Cross1b16b0e2019-02-12 14:41:32 -08001937// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1938// module.
1939//
1940// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1941// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001942func ImportFactoryHost() android.Module {
1943 module := &Import{}
1944
1945 module.AddProperties(&module.properties)
1946
1947 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001948 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001949 return module
1950}
1951
Colin Cross42be7612019-02-21 18:12:14 -08001952// dex_import module
1953
1954type DexImportProperties struct {
1955 Jars []string
1956}
1957
1958type DexImport struct {
1959 android.ModuleBase
1960 android.DefaultableModuleBase
1961 prebuilt android.Prebuilt
1962
1963 properties DexImportProperties
1964
1965 dexJarFile android.Path
1966 maybeStrippedDexJarFile android.Path
1967
1968 dexpreopter
1969}
1970
1971func (j *DexImport) Prebuilt() *android.Prebuilt {
1972 return &j.prebuilt
1973}
1974
1975func (j *DexImport) PrebuiltSrcs() []string {
1976 return j.properties.Jars
1977}
1978
1979func (j *DexImport) Name() string {
1980 return j.prebuilt.Name(j.ModuleBase.Name())
1981}
1982
1983func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1984 android.ExtractSourcesDeps(ctx, j.properties.Jars)
1985}
1986
1987func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1988 if len(j.properties.Jars) != 1 {
1989 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1990 }
1991
1992 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1993 j.dexpreopter.isInstallable = true
1994 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1995
1996 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1997 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1998
1999 if j.dexpreopter.uncompressedDex {
2000 rule := android.NewRuleBuilder()
2001
2002 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2003 rule.Temporary(temporary)
2004
2005 // use zip2zip to uncompress classes*.dex files
2006 rule.Command().
2007 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2008 FlagWithInput("-i ", inputJar).
2009 FlagWithOutput("-o ", temporary).
2010 FlagWithArg("-0 ", "'classes*.dex'")
2011
2012 // use zipalign to align uncompressed classes*.dex files
2013 rule.Command().
2014 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2015 Flag("-f").
2016 Text("4").
2017 Input(temporary).
2018 Output(dexOutputFile)
2019
2020 rule.DeleteTemporaryFiles()
2021
2022 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2023 } else {
2024 ctx.Build(pctx, android.BuildParams{
2025 Rule: android.Cp,
2026 Input: inputJar,
2027 Output: dexOutputFile,
2028 })
2029 }
2030
2031 j.dexJarFile = dexOutputFile
2032
2033 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2034
2035 j.maybeStrippedDexJarFile = dexOutputFile
2036
2037 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2038 ctx.ModuleName()+".jar", dexOutputFile)
2039}
2040
2041func (j *DexImport) DexJar() android.Path {
2042 return j.dexJarFile
2043}
2044
2045// dex_import imports a `.jar` file containing classes.dex files.
2046//
2047// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2048// to the device.
2049func DexImportFactory() android.Module {
2050 module := &DexImport{}
2051
2052 module.AddProperties(&module.properties)
2053
2054 android.InitPrebuiltModule(module, &module.properties.Jars)
2055 InitJavaModule(module, android.DeviceSupported)
2056 return module
2057}
2058
Colin Cross89536d42017-07-07 14:35:50 -07002059//
2060// Defaults
2061//
2062type Defaults struct {
2063 android.ModuleBase
2064 android.DefaultsModuleBase
2065}
2066
2067func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2068}
2069
Colin Cross1b16b0e2019-02-12 14:41:32 -08002070// java_defaults provides a set of properties that can be inherited by other java or android modules.
2071//
2072// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2073// property in the defaults module that exists in the depending module will be prepended to the depending module's
2074// value for that property.
2075//
2076// Example:
2077//
2078// java_defaults {
2079// name: "example_defaults",
2080// srcs: ["common/**/*.java"],
2081// javacflags: ["-Xlint:all"],
2082// aaptflags: ["--auto-add-overlay"],
2083// }
2084//
2085// java_library {
2086// name: "example",
2087// defaults: ["example_defaults"],
2088// srcs: ["example/**/*.java"],
2089// }
2090//
2091// is functionally identical to:
2092//
2093// java_library {
2094// name: "example",
2095// srcs: [
2096// "common/**/*.java",
2097// "example/**/*.java",
2098// ],
2099// javacflags: ["-Xlint:all"],
2100// }
Colin Cross89536d42017-07-07 14:35:50 -07002101func defaultsFactory() android.Module {
2102 return DefaultsFactory()
2103}
2104
2105func DefaultsFactory(props ...interface{}) android.Module {
2106 module := &Defaults{}
2107
2108 module.AddProperties(props...)
2109 module.AddProperties(
2110 &CompilerProperties{},
2111 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002112 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002113 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002114 &aaptProperties{},
2115 &androidLibraryProperties{},
2116 &appProperties{},
2117 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002118 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002119 &ImportProperties{},
2120 &AARImportProperties{},
2121 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002122 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002123 )
2124
2125 android.InitDefaultsModule(module)
2126
2127 return module
2128}
Nan Zhangea568a42017-11-08 21:20:04 -08002129
2130var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002131var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002132var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002133var inList = android.InList