blob: 1fd0a9e3117a90e98bfe8a8dc5bdc19126cd72dc [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)
44 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070045 android.RegisterModuleType("java_import", ImportFactory)
46 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080047 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
48 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080049 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070050
Colin Cross798bfce2016-10-12 14:28:16 -070051 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070052}
53
Colin Cross2fe66872015-03-30 17:20:39 -070054// TODO:
55// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070056// Renderscript
57// Post-jar passes:
58// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070059// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070060// DroidDoc
61// Findbugs
62
Colin Cross89536d42017-07-07 14:35:50 -070063type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070064 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
65 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080066 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070067
68 // list of source files that should not be used to build the Java module.
69 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080070 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
72 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070073 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070074
Colin Cross86a63ff2017-09-27 17:33:10 -070075 // list of directories that should be excluded from java_resource_dirs
76 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070077
Colin Cross0f37af02017-09-27 17:42:05 -070078 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080079 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070080
Colin Crosscedd4762018-09-13 11:26:19 -070081 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080082 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070083
Paul Duffin2fbbfb82019-02-13 12:49:33 +000084 // don't build against the default libraries (bootclasspath, ext, and framework for device
85 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070086 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070087
Paul Duffin2fbbfb82019-02-13 12:49:33 +000088 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070089 No_framework_libs *bool
90
Colin Cross7d5136f2015-05-11 13:39:40 -070091 // list of module-specific flags that will be used for javac compiles
92 Javacflags []string `android:"arch_variant"`
93
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020094 // list of module-specific flags that will be used for kotlinc compiles
95 Kotlincflags []string `android:"arch_variant"`
96
Colin Cross7d5136f2015-05-11 13:39:40 -070097 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070098 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070099
100 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700101 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700102
103 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800104 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700105
Colin Cross540eff82017-06-22 17:01:52 -0700106 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800107 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700108
109 // If not blank, set the java version passed to javac as -source and -target
110 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700111
Colin Cross9ae1b922018-06-26 17:59:05 -0700112 // If set to true, allow this module to be dexed and installed on devices. Has no
113 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700114 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700115
Colin Cross0f37af02017-09-27 17:42:05 -0700116 // If set to true, include sources used to compile the module in to the final jar
117 Include_srcs *bool
118
Colin Crossbe9cdb82019-01-21 21:37:16 -0800119 // List of modules to use as annotation processors
120 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700121
Nan Zhang61eaedb2017-11-02 13:28:15 -0700122 // The number of Java source entries each Javac instance can process
123 Javac_shard_size *int64
124
Nan Zhang5f8cb422018-02-06 10:34:32 -0800125 // Add host jdk tools.jar to bootclasspath
126 Use_tools_jar *bool
127
Colin Cross1369cdb2017-09-29 17:58:17 -0700128 Openjdk9 struct {
129 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800130 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700131
132 // List of javac flags that should only be used when passing -source 1.9
133 Javacflags []string
134 }
Colin Crosscb933592017-11-22 13:49:43 -0800135
Colin Cross81440082018-08-15 20:21:55 -0700136 // When compiling language level 9+ .java code in packages that are part of
137 // a system module, patch_module names the module that your sources and
138 // dependencies should be patched into. The Android runtime currently
139 // doesn't implement the JEP 261 module system so this option is only
140 // supported at compile time. It should only be needed to compile tests in
141 // packages that exist in libcore and which are inconvenient to move
142 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100143 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700144
Colin Crosscb933592017-11-22 13:49:43 -0800145 Jacoco struct {
146 // List of classes to include for instrumentation with jacoco to collect coverage
147 // information at runtime when building with coverage enabled. If unset defaults to all
148 // classes.
149 // Supports '*' as the last character of an entry in the list as a wildcard match.
150 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
151 // it matches classes in the package that have the class name as a prefix.
152 Include_filter []string
153
154 // List of classes to exclude from instrumentation with jacoco to collect coverage
155 // information at runtime when building with coverage enabled. Overrides classes selected
156 // by the include_filter property.
157 // Supports '*' as the last character of an entry in the list as a wildcard match.
158 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
159 // it matches classes in the package that have the class name as a prefix.
160 Exclude_filter []string
161 }
162
Andreas Gampef3e5b552018-01-22 21:27:21 -0800163 Errorprone struct {
164 // List of javac flags that should only be used when running errorprone.
165 Javacflags []string
166 }
167
Colin Cross0f2ee152017-12-14 15:22:43 -0800168 Proto struct {
169 // List of extra options that will be passed to the proto generator.
170 Output_params []string
171 }
172
Colin Crosscb933592017-11-22 13:49:43 -0800173 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800174
175 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800176 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700177}
178
Colin Cross89536d42017-07-07 14:35:50 -0700179type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700180 // list of module-specific flags that will be used for dex compiles
181 Dxflags []string `android:"arch_variant"`
182
Colin Cross83bb3162018-06-25 15:48:06 -0700183 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
184 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800185 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700186
Colin Cross83bb3162018-06-25 15:48:06 -0700187 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
188 // Defaults to sdk_version if not set.
189 Min_sdk_version *string
190
Dan Willemsen419290a2018-10-31 15:28:47 -0700191 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
192 // Defaults to sdk_version if not set.
193 Target_sdk_version *string
194
Colin Cross6af2e492018-05-22 11:12:33 -0700195 // if true, compile against the platform APIs instead of an SDK.
196 Platform_apis *bool
197
Colin Crossebe1a512017-11-14 13:12:14 -0800198 Aidl struct {
199 // Top level directories to pass to aidl tool
200 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700201
Colin Crossebe1a512017-11-14 13:12:14 -0800202 // Directories rooted at the Android.bp file to pass to aidl tool
203 Local_include_dirs []string
204
205 // directories that should be added as include directories for any aidl sources of modules
206 // that depend on this module, as well as to aidl for this module.
207 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100208
209 // whether to generate traces (for systrace) for this interface
210 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100211
212 // whether to generate Binder#GetTransaction name method.
213 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800214 }
Colin Cross92430102017-10-09 14:59:32 -0700215
216 // If true, export a copy of the module as a -hostdex module for host testing.
217 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700218
David Brazdil17ef5632018-06-27 10:27:45 +0100219 // If set to true, compile dex regardless of installable. Defaults to false.
220 Compile_dex *bool
221
Colin Cross66dbc0b2017-12-28 12:23:20 -0800222 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700223 // If false, disable all optimization. Defaults to true for android_app and android_test
224 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800225 Enabled *bool
226
227 // If true, optimize for size by removing unused code. Defaults to true for apps,
228 // false for libraries and tests.
229 Shrink *bool
230
231 // If true, optimize bytecode. Defaults to false.
232 Optimize *bool
233
234 // If true, obfuscate bytecode. Defaults to false.
235 Obfuscate *bool
236
237 // If true, do not use the flag files generated by aapt that automatically keep
238 // classes referenced by the app manifest. Defaults to false.
239 No_aapt_flags *bool
240
241 // Flags to pass to proguard.
242 Proguard_flags []string
243
244 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800245 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800246 }
247
Colin Cross1369cdb2017-09-29 17:58:17 -0700248 // When targeting 1.9, override the modules to use with --system
249 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700250
251 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800252 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700253}
254
Colin Cross46c9b8b2017-06-22 16:51:17 -0700255// Module contains the properties and members used by all java module types
256type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700257 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700258 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700259
Colin Cross89536d42017-07-07 14:35:50 -0700260 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700261 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700262 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700263
Colin Cross331a1212018-08-15 20:40:52 -0700264 // jar file containing header classes including static library dependencies, suitable for
265 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700266 headerJarFile android.Path
267
Colin Cross331a1212018-08-15 20:40:52 -0700268 // jar file containing implementation classes including static library dependencies but no
269 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700270 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700271
Colin Cross331a1212018-08-15 20:40:52 -0700272 // jar file containing only resources including from static library dependencies
273 resourceJar android.Path
274
275 // jar file containing implementation classes and resources including static library
276 // dependencies
277 implementationAndResourcesJar android.Path
278
279 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700280 dexJarFile android.Path
281
Colin Cross43f08db2018-11-12 10:13:39 -0800282 // output file that contains classes.dex if it should be in the output file
283 maybeStrippedDexJarFile android.Path
284
Colin Crosscb933592017-11-22 13:49:43 -0800285 // output file containing uninstrumented classes that will be instrumented by jacoco
286 jacocoReportClassesFile android.Path
287
Colin Cross66dbc0b2017-12-28 12:23:20 -0800288 // output file containing mapping of obfuscated names
289 proguardDictionary android.Path
290
Colin Cross331a1212018-08-15 20:40:52 -0700291 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700292 outputFile android.Path
293 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700294
Colin Cross635c3b02016-05-18 15:37:25 -0700295 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700296
Colin Cross635c3b02016-05-18 15:37:25 -0700297 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700298
Colin Cross2fe66872015-03-30 17:20:39 -0700299 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700300 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800301
302 // list of .java files and srcjars that was passed to javac
303 compiledJavaSrcs android.Paths
304 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800305
306 // list of extra progurad flag files
307 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900308
Colin Cross094054a2018-10-17 15:10:48 -0700309 // manifest file to use instead of properties.Manifest
310 overrideManifest android.OptionalPath
311
Jiyong Park1be96912018-05-28 18:02:19 +0900312 // list of SDK lib names that this java moudule is exporting
313 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700314
315 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
316 // filter out Exclude_srcs, will be used by android.IDEInfo struct
317 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800318
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800319 // expanded Jarjar_rules
320 expandJarjarRules android.Path
321
Colin Crossf24a22a2019-01-31 14:12:44 -0800322 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800323 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700324}
325
Colin Cross54250902017-12-05 09:28:08 -0800326func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700327 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800328}
329
Jiyong Park8fd61922018-11-08 02:50:25 +0900330func (j *Module) DexJarFile() android.Path {
331 return j.dexJarFile
332}
333
Colin Cross54250902017-12-05 09:28:08 -0800334var _ android.SourceFileProducer = (*Module)(nil)
335
Colin Crossf506d872017-07-19 15:53:04 -0700336type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700337 HeaderJars() android.Paths
338 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700339 ResourceJars() android.Paths
340 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800341 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700342 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900343 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700344}
345
Jiyong Parkc678ad32018-04-10 13:07:10 +0900346type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800347 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
348 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900349}
350
Nan Zhangb2b33de2018-02-23 11:18:47 -0800351type SrcDependency interface {
352 CompiledSrcs() android.Paths
353 CompiledSrcJars() android.Paths
354}
355
356func (j *Module) CompiledSrcs() android.Paths {
357 return j.compiledJavaSrcs
358}
359
360func (j *Module) CompiledSrcJars() android.Paths {
361 return j.compiledSrcJars
362}
363
364var _ SrcDependency = (*Module)(nil)
365
Colin Cross89536d42017-07-07 14:35:50 -0700366func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
367 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
368 android.InitDefaultableModule(module)
369}
370
Colin Crossbe1da472017-07-07 15:59:46 -0700371type dependencyTag struct {
372 blueprint.BaseDependencyTag
373 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700374}
375
Colin Crossa4f08812018-10-02 22:03:40 -0700376type jniDependencyTag struct {
377 blueprint.BaseDependencyTag
378 target android.Target
379}
380
Colin Crossbe1da472017-07-07 15:59:46 -0700381var (
Colin Cross4b964c02018-10-15 16:18:06 -0700382 staticLibTag = dependencyTag{name: "staticlib"}
383 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800384 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700385 bootClasspathTag = dependencyTag{name: "bootclasspath"}
386 systemModulesTag = dependencyTag{name: "system modules"}
387 frameworkResTag = dependencyTag{name: "framework-res"}
388 frameworkApkTag = dependencyTag{name: "framework-apk"}
389 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800390 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700391 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
392 certificateTag = dependencyTag{name: "certificate"}
393 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700394)
Colin Cross2fe66872015-03-30 17:20:39 -0700395
Colin Crossfc3674a2017-09-18 17:41:52 -0700396type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700397 useModule, useFiles, useDefaultLibs, invalidVersion bool
398
Colin Cross86a60ae2018-05-29 14:44:55 -0700399 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700400 systemModules string
401
Colin Crossa97c5d32018-03-28 14:58:31 -0700402 frameworkResModule string
403
Colin Cross86a60ae2018-05-29 14:44:55 -0700404 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700405 aidl android.Path
406}
407
Colin Crossa4f08812018-10-02 22:03:40 -0700408type jniLib struct {
409 name string
410 path android.Path
411 target android.Target
412}
413
Colin Cross3144dfc2018-01-03 15:06:47 -0800414func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
415 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
416}
417
418func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
419 return j.shouldInstrument(ctx) &&
420 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
421 ctx.Config().UnbundledBuild())
422}
423
Colin Cross83bb3162018-06-25 15:48:06 -0700424func (j *Module) sdkVersion() string {
425 return String(j.deviceProperties.Sdk_version)
426}
427
428func (j *Module) minSdkVersion() string {
429 if j.deviceProperties.Min_sdk_version != nil {
430 return *j.deviceProperties.Min_sdk_version
431 }
432 return j.sdkVersion()
433}
434
Dan Willemsen419290a2018-10-31 15:28:47 -0700435func (j *Module) targetSdkVersion() string {
436 if j.deviceProperties.Target_sdk_version != nil {
437 return *j.deviceProperties.Target_sdk_version
438 }
439 return j.sdkVersion()
440}
441
Colin Crossbe1da472017-07-07 15:59:46 -0700442func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700443 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700444 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700445 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700446 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700447 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100448 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700449 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700450 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700451 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700452 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100453 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700454 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800455 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700456 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
457 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800458 }
Colin Crossbe1da472017-07-07 15:59:46 -0700459 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700460 } else if j.deviceProperties.System_modules == nil {
461 ctx.PropertyErrorf("no_standard_libs",
462 "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 +0100463 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700464 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700465 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100466 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700467 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800468 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800469 if ctx.ModuleName() == "android_stubs_current" ||
470 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700471 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700472 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800473 }
Colin Cross2fe66872015-03-30 17:20:39 -0700474 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700475
Colin Cross42d48b72018-08-29 14:10:52 -0700476 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
477 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700478
Colin Crossbe9cdb82019-01-21 21:37:16 -0800479 ctx.AddFarVariationDependencies([]blueprint.Variation{
480 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
481 }, pluginTag, j.properties.Plugins...)
482
Colin Cross6af17aa2017-09-20 12:59:05 -0700483 if j.hasSrcExt(".proto") {
484 protoDeps(ctx, &j.protoProperties)
485 }
Colin Cross93e85952017-08-15 13:34:18 -0700486
487 if j.hasSrcExt(".kt") {
488 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
489 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700490 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800491 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800492 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
493 }
Colin Cross93e85952017-08-15 13:34:18 -0700494 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800495
496 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700497 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800498 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700499}
500
501func hasSrcExt(srcs []string, ext string) bool {
502 for _, src := range srcs {
503 if filepath.Ext(src) == ext {
504 return true
505 }
506 }
507
508 return false
509}
510
Nan Zhang61eaedb2017-11-02 13:28:15 -0700511func shardPaths(paths android.Paths, shardSize int) []android.Paths {
512 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
513 for len(paths) > shardSize {
514 ret = append(ret, paths[0:shardSize])
515 paths = paths[shardSize:]
516 }
517 if len(paths) > 0 {
518 ret = append(ret, paths)
519 }
520 return ret
521}
522
Colin Cross6af17aa2017-09-20 12:59:05 -0700523func (j *Module) hasSrcExt(ext string) bool {
524 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700525}
526
Colin Cross46c9b8b2017-06-22 16:51:17 -0700527func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700528 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700529
Colin Crossebe1a512017-11-14 13:12:14 -0800530 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
531 aidlIncludes = append(aidlIncludes,
532 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
533 aidlIncludes = append(aidlIncludes,
534 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700535
Steven Moreland36b130f2019-02-05 17:02:55 -0800536 flags := []string{}
Steven Moreland667f6882018-07-26 12:55:08 -0700537
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700538 if aidlPreprocess.Valid() {
539 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700540 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700541 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700542 }
543
Colin Cross635c3b02016-05-18 15:37:25 -0700544 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800545 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700546 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800547 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700548 flags = append(flags, "-I"+src.String())
549 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700550
Martijn Coeneneab15642018-03-09 09:29:59 +0100551 if Bool(j.deviceProperties.Aidl.Generate_traces) {
552 flags = append(flags, "-t")
553 }
554
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100555 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
556 flags = append(flags, "--transaction_names")
557 }
558
Colin Crossf03c82b2015-04-13 13:53:40 -0700559 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700560}
561
Colin Cross32f676a2017-09-06 13:41:06 -0700562type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800563 classpath classpath
564 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700565 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800566 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700567 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700568 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700569 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700570 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800571 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700572 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700573 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700574 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700575 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800576 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800577
578 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700579}
Colin Cross2fe66872015-03-30 17:20:39 -0700580
Colin Cross54250902017-12-05 09:28:08 -0800581func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
582 for _, f := range dep.Srcs() {
583 if f.Ext() != ".jar" {
584 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
585 ctx.OtherModuleName(dep.(blueprint.Module)))
586 }
587 }
588}
589
Jiyong Park2d492942018-03-05 17:44:10 +0900590type linkType int
591
592const (
593 javaCore linkType = iota
594 javaSdk
595 javaSystem
596 javaPlatform
597)
598
Jiyong Park46f78fb2018-10-20 16:33:17 +0900599func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700600 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700601 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900602 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
603 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
604 name == "core-lambda-stubs":
605 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100606 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900607 return javaCore, false
608 case name == "android_system_stubs_current":
609 return javaSystem, true
610 case strings.HasPrefix(ver, "system_"):
611 return javaSystem, false
612 case name == "android_test_stubs_current":
613 return javaSystem, true
614 case strings.HasPrefix(ver, "test_"):
615 return javaPlatform, false
616 case name == "android_stubs_current":
617 return javaSdk, true
618 case ver == "current":
619 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700620 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900621 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700622 default:
623 if _, err := strconv.Atoi(ver); err != nil {
624 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
625 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900626 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900627 }
628}
629
Jiyong Park750e5572018-01-31 00:20:13 +0900630func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700631 if ctx.Host() {
632 return
633 }
634
Jiyong Park46f78fb2018-10-20 16:33:17 +0900635 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
636 if stubs {
637 return
638 }
639 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900640 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."
641
642 switch myLinkType {
643 case javaCore:
644 if otherLinkType != javaCore {
645 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 +0900646 ctx.OtherModuleName(to))
647 }
Jiyong Park2d492942018-03-05 17:44:10 +0900648 break
649 case javaSdk:
650 if otherLinkType != javaCore && otherLinkType != javaSdk {
651 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
652 ctx.OtherModuleName(to))
653 }
654 break
655 case javaSystem:
656 if otherLinkType == javaPlatform {
657 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
658 ctx.OtherModuleName(to))
659 }
660 break
661 case javaPlatform:
662 // no restriction on link-type
663 break
Jiyong Park750e5572018-01-31 00:20:13 +0900664 }
665}
666
Colin Cross32f676a2017-09-06 13:41:06 -0700667func (j *Module) collectDeps(ctx android.ModuleContext) deps {
668 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700669
Colin Cross300f0382018-03-06 13:11:51 -0800670 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700671 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800672 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700673 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800674 } else if sdkDep.useFiles {
675 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700676 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800677 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
678 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700679 }
680
Colin Crossd11fcda2017-10-23 17:59:01 -0700681 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700682 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700683 tag := ctx.OtherModuleDependencyTag(module)
684
Colin Crossa4f08812018-10-02 22:03:40 -0700685 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700686 // Handled by AndroidApp.collectAppDeps
687 return
688 }
689 if tag == certificateTag {
690 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700691 return
692 }
693
Jiyong Park750e5572018-01-31 00:20:13 +0900694 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700695 switch tag {
696 case bootClasspathTag, libTag, staticLibTag:
697 checkLinkType(ctx, j, to, tag.(dependencyTag))
698 }
Jiyong Park750e5572018-01-31 00:20:13 +0900699 }
Colin Cross54250902017-12-05 09:28:08 -0800700 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800701 case SdkLibraryDependency:
702 switch tag {
703 case libTag:
704 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
705 // names of sdk libs that are directly depended are exported
706 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
707 default:
708 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
709 }
Colin Cross54250902017-12-05 09:28:08 -0800710 case Dependency:
711 switch tag {
712 case bootClasspathTag:
713 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700714 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800715 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900716 // sdk lib names from dependencies are re-exported
717 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800718 case staticLibTag:
719 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
720 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
721 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700722 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900723 // sdk lib names from dependencies are re-exported
724 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800725 case pluginTag:
726 if plugin, ok := dep.(*Plugin); ok {
727 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
728 if plugin.pluginProperties.Processor_class != nil {
729 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
730 }
731 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
732 } else {
733 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
734 }
Colin Cross54250902017-12-05 09:28:08 -0800735 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100736 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800737 // framework.jar has a one-off dependency on the R.java and Manifest.java files
738 // generated by framework-res.apk
739 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
740 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800741 case frameworkApkTag:
742 if ctx.ModuleName() == "android_stubs_current" ||
743 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700744 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800745 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
746 // resource files out of there for aapt.
747 //
748 // Normally the package rule runs aapt, which includes the resource,
749 // but we're not running that in our package rule so just copy in the
750 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700751 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800752 }
Colin Cross54250902017-12-05 09:28:08 -0800753 case kotlinStdlibTag:
754 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800755 case kotlinAnnotationsTag:
756 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800757 }
758
759 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
760 case android.SourceFileProducer:
761 switch tag {
762 case libTag:
763 checkProducesJars(ctx, dep)
764 deps.classpath = append(deps.classpath, dep.Srcs()...)
765 case staticLibTag:
766 checkProducesJars(ctx, dep)
767 deps.classpath = append(deps.classpath, dep.Srcs()...)
768 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
769 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
770 case android.DefaultsDepTag, android.SourceDepTag:
771 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900772 case publicApiFileTag, systemApiFileTag, testApiFileTag:
773 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800774 default:
775 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
776 }
777 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700778 switch tag {
779 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700780 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700781 case systemModulesTag:
782 if deps.systemModules != nil {
783 panic("Found two system module dependencies")
784 }
785 sm := module.(*SystemModules)
786 if sm.outputFile == nil {
787 panic("Missing directory for system module dependency")
788 }
789 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700790 default:
791 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700792 }
Colin Crossec7a0422017-07-07 14:47:12 -0700793 }
Colin Cross2fe66872015-03-30 17:20:39 -0700794 })
795
Jiyong Park1be96912018-05-28 18:02:19 +0900796 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
797
Colin Cross32f676a2017-09-06 13:41:06 -0700798 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700799}
800
Colin Cross83bb3162018-06-25 15:48:06 -0700801func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700802 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800803 v := sdkContext.sdkVersion()
804 // For PDK builds, use the latest SDK version instead of "current"
805 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
806 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
807 latestSdkVersion := 0
808 if len(sdkVersions) > 0 {
809 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
810 }
811 v = strconv.Itoa(latestSdkVersion)
812 }
813
814 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700815 if err != nil {
816 ctx.PropertyErrorf("sdk_version", "%s", err)
817 }
Nan Zhang357466b2018-04-17 17:38:36 -0700818 if javaVersion != "" {
819 ret = javaVersion
820 } else if ctx.Device() && sdk <= 23 {
821 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900822 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700823 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700824 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700825 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
826 ret = "1.8"
827 } else {
828 ret = "1.9"
829 }
830
831 return ret
832}
833
Nan Zhanged19fc32017-10-19 13:06:22 -0700834func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700835
Colin Crossf03c82b2015-04-13 13:53:40 -0700836 var flags javaBuilderFlags
837
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100838 // javaVersion flag.
839 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
840
Nan Zhanged19fc32017-10-19 13:06:22 -0700841 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700842 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100843 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700844 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700845 }
Colin Cross6510f912017-11-29 00:27:14 -0800846 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700847 // Override the -g flag passed globally to remove local variable debug info to reduce
848 // disk and memory usage.
849 javacFlags = append(javacFlags, "-g:source,lines")
850 }
Colin Cross64162712017-08-08 13:17:59 -0700851
Colin Cross66548102018-06-19 22:47:35 -0700852 if ctx.Config().RunErrorProne() {
853 if config.ErrorProneClasspath == nil {
854 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
855 }
856
857 errorProneFlags := []string{
858 "-Xplugin:ErrorProne",
859 "${config.ErrorProneChecks}",
860 }
861 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
862
863 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
864 "'" + strings.Join(errorProneFlags, " ") + "'"
865 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800866 }
867
Nan Zhanged19fc32017-10-19 13:06:22 -0700868 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800869 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
870 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700871 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800872
Colin Crossbe9cdb82019-01-21 21:37:16 -0800873 flags.processor = strings.Join(deps.processorClasses, ",")
874
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100875 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800876 !Bool(j.properties.No_standard_libs) &&
877 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
878 // Give host-side tools a version of OpenJDK's standard libraries
879 // close to what they're targeting. As of Dec 2017, AOSP is only
880 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
881 //
882 // When building with OpenJDK 8, the following should have no
883 // effect since those jars would be available by default.
884 //
885 // When building with OpenJDK 9 but targeting a version < 1.8,
886 // putting them on the bootclasspath means that:
887 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
888 // b) references to existing APIs are not reinterpreted in an
889 // OpenJDK 9-specific way, eg. calls to subclasses of
890 // java.nio.Buffer as in http://b/70862583
891 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
892 flags.bootClasspath = append(flags.bootClasspath,
893 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
894 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800895 if Bool(j.properties.Use_tools_jar) {
896 flags.bootClasspath = append(flags.bootClasspath,
897 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
898 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800899 }
900
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100901 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800902 // Manually specify build directory in case it is not under the repo root.
903 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
904 // just adding a symlink under the root doesn't help.)
905 patchPaths := ".:" + ctx.Config().BuildDir()
906 classPath := flags.classpath.FormJavaClassPath("")
907 if classPath != "" {
908 patchPaths += ":" + classPath
909 }
910 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700911 }
912
Nan Zhanged19fc32017-10-19 13:06:22 -0700913 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700914 if deps.systemModules != nil {
915 flags.systemModules = append(flags.systemModules, deps.systemModules)
916 }
917
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700919 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700920 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700921 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700922 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
923 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700924 }
925
Colin Cross81440082018-08-15 20:21:55 -0700926 if len(javacFlags) > 0 {
927 // optimization.
928 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
929 flags.javacFlags = "$javacFlags"
930 }
931
Nan Zhanged19fc32017-10-19 13:06:22 -0700932 return flags
933}
Colin Crossc0b06f12015-04-08 13:03:43 -0700934
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800935func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700936
Colin Crossebe1a512017-11-14 13:12:14 -0800937 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700938
939 deps := j.collectDeps(ctx)
940 flags := j.collectBuilderFlags(ctx, deps)
941
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100942 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700943 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
944 }
Colin Cross8a497952019-03-05 22:25:09 -0800945 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700946 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800947 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700948 }
949
Colin Crossaf050172017-11-15 23:01:59 -0800950 srcFiles = j.genSources(ctx, srcFiles, flags)
951
952 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700953 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800954 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700955
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700956 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
957 // that IDEInfo struct will use
958 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
959
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800960 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800961 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800962 }
963
Colin Cross1ee23172017-10-18 14:44:18 -0700964 jarName := ctx.ModuleName() + ".jar"
965
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000966 javaSrcFiles := srcFiles.FilterByExt(".java")
967 var uniqueSrcFiles android.Paths
968 set := make(map[string]bool)
969 for _, v := range javaSrcFiles {
970 if _, found := set[v.String()]; !found {
971 set[v.String()] = true
972 uniqueSrcFiles = append(uniqueSrcFiles, v)
973 }
974 }
975
Colin Cross55f63ea2018-08-27 12:37:09 -0700976 var kotlinJars android.Paths
977
Colin Cross93e85952017-08-15 13:34:18 -0700978 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200979 // user defined kotlin flags.
980 kotlincFlags := j.properties.Kotlincflags
981 CheckKotlincFlags(ctx, kotlincFlags)
982
Colin Cross93e85952017-08-15 13:34:18 -0700983 // If there are kotlin files, compile them first but pass all the kotlin and java files
984 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
985 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200986 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700987 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200988 kotlincFlags = append(kotlincFlags, "-no-jdk")
989 }
990 if len(kotlincFlags) > 0 {
991 // optimization.
992 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
993 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700994 }
995
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000996 var kotlinSrcFiles android.Paths
997 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
998 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
999
Colin Crossafbb1732019-01-17 15:42:52 -08001000 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1001 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1002
1003 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1004 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1005
1006 if len(flags.processorPath) > 0 {
1007 // Use kapt for annotation processing
1008 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1009 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1010 srcJars = append(srcJars, kaptSrcJar)
1011 // Disable annotation processing in javac, it's already been handled by kapt
1012 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001013 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001014 }
Colin Cross93e85952017-08-15 13:34:18 -07001015
Colin Cross1ee23172017-10-18 14:44:18 -07001016 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001017 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001018 if ctx.Failed() {
1019 return
1020 }
1021
1022 // Make javac rule depend on the kotlinc rule
1023 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001024
Colin Cross93e85952017-08-15 13:34:18 -07001025 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001026 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001027 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001028 }
1029
Colin Cross55f63ea2018-08-27 12:37:09 -07001030 jars := append(android.Paths(nil), kotlinJars...)
1031
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001032 // Store the list of .java files that was passed to javac
1033 j.compiledJavaSrcs = uniqueSrcFiles
1034 j.compiledSrcJars = srcJars
1035
Nan Zhang61eaedb2017-11-02 13:28:15 -07001036 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001037 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001038 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1039 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001040 // Formerly, there was a check here that prevented annotation processors
1041 // from being used when sharding was enabled, as some annotation processors
1042 // do not function correctly in sharded environments. It was removed to
1043 // allow for the use of annotation processors that do function correctly
1044 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001045 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001046 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001047 if ctx.Failed() {
1048 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001049 }
1050 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001051 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001052 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001053 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001054 // If error-prone is enabled, add an additional rule to compile the java files into
1055 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001056 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001057 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1058 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001059 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001060 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001061 extraJarDeps = append(extraJarDeps, errorprone)
1062 }
1063
Nan Zhang61eaedb2017-11-02 13:28:15 -07001064 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001065 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001066 shardSize := int(*(j.properties.Javac_shard_size))
1067 var shardSrcs []android.Paths
1068 if len(uniqueSrcFiles) > 0 {
1069 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1070 for idx, shardSrc := range shardSrcs {
1071 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1072 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1073 jars = append(jars, classes)
1074 }
1075 }
1076 if len(srcJars) > 0 {
1077 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1078 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1079 jars = append(jars, classes)
1080 }
1081 } else {
1082 classes := android.PathForModuleOut(ctx, "javac", jarName)
1083 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1084 jars = append(jars, classes)
1085 }
Colin Crossd6891432017-09-27 17:39:56 -07001086 if ctx.Failed() {
1087 return
1088 }
Colin Cross2fe66872015-03-30 17:20:39 -07001089 }
1090
Colin Crosscedd4762018-09-13 11:26:19 -07001091 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1092 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001093 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1094
1095 var resArgs []string
1096 var resDeps android.Paths
1097
1098 resArgs = append(resArgs, dirArgs...)
1099 resDeps = append(resDeps, dirDeps...)
1100
1101 resArgs = append(resArgs, fileArgs...)
1102 resDeps = append(resDeps, fileDeps...)
1103
Colin Crossff3ae9d2018-04-10 16:15:18 -07001104 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001105 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001106 resArgs = append(resArgs, srcArgs...)
1107 resDeps = append(resDeps, srcDeps...)
1108 }
Colin Cross40a36712017-09-27 17:41:35 -07001109
1110 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001111 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001112 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001113 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001114 if ctx.Failed() {
1115 return
1116 }
1117 }
1118
Colin Cross331a1212018-08-15 20:40:52 -07001119 if len(deps.staticResourceJars) > 0 {
1120 var jars android.Paths
1121 if j.resourceJar != nil {
1122 jars = append(jars, j.resourceJar)
1123 }
1124 jars = append(jars, deps.staticResourceJars...)
1125
1126 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1127 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1128 false, nil, nil)
1129 j.resourceJar = combinedJar
1130 }
1131
Colin Cross32f676a2017-09-06 13:41:06 -07001132 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001133
Colin Cross094054a2018-10-17 15:10:48 -07001134 manifest := j.overrideManifest
1135 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001136 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001137 }
Colin Cross635acc92017-09-12 22:50:46 -07001138
Colin Cross8a497952019-03-05 22:25:09 -08001139 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001140 if len(services) > 0 {
1141 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1142 var zipargs []string
1143 for _, file := range services {
1144 serviceFile := file.String()
1145 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1146 }
1147 ctx.Build(pctx, android.BuildParams{
1148 Rule: zip,
1149 Output: servicesJar,
1150 Implicits: services,
1151 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001152 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001153 },
1154 })
1155 jars = append(jars, servicesJar)
1156 }
1157
Colin Cross0a6e0072017-08-30 14:24:55 -07001158 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001159 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001160 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001161
1162 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001163 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1164 // Optimization: skip the combine step if there is nothing to do
1165 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1166 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1167 // any if len(jars) == 1.
1168 outputFile = moduleOutPath
1169 } else {
1170 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1171 ctx.Build(pctx, android.BuildParams{
1172 Rule: android.Cp,
1173 Input: jars[0],
1174 Output: combinedJar,
1175 })
1176 outputFile = combinedJar
1177 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001178 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001179 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001180 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001181 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001182 outputFile = combinedJar
1183 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001184
Colin Cross331a1212018-08-15 20:40:52 -07001185 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001186 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001187 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001188 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001189 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001190 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001191
1192 // jarjar resource jar if necessary
1193 if j.resourceJar != nil {
1194 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001195 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001196 j.resourceJar = resourceJarJarFile
1197 }
1198
Colin Cross0a6e0072017-08-30 14:24:55 -07001199 if ctx.Failed() {
1200 return
1201 }
1202 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001203 j.implementationJarFile = outputFile
1204 if j.headerJarFile == nil {
1205 j.headerJarFile = j.implementationJarFile
1206 }
Colin Cross2fe66872015-03-30 17:20:39 -07001207
Colin Cross6510f912017-11-29 00:27:14 -08001208 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001209 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1210 j.properties.Instrument = true
1211 }
1212 }
1213
Colin Cross3144dfc2018-01-03 15:06:47 -08001214 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001215 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1216 }
1217
Colin Cross331a1212018-08-15 20:40:52 -07001218 // merge implementation jar with resources if necessary
1219 implementationAndResourcesJar := outputFile
1220 if j.resourceJar != nil {
1221 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1222 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1223 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1224 false, nil, nil)
1225 implementationAndResourcesJar = combinedJar
1226 }
1227
1228 j.implementationAndResourcesJar = implementationAndResourcesJar
1229
Colin Cross9ae1b922018-06-26 17:59:05 -07001230 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001231 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001232 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001233 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001234 if ctx.Failed() {
1235 return
1236 }
Colin Cross331a1212018-08-15 20:40:52 -07001237
Colin Cross8faf8fc2019-01-16 15:15:52 -08001238 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001239 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1240 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001241
Colin Cross331a1212018-08-15 20:40:52 -07001242 // merge dex jar with resources if necessary
1243 if j.resourceJar != nil {
1244 jars := android.Paths{dexOutputFile, j.resourceJar}
1245 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1246 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1247 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001248 if j.deviceProperties.UncompressDex {
1249 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1250 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1251 dexOutputFile = combinedAlignedJar
1252 } else {
1253 dexOutputFile = combinedJar
1254 }
Colin Cross331a1212018-08-15 20:40:52 -07001255 }
1256
1257 j.dexJarFile = dexOutputFile
1258
Colin Cross8faf8fc2019-01-16 15:15:52 -08001259 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001260 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1261
1262 j.maybeStrippedDexJarFile = dexOutputFile
1263
Colin Cross3063b782018-08-15 11:19:12 -07001264 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001265
1266 if ctx.Failed() {
1267 return
1268 }
Colin Cross331a1212018-08-15 20:40:52 -07001269 } else {
1270 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001271 }
Colin Cross331a1212018-08-15 20:40:52 -07001272
Colin Crossb7a63242015-04-16 14:09:14 -07001273 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001274
1275 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1276 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001277}
1278
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001279// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1280// since some of these flags may be used internally.
1281func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1282 for _, flag := range flags {
1283 flag = strings.TrimSpace(flag)
1284
1285 if !strings.HasPrefix(flag, "-") {
1286 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1287 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1288 ctx.PropertyErrorf("kotlincflags",
1289 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1290 } else if inList(flag, config.KotlincIllegalFlags) {
1291 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1292 } else if flag == "-include-runtime" {
1293 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1294 } else {
1295 args := strings.Split(flag, " ")
1296 if args[0] == "-kotlin-home" {
1297 ctx.PropertyErrorf("kotlincflags",
1298 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1299 }
1300 }
1301 }
1302}
1303
Colin Cross8eadbf02017-10-24 17:46:00 -07001304func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001305 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001306
1307 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001308 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001309 // Compile java sources into turbine.jar.
1310 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1311 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1312 if ctx.Failed() {
1313 return nil
1314 }
1315 jars = append(jars, turbineJar)
1316 }
1317
Colin Cross55f63ea2018-08-27 12:37:09 -07001318 jars = append(jars, extraJars...)
1319
Nan Zhanged19fc32017-10-19 13:06:22 -07001320 // Combine any static header libraries into classes-header.jar. If there is only
1321 // one input jar this step will be skipped.
1322 var headerJar android.Path
1323 jars = append(jars, deps.staticHeaderJars...)
1324
Colin Cross5c6ecc12017-10-23 18:12:27 -07001325 // we cannot skip the combine step for now if there is only one jar
1326 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1327 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001328 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1329 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001330 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001331
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001332 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001333 // Transform classes.jar into classes-jarjar.jar
1334 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001335 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001336 headerJar = jarjarFile
1337 if ctx.Failed() {
1338 return nil
1339 }
1340 }
1341
1342 return headerJar
1343}
1344
Colin Crosscb933592017-11-22 13:49:43 -08001345func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001346 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001347
Colin Cross7a3139e2017-12-19 13:57:50 -08001348 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001349
Colin Cross84c38822018-01-03 15:59:46 -08001350 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001351 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1352
1353 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1354
1355 j.jacocoReportClassesFile = jacocoReportClassesFile
1356
1357 return instrumentedJar
1358}
1359
albaltai36ff7dc2018-12-25 14:35:23 +08001360var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001361
Nan Zhanged19fc32017-10-19 13:06:22 -07001362func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001363 if j.headerJarFile == nil {
1364 return nil
1365 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001366 return android.Paths{j.headerJarFile}
1367}
1368
1369func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001370 if j.implementationJarFile == nil {
1371 return nil
1372 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001373 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001374}
1375
Colin Crossf24a22a2019-01-31 14:12:44 -08001376func (j *Module) DexJar() android.Path {
1377 return j.dexJarFile
1378}
1379
Colin Cross331a1212018-08-15 20:40:52 -07001380func (j *Module) ResourceJars() android.Paths {
1381 if j.resourceJar == nil {
1382 return nil
1383 }
1384 return android.Paths{j.resourceJar}
1385}
1386
1387func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001388 if j.implementationAndResourcesJar == nil {
1389 return nil
1390 }
Colin Cross331a1212018-08-15 20:40:52 -07001391 return android.Paths{j.implementationAndResourcesJar}
1392}
1393
Colin Cross46c9b8b2017-06-22 16:51:17 -07001394func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001395 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001396 return j.exportAidlIncludeDirs
1397}
1398
Jiyong Park1be96912018-05-28 18:02:19 +09001399func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001400 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001401 return j.exportedSdkLibs
1402}
1403
Colin Cross46c9b8b2017-06-22 16:51:17 -07001404var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001405
Colin Cross46c9b8b2017-06-22 16:51:17 -07001406func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001407 return j.logtagsSrcs
1408}
1409
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001410// Collect information for opening IDE project files in java/jdeps.go.
1411func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1412 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1413 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1414 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001415 if j.expandJarjarRules != nil {
1416 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001417 }
1418}
1419
1420func (j *Module) CompilerDeps() []string {
1421 jdeps := []string{}
1422 jdeps = append(jdeps, j.properties.Libs...)
1423 jdeps = append(jdeps, j.properties.Static_libs...)
1424 return jdeps
1425}
1426
Colin Cross2fe66872015-03-30 17:20:39 -07001427//
1428// Java libraries (.jar file)
1429//
1430
Colin Crossf506d872017-07-19 15:53:04 -07001431type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001432 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001433}
1434
Colin Cross42be7612019-02-21 18:12:14 -08001435func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001436 // Store uncompressed (and do not strip) dex files from boot class path jars.
1437 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1438 return true
1439 }
1440
1441 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001442 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001443 return true
1444 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001445 if ctx.Config().UncompressPrivAppDex() &&
1446 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1447 return true
1448 }
1449
Colin Cross2fc72f62018-12-21 12:59:54 -08001450 return false
1451}
1452
Colin Crossf506d872017-07-19 15:53:04 -07001453func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001454 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1455 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001456 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001457 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001458 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001459 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001460
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001461 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001462 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1463 ctx.ModuleName()+".jar", j.outputFile)
1464 }
Colin Crossb7a63242015-04-16 14:09:14 -07001465}
1466
Colin Crossf506d872017-07-19 15:53:04 -07001467func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001468 j.deps(ctx)
1469}
1470
Colin Cross1b16b0e2019-02-12 14:41:32 -08001471// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1472//
1473// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1474// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1475// as a `static_libs` dependency of another module.
1476//
1477// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1478// a device.
1479//
1480// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1481// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001482func LibraryFactory() android.Module {
1483 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001484
Colin Cross9ae1b922018-06-26 17:59:05 -07001485 module.AddProperties(
1486 &module.Module.properties,
1487 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001488 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001489 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001490
Colin Cross9ae1b922018-06-26 17:59:05 -07001491 InitJavaModule(module, android.HostAndDeviceSupported)
1492 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001493}
1494
Colin Cross1b16b0e2019-02-12 14:41:32 -08001495// java_library_static is an obsolete alias for java_library.
1496func LibraryStaticFactory() android.Module {
1497 return LibraryFactory()
1498}
1499
1500// java_library_host builds and links sources into a `.jar` file for the host.
1501//
1502// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1503// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001504func LibraryHostFactory() android.Module {
1505 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001506
Colin Cross6af17aa2017-09-20 12:59:05 -07001507 module.AddProperties(
1508 &module.Module.properties,
1509 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001510
Colin Cross9ae1b922018-06-26 17:59:05 -07001511 module.Module.properties.Installable = proptools.BoolPtr(true)
1512
Colin Cross89536d42017-07-07 14:35:50 -07001513 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001514 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001515}
1516
1517//
Colin Crossb628ea52018-08-14 16:42:33 -07001518// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001519//
1520
1521type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001522 // list of compatibility suites (for example "cts", "vts") that the module should be
1523 // installed into.
1524 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001525
1526 // the name of the test configuration (for example "AndroidTest.xml") that should be
1527 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001528 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001529
Jack He33338892018-09-19 02:21:28 -07001530 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1531 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001532 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001533
Colin Crossd96ca352018-08-10 16:06:24 -07001534 // list of files or filegroup modules that provide data that should be installed alongside
1535 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001536 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001537}
1538
1539type Test struct {
1540 Library
1541
1542 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001543
1544 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001545 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001546}
1547
1548func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001549 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 -08001550 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001551
1552 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001553}
1554
Colin Cross1b16b0e2019-02-12 14:41:32 -08001555// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1556// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1557//
1558// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1559// compiled against the device bootclasspath.
1560//
1561// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1562// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001563func TestFactory() android.Module {
1564 module := &Test{}
1565
1566 module.AddProperties(
1567 &module.Module.properties,
1568 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001569 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001570 &module.Module.protoProperties,
1571 &module.testProperties)
1572
Colin Cross9ae1b922018-06-26 17:59:05 -07001573 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001574 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001575
Colin Cross05638fc2018-04-09 18:40:24 -07001576 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001577 return module
1578}
1579
Colin Cross1b16b0e2019-02-12 14:41:32 -08001580// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1581// allow running the test with `atest` or a `TEST_MAPPING` file.
1582//
1583// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1584// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001585func TestHostFactory() android.Module {
1586 module := &Test{}
1587
1588 module.AddProperties(
1589 &module.Module.properties,
1590 &module.Module.protoProperties,
1591 &module.testProperties)
1592
Colin Cross9ae1b922018-06-26 17:59:05 -07001593 module.Module.properties.Installable = proptools.BoolPtr(true)
1594
Colin Cross05638fc2018-04-09 18:40:24 -07001595 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001596 return module
1597}
1598
1599//
Colin Cross2fe66872015-03-30 17:20:39 -07001600// Java Binaries (.jar file plus wrapper script)
1601//
1602
Colin Crossf506d872017-07-19 15:53:04 -07001603type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001604 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001605 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001606
1607 // Name of the class containing main to be inserted into the manifest as Main-Class.
1608 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001609}
1610
Colin Crossf506d872017-07-19 15:53:04 -07001611type Binary struct {
1612 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001613
Colin Crossf506d872017-07-19 15:53:04 -07001614 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001615
Colin Cross6b4a32d2017-12-05 13:42:45 -08001616 isWrapperVariant bool
1617
Colin Crossc3315992017-12-08 19:12:36 -08001618 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001619 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001620}
1621
Alex Light24237172017-10-26 09:46:21 -07001622func (j *Binary) HostToolPath() android.OptionalPath {
1623 return android.OptionalPathForPath(j.binaryFile)
1624}
1625
Colin Crossf506d872017-07-19 15:53:04 -07001626func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001627 if ctx.Arch().ArchType == android.Common {
1628 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001629 if j.binaryProperties.Main_class != nil {
1630 if j.properties.Manifest != nil {
1631 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1632 }
1633 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1634 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1635 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1636 }
1637
Colin Cross6b4a32d2017-12-05 13:42:45 -08001638 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001639 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001640 // Handle the binary wrapper
1641 j.isWrapperVariant = true
1642
Colin Cross366938f2017-12-11 16:29:02 -08001643 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001644 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001645 } else {
1646 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1647 }
1648
1649 // Depend on the installed jar so that the wrapper doesn't get executed by
1650 // another build rule before the jar has been installed.
1651 jarFile := ctx.PrimaryModule().(*Binary).installFile
1652
1653 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1654 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001655 }
Colin Cross2fe66872015-03-30 17:20:39 -07001656}
1657
Colin Crossf506d872017-07-19 15:53:04 -07001658func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001659 if ctx.Arch().ArchType == android.Common {
1660 j.deps(ctx)
1661 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001662}
1663
Colin Cross1b16b0e2019-02-12 14:41:32 -08001664// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1665// as well.
1666//
1667// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1668// compiled against the device bootclasspath.
1669//
1670// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1671// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001672func BinaryFactory() android.Module {
1673 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001674
Colin Cross36242852017-06-23 15:06:31 -07001675 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001676 &module.Module.properties,
1677 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001678 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001679 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001680 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001681
Colin Cross9ae1b922018-06-26 17:59:05 -07001682 module.Module.properties.Installable = proptools.BoolPtr(true)
1683
Colin Cross6b4a32d2017-12-05 13:42:45 -08001684 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1685 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001686 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001687}
1688
Colin Cross1b16b0e2019-02-12 14:41:32 -08001689// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1690//
1691// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1692// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001693func BinaryHostFactory() android.Module {
1694 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001695
Colin Cross36242852017-06-23 15:06:31 -07001696 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001697 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001698 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001699 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001700
Colin Cross9ae1b922018-06-26 17:59:05 -07001701 module.Module.properties.Installable = proptools.BoolPtr(true)
1702
Colin Cross6b4a32d2017-12-05 13:42:45 -08001703 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1704 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001705 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001706}
1707
1708//
1709// Java prebuilts
1710//
1711
Colin Cross74d73e22017-08-02 11:05:49 -07001712type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001713 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001714
Nan Zhangea568a42017-11-08 21:20:04 -08001715 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001716
1717 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001718
1719 // List of shared java libs that this module has dependencies to
1720 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001721
1722 // List of files to remove from the jar file(s)
1723 Exclude_files []string
1724
1725 // List of directories to remove from the jar file(s)
1726 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001727
1728 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001729 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001730}
1731
1732type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001733 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001734 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001735 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001736
Colin Cross74d73e22017-08-02 11:05:49 -07001737 properties ImportProperties
1738
Colin Cross0a6e0072017-08-30 14:24:55 -07001739 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001740 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001741}
1742
Colin Cross83bb3162018-06-25 15:48:06 -07001743func (j *Import) sdkVersion() string {
1744 return String(j.properties.Sdk_version)
1745}
1746
1747func (j *Import) minSdkVersion() string {
1748 return j.sdkVersion()
1749}
1750
Colin Cross74d73e22017-08-02 11:05:49 -07001751func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001752 return &j.prebuilt
1753}
1754
Colin Cross74d73e22017-08-02 11:05:49 -07001755func (j *Import) PrebuiltSrcs() []string {
1756 return j.properties.Jars
1757}
1758
1759func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001760 return j.prebuilt.Name(j.ModuleBase.Name())
1761}
1762
Colin Cross74d73e22017-08-02 11:05:49 -07001763func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001764 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001765}
1766
Colin Cross74d73e22017-08-02 11:05:49 -07001767func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001768 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001769
Nan Zhang4c819fb2018-08-27 18:31:46 -07001770 jarName := ctx.ModuleName() + ".jar"
1771 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001772 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1773 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001774 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001775 inputFile := outputFile
1776 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1777 TransformJetifier(ctx, outputFile, inputFile)
1778 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001779 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001780
1781 ctx.VisitDirectDeps(func(module android.Module) {
1782 otherName := ctx.OtherModuleName(module)
1783 tag := ctx.OtherModuleDependencyTag(module)
1784
1785 switch dep := module.(type) {
1786 case Dependency:
1787 switch tag {
1788 case libTag, staticLibTag:
1789 // sdk lib names from dependencies are re-exported
1790 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1791 }
1792 case SdkLibraryDependency:
1793 switch tag {
1794 case libTag:
1795 // names of sdk libs that are directly depended are exported
1796 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1797 }
1798 }
1799 })
1800
1801 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001802 if Bool(j.properties.Installable) {
1803 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1804 ctx.ModuleName()+".jar", outputFile)
1805 }
Colin Cross2fe66872015-03-30 17:20:39 -07001806}
1807
Colin Cross74d73e22017-08-02 11:05:49 -07001808var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001809
Nan Zhanged19fc32017-10-19 13:06:22 -07001810func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001811 if j.combinedClasspathFile == nil {
1812 return nil
1813 }
Colin Cross37f6d792018-07-12 12:28:41 -07001814 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001815}
1816
1817func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001818 if j.combinedClasspathFile == nil {
1819 return nil
1820 }
Colin Cross37f6d792018-07-12 12:28:41 -07001821 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001822}
1823
Colin Cross331a1212018-08-15 20:40:52 -07001824func (j *Import) ResourceJars() android.Paths {
1825 return nil
1826}
1827
1828func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001829 if j.combinedClasspathFile == nil {
1830 return nil
1831 }
Colin Cross331a1212018-08-15 20:40:52 -07001832 return android.Paths{j.combinedClasspathFile}
1833}
1834
Colin Crossf24a22a2019-01-31 14:12:44 -08001835func (j *Import) DexJar() android.Path {
1836 return nil
1837}
1838
Colin Cross74d73e22017-08-02 11:05:49 -07001839func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001840 return nil
1841}
1842
Jiyong Park1be96912018-05-28 18:02:19 +09001843func (j *Import) ExportedSdkLibs() []string {
1844 return j.exportedSdkLibs
1845}
1846
albaltai36ff7dc2018-12-25 14:35:23 +08001847// Add compile time check for interface implementation
1848var _ android.IDEInfo = (*Import)(nil)
1849var _ android.IDECustomizedModuleName = (*Import)(nil)
1850
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001851// Collect information for opening IDE project files in java/jdeps.go.
1852const (
1853 removedPrefix = "prebuilt_"
1854)
1855
1856func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1857 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1858}
1859
1860func (j *Import) IDECustomizedModuleName() string {
1861 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1862 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1863 // solution to get the Import name.
1864 name := j.Name()
1865 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001866 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001867 }
1868 return name
1869}
1870
Colin Cross74d73e22017-08-02 11:05:49 -07001871var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001872
Colin Cross1b16b0e2019-02-12 14:41:32 -08001873// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1874//
1875// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1876// compiled against an Android classpath.
1877//
1878// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1879// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001880func ImportFactory() android.Module {
1881 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001882
Colin Cross74d73e22017-08-02 11:05:49 -07001883 module.AddProperties(&module.properties)
1884
1885 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001886 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001887 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001888}
1889
Colin Cross1b16b0e2019-02-12 14:41:32 -08001890// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1891// module.
1892//
1893// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1894// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001895func ImportFactoryHost() android.Module {
1896 module := &Import{}
1897
1898 module.AddProperties(&module.properties)
1899
1900 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001901 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001902 return module
1903}
1904
Colin Cross42be7612019-02-21 18:12:14 -08001905// dex_import module
1906
1907type DexImportProperties struct {
1908 Jars []string
1909}
1910
1911type DexImport struct {
1912 android.ModuleBase
1913 android.DefaultableModuleBase
1914 prebuilt android.Prebuilt
1915
1916 properties DexImportProperties
1917
1918 dexJarFile android.Path
1919 maybeStrippedDexJarFile android.Path
1920
1921 dexpreopter
1922}
1923
1924func (j *DexImport) Prebuilt() *android.Prebuilt {
1925 return &j.prebuilt
1926}
1927
1928func (j *DexImport) PrebuiltSrcs() []string {
1929 return j.properties.Jars
1930}
1931
1932func (j *DexImport) Name() string {
1933 return j.prebuilt.Name(j.ModuleBase.Name())
1934}
1935
1936func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1937 android.ExtractSourcesDeps(ctx, j.properties.Jars)
1938}
1939
1940func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1941 if len(j.properties.Jars) != 1 {
1942 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1943 }
1944
1945 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1946 j.dexpreopter.isInstallable = true
1947 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1948
1949 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1950 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1951
1952 if j.dexpreopter.uncompressedDex {
1953 rule := android.NewRuleBuilder()
1954
1955 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1956 rule.Temporary(temporary)
1957
1958 // use zip2zip to uncompress classes*.dex files
1959 rule.Command().
1960 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
1961 FlagWithInput("-i ", inputJar).
1962 FlagWithOutput("-o ", temporary).
1963 FlagWithArg("-0 ", "'classes*.dex'")
1964
1965 // use zipalign to align uncompressed classes*.dex files
1966 rule.Command().
1967 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
1968 Flag("-f").
1969 Text("4").
1970 Input(temporary).
1971 Output(dexOutputFile)
1972
1973 rule.DeleteTemporaryFiles()
1974
1975 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
1976 } else {
1977 ctx.Build(pctx, android.BuildParams{
1978 Rule: android.Cp,
1979 Input: inputJar,
1980 Output: dexOutputFile,
1981 })
1982 }
1983
1984 j.dexJarFile = dexOutputFile
1985
1986 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1987
1988 j.maybeStrippedDexJarFile = dexOutputFile
1989
1990 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1991 ctx.ModuleName()+".jar", dexOutputFile)
1992}
1993
1994func (j *DexImport) DexJar() android.Path {
1995 return j.dexJarFile
1996}
1997
1998// dex_import imports a `.jar` file containing classes.dex files.
1999//
2000// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2001// to the device.
2002func DexImportFactory() android.Module {
2003 module := &DexImport{}
2004
2005 module.AddProperties(&module.properties)
2006
2007 android.InitPrebuiltModule(module, &module.properties.Jars)
2008 InitJavaModule(module, android.DeviceSupported)
2009 return module
2010}
2011
Colin Cross89536d42017-07-07 14:35:50 -07002012//
2013// Defaults
2014//
2015type Defaults struct {
2016 android.ModuleBase
2017 android.DefaultsModuleBase
2018}
2019
2020func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2021}
2022
Colin Cross1b16b0e2019-02-12 14:41:32 -08002023// java_defaults provides a set of properties that can be inherited by other java or android modules.
2024//
2025// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2026// property in the defaults module that exists in the depending module will be prepended to the depending module's
2027// value for that property.
2028//
2029// Example:
2030//
2031// java_defaults {
2032// name: "example_defaults",
2033// srcs: ["common/**/*.java"],
2034// javacflags: ["-Xlint:all"],
2035// aaptflags: ["--auto-add-overlay"],
2036// }
2037//
2038// java_library {
2039// name: "example",
2040// defaults: ["example_defaults"],
2041// srcs: ["example/**/*.java"],
2042// }
2043//
2044// is functionally identical to:
2045//
2046// java_library {
2047// name: "example",
2048// srcs: [
2049// "common/**/*.java",
2050// "example/**/*.java",
2051// ],
2052// javacflags: ["-Xlint:all"],
2053// }
Colin Cross89536d42017-07-07 14:35:50 -07002054func defaultsFactory() android.Module {
2055 return DefaultsFactory()
2056}
2057
2058func DefaultsFactory(props ...interface{}) android.Module {
2059 module := &Defaults{}
2060
2061 module.AddProperties(props...)
2062 module.AddProperties(
2063 &CompilerProperties{},
2064 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002065 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002066 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002067 &aaptProperties{},
2068 &androidLibraryProperties{},
2069 &appProperties{},
2070 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002071 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002072 &ImportProperties{},
2073 &AARImportProperties{},
2074 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002075 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002076 )
2077
2078 android.InitDefaultsModule(module)
2079
2080 return module
2081}
Nan Zhangea568a42017-11-08 21:20:04 -08002082
2083var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002084var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002085var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002086var inList = android.InList