blob: beee1a5b5f5f30c308d32f9463dfd0c0e287ebff [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Colin Cross2fe66872015-03-30 17:20:39 -070055// TODO:
56// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070057// Renderscript
58// Post-jar passes:
59// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070060// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070061// DroidDoc
62// Findbugs
63
Colin Cross89536d42017-07-07 14:35:50 -070064type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070065 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
66 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070068
69 // list of source files that should not be used to build the Java module.
70 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080071 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070072
73 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070074 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070075
Colin Cross86a63ff2017-09-27 17:33:10 -070076 // list of directories that should be excluded from java_resource_dirs
77 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070078
Colin Cross0f37af02017-09-27 17:42:05 -070079 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080080 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070081
Colin Crosscedd4762018-09-13 11:26:19 -070082 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080083 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070084
Paul Duffin2fbbfb82019-02-13 12:49:33 +000085 // don't build against the default libraries (bootclasspath, ext, and framework for device
86 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070087 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070088
Paul Duffin2fbbfb82019-02-13 12:49:33 +000089 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070090 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020095 // list of module-specific flags that will be used for kotlinc compiles
96 Kotlincflags []string `android:"arch_variant"`
97
Colin Cross7d5136f2015-05-11 13:39:40 -070098 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700102 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
104 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700106
Colin Cross540eff82017-06-22 17:01:52 -0700107 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700109
110 // If not blank, set the java version passed to javac as -source and -target
111 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700112
Colin Cross9ae1b922018-06-26 17:59:05 -0700113 // If set to true, allow this module to be dexed and installed on devices. Has no
114 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700115 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700116
Colin Cross0f37af02017-09-27 17:42:05 -0700117 // If set to true, include sources used to compile the module in to the final jar
118 Include_srcs *bool
119
Colin Crossbe9cdb82019-01-21 21:37:16 -0800120 // List of modules to use as annotation processors
121 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700122
Nan Zhang61eaedb2017-11-02 13:28:15 -0700123 // The number of Java source entries each Javac instance can process
124 Javac_shard_size *int64
125
Nan Zhang5f8cb422018-02-06 10:34:32 -0800126 // Add host jdk tools.jar to bootclasspath
127 Use_tools_jar *bool
128
Colin Cross1369cdb2017-09-29 17:58:17 -0700129 Openjdk9 struct {
130 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800131 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700132
133 // List of javac flags that should only be used when passing -source 1.9
134 Javacflags []string
135 }
Colin Crosscb933592017-11-22 13:49:43 -0800136
Colin Cross81440082018-08-15 20:21:55 -0700137 // When compiling language level 9+ .java code in packages that are part of
138 // a system module, patch_module names the module that your sources and
139 // dependencies should be patched into. The Android runtime currently
140 // doesn't implement the JEP 261 module system so this option is only
141 // supported at compile time. It should only be needed to compile tests in
142 // packages that exist in libcore and which are inconvenient to move
143 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100144 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700145
Colin Crosscb933592017-11-22 13:49:43 -0800146 Jacoco struct {
147 // List of classes to include for instrumentation with jacoco to collect coverage
148 // information at runtime when building with coverage enabled. If unset defaults to all
149 // classes.
150 // Supports '*' as the last character of an entry in the list as a wildcard match.
151 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
152 // it matches classes in the package that have the class name as a prefix.
153 Include_filter []string
154
155 // List of classes to exclude from instrumentation with jacoco to collect coverage
156 // information at runtime when building with coverage enabled. Overrides classes selected
157 // by the include_filter property.
158 // Supports '*' as the last character of an entry in the list as a wildcard match.
159 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
160 // it matches classes in the package that have the class name as a prefix.
161 Exclude_filter []string
162 }
163
Andreas Gampef3e5b552018-01-22 21:27:21 -0800164 Errorprone struct {
165 // List of javac flags that should only be used when running errorprone.
166 Javacflags []string
167 }
168
Colin Cross0f2ee152017-12-14 15:22:43 -0800169 Proto struct {
170 // List of extra options that will be passed to the proto generator.
171 Output_params []string
172 }
173
Colin Crosscb933592017-11-22 13:49:43 -0800174 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800175
176 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800177 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700178}
179
Colin Cross89536d42017-07-07 14:35:50 -0700180type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700181 // list of module-specific flags that will be used for dex compiles
182 Dxflags []string `android:"arch_variant"`
183
Colin Cross83bb3162018-06-25 15:48:06 -0700184 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
185 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800186 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700187
Colin Cross83bb3162018-06-25 15:48:06 -0700188 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
189 // Defaults to sdk_version if not set.
190 Min_sdk_version *string
191
Dan Willemsen419290a2018-10-31 15:28:47 -0700192 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
193 // Defaults to sdk_version if not set.
194 Target_sdk_version *string
195
Colin Cross6af2e492018-05-22 11:12:33 -0700196 // if true, compile against the platform APIs instead of an SDK.
197 Platform_apis *bool
198
Colin Crossebe1a512017-11-14 13:12:14 -0800199 Aidl struct {
200 // Top level directories to pass to aidl tool
201 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700202
Colin Crossebe1a512017-11-14 13:12:14 -0800203 // Directories rooted at the Android.bp file to pass to aidl tool
204 Local_include_dirs []string
205
206 // directories that should be added as include directories for any aidl sources of modules
207 // that depend on this module, as well as to aidl for this module.
208 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100209
210 // whether to generate traces (for systrace) for this interface
211 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100212
213 // whether to generate Binder#GetTransaction name method.
214 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800215 }
Colin Cross92430102017-10-09 14:59:32 -0700216
217 // If true, export a copy of the module as a -hostdex module for host testing.
218 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700219
David Brazdil17ef5632018-06-27 10:27:45 +0100220 // If set to true, compile dex regardless of installable. Defaults to false.
221 Compile_dex *bool
222
Colin Cross66dbc0b2017-12-28 12:23:20 -0800223 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700224 // If false, disable all optimization. Defaults to true for android_app and android_test
225 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800226 Enabled *bool
227
228 // If true, optimize for size by removing unused code. Defaults to true for apps,
229 // false for libraries and tests.
230 Shrink *bool
231
232 // If true, optimize bytecode. Defaults to false.
233 Optimize *bool
234
235 // If true, obfuscate bytecode. Defaults to false.
236 Obfuscate *bool
237
238 // If true, do not use the flag files generated by aapt that automatically keep
239 // classes referenced by the app manifest. Defaults to false.
240 No_aapt_flags *bool
241
242 // Flags to pass to proguard.
243 Proguard_flags []string
244
245 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800246 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800247 }
248
Colin Cross1369cdb2017-09-29 17:58:17 -0700249 // When targeting 1.9, override the modules to use with --system
250 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700251
252 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800253 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700254}
255
Colin Cross46c9b8b2017-06-22 16:51:17 -0700256// Module contains the properties and members used by all java module types
257type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700258 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700259 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700260
Colin Cross89536d42017-07-07 14:35:50 -0700261 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700262 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700263 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700264
Colin Cross331a1212018-08-15 20:40:52 -0700265 // jar file containing header classes including static library dependencies, suitable for
266 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700267 headerJarFile android.Path
268
Colin Cross331a1212018-08-15 20:40:52 -0700269 // jar file containing implementation classes including static library dependencies but no
270 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700271 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700272
Colin Cross331a1212018-08-15 20:40:52 -0700273 // jar file containing only resources including from static library dependencies
274 resourceJar android.Path
275
276 // jar file containing implementation classes and resources including static library
277 // dependencies
278 implementationAndResourcesJar android.Path
279
280 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700281 dexJarFile android.Path
282
Colin Cross43f08db2018-11-12 10:13:39 -0800283 // output file that contains classes.dex if it should be in the output file
284 maybeStrippedDexJarFile android.Path
285
Colin Crosscb933592017-11-22 13:49:43 -0800286 // output file containing uninstrumented classes that will be instrumented by jacoco
287 jacocoReportClassesFile android.Path
288
Colin Cross66dbc0b2017-12-28 12:23:20 -0800289 // output file containing mapping of obfuscated names
290 proguardDictionary android.Path
291
Colin Cross331a1212018-08-15 20:40:52 -0700292 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700293 outputFile android.Path
294 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700295
Colin Cross635c3b02016-05-18 15:37:25 -0700296 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700297
Colin Cross635c3b02016-05-18 15:37:25 -0700298 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700299
Colin Cross2fe66872015-03-30 17:20:39 -0700300 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700301 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800302
303 // list of .java files and srcjars that was passed to javac
304 compiledJavaSrcs android.Paths
305 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800306
307 // list of extra progurad flag files
308 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900309
Colin Cross094054a2018-10-17 15:10:48 -0700310 // manifest file to use instead of properties.Manifest
311 overrideManifest android.OptionalPath
312
Jiyong Park1be96912018-05-28 18:02:19 +0900313 // list of SDK lib names that this java moudule is exporting
314 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700315
316 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
317 // filter out Exclude_srcs, will be used by android.IDEInfo struct
318 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800319
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800320 // expanded Jarjar_rules
321 expandJarjarRules android.Path
322
Colin Crossf24a22a2019-01-31 14:12:44 -0800323 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800324 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700325}
326
Colin Cross54250902017-12-05 09:28:08 -0800327func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700328 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800329}
330
Jiyong Park8fd61922018-11-08 02:50:25 +0900331func (j *Module) DexJarFile() android.Path {
332 return j.dexJarFile
333}
334
Colin Cross54250902017-12-05 09:28:08 -0800335var _ android.SourceFileProducer = (*Module)(nil)
336
Colin Crossf506d872017-07-19 15:53:04 -0700337type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700338 HeaderJars() android.Paths
339 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700340 ResourceJars() android.Paths
341 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800342 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700343 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900344 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700345}
346
Jiyong Parkc678ad32018-04-10 13:07:10 +0900347type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800348 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
349 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900350}
351
Nan Zhangb2b33de2018-02-23 11:18:47 -0800352type SrcDependency interface {
353 CompiledSrcs() android.Paths
354 CompiledSrcJars() android.Paths
355}
356
357func (j *Module) CompiledSrcs() android.Paths {
358 return j.compiledJavaSrcs
359}
360
361func (j *Module) CompiledSrcJars() android.Paths {
362 return j.compiledSrcJars
363}
364
365var _ SrcDependency = (*Module)(nil)
366
Colin Cross89536d42017-07-07 14:35:50 -0700367func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
368 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
369 android.InitDefaultableModule(module)
370}
371
Colin Crossbe1da472017-07-07 15:59:46 -0700372type dependencyTag struct {
373 blueprint.BaseDependencyTag
374 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700375}
376
Colin Crossa4f08812018-10-02 22:03:40 -0700377type jniDependencyTag struct {
378 blueprint.BaseDependencyTag
379 target android.Target
380}
381
Colin Crossbe1da472017-07-07 15:59:46 -0700382var (
Colin Cross4b964c02018-10-15 16:18:06 -0700383 staticLibTag = dependencyTag{name: "staticlib"}
384 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800385 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700386 bootClasspathTag = dependencyTag{name: "bootclasspath"}
387 systemModulesTag = dependencyTag{name: "system modules"}
388 frameworkResTag = dependencyTag{name: "framework-res"}
389 frameworkApkTag = dependencyTag{name: "framework-apk"}
390 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800391 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700392 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
393 certificateTag = dependencyTag{name: "certificate"}
394 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700395)
Colin Cross2fe66872015-03-30 17:20:39 -0700396
Colin Crossfc3674a2017-09-18 17:41:52 -0700397type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700398 useModule, useFiles, useDefaultLibs, invalidVersion bool
399
Colin Cross86a60ae2018-05-29 14:44:55 -0700400 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700401 systemModules string
402
Colin Crossa97c5d32018-03-28 14:58:31 -0700403 frameworkResModule string
404
Colin Cross86a60ae2018-05-29 14:44:55 -0700405 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700406 aidl android.Path
407}
408
Colin Crossa4f08812018-10-02 22:03:40 -0700409type jniLib struct {
410 name string
411 path android.Path
412 target android.Target
413}
414
Colin Cross3144dfc2018-01-03 15:06:47 -0800415func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
416 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
417}
418
419func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
420 return j.shouldInstrument(ctx) &&
421 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
422 ctx.Config().UnbundledBuild())
423}
424
Colin Cross83bb3162018-06-25 15:48:06 -0700425func (j *Module) sdkVersion() string {
426 return String(j.deviceProperties.Sdk_version)
427}
428
429func (j *Module) minSdkVersion() string {
430 if j.deviceProperties.Min_sdk_version != nil {
431 return *j.deviceProperties.Min_sdk_version
432 }
433 return j.sdkVersion()
434}
435
Dan Willemsen419290a2018-10-31 15:28:47 -0700436func (j *Module) targetSdkVersion() string {
437 if j.deviceProperties.Target_sdk_version != nil {
438 return *j.deviceProperties.Target_sdk_version
439 }
440 return j.sdkVersion()
441}
442
Colin Crossbe1da472017-07-07 15:59:46 -0700443func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700444 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700445 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700446 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700447 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700448 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100449 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700450 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700451 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700452 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700453 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100454 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700455 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800456 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700457 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
458 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800459 }
Colin Crossbe1da472017-07-07 15:59:46 -0700460 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700461 } else if j.deviceProperties.System_modules == nil {
462 ctx.PropertyErrorf("no_standard_libs",
463 "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 +0100464 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700465 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700466 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100467 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700468 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800469 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800470 if ctx.ModuleName() == "android_stubs_current" ||
471 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700472 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700473 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800474 }
Colin Cross2fe66872015-03-30 17:20:39 -0700475 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700476
Colin Cross42d48b72018-08-29 14:10:52 -0700477 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
478 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700479
Colin Crossbe9cdb82019-01-21 21:37:16 -0800480 ctx.AddFarVariationDependencies([]blueprint.Variation{
481 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
482 }, pluginTag, j.properties.Plugins...)
483
Colin Cross6af17aa2017-09-20 12:59:05 -0700484 if j.hasSrcExt(".proto") {
485 protoDeps(ctx, &j.protoProperties)
486 }
Colin Cross93e85952017-08-15 13:34:18 -0700487
488 if j.hasSrcExt(".kt") {
489 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
490 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700491 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800492 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800493 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
494 }
Colin Cross93e85952017-08-15 13:34:18 -0700495 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800496
497 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700498 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800499 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700500}
501
502func hasSrcExt(srcs []string, ext string) bool {
503 for _, src := range srcs {
504 if filepath.Ext(src) == ext {
505 return true
506 }
507 }
508
509 return false
510}
511
Nan Zhang61eaedb2017-11-02 13:28:15 -0700512func shardPaths(paths android.Paths, shardSize int) []android.Paths {
513 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
514 for len(paths) > shardSize {
515 ret = append(ret, paths[0:shardSize])
516 paths = paths[shardSize:]
517 }
518 if len(paths) > 0 {
519 ret = append(ret, paths)
520 }
521 return ret
522}
523
Colin Cross6af17aa2017-09-20 12:59:05 -0700524func (j *Module) hasSrcExt(ext string) bool {
525 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700526}
527
Colin Cross46c9b8b2017-06-22 16:51:17 -0700528func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700529 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700530
Colin Crossebe1a512017-11-14 13:12:14 -0800531 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
532 aidlIncludes = append(aidlIncludes,
533 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
534 aidlIncludes = append(aidlIncludes,
535 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700536
Steven Moreland36b130f2019-02-05 17:02:55 -0800537 flags := []string{}
Steven Moreland667f6882018-07-26 12:55:08 -0700538
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700539 if aidlPreprocess.Valid() {
540 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700541 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700542 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700543 }
544
Colin Cross635c3b02016-05-18 15:37:25 -0700545 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800546 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700547 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800548 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700549 flags = append(flags, "-I"+src.String())
550 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700551
Martijn Coeneneab15642018-03-09 09:29:59 +0100552 if Bool(j.deviceProperties.Aidl.Generate_traces) {
553 flags = append(flags, "-t")
554 }
555
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100556 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
557 flags = append(flags, "--transaction_names")
558 }
559
Colin Crossf03c82b2015-04-13 13:53:40 -0700560 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700561}
562
Colin Cross32f676a2017-09-06 13:41:06 -0700563type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800564 classpath classpath
565 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700566 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800567 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700568 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700569 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700570 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700571 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800572 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700573 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700574 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700575 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700576 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800577 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800578
579 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700580}
Colin Cross2fe66872015-03-30 17:20:39 -0700581
Colin Cross54250902017-12-05 09:28:08 -0800582func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
583 for _, f := range dep.Srcs() {
584 if f.Ext() != ".jar" {
585 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
586 ctx.OtherModuleName(dep.(blueprint.Module)))
587 }
588 }
589}
590
Jiyong Park2d492942018-03-05 17:44:10 +0900591type linkType int
592
593const (
594 javaCore linkType = iota
595 javaSdk
596 javaSystem
597 javaPlatform
598)
599
Jiyong Park46f78fb2018-10-20 16:33:17 +0900600func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700601 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700602 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900603 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
604 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
605 name == "core-lambda-stubs":
606 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100607 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900608 return javaCore, false
609 case name == "android_system_stubs_current":
610 return javaSystem, true
611 case strings.HasPrefix(ver, "system_"):
612 return javaSystem, false
613 case name == "android_test_stubs_current":
614 return javaSystem, true
615 case strings.HasPrefix(ver, "test_"):
616 return javaPlatform, false
617 case name == "android_stubs_current":
618 return javaSdk, true
619 case ver == "current":
620 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700621 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900622 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700623 default:
624 if _, err := strconv.Atoi(ver); err != nil {
625 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
626 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900627 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900628 }
629}
630
Jiyong Park750e5572018-01-31 00:20:13 +0900631func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700632 if ctx.Host() {
633 return
634 }
635
Jiyong Park46f78fb2018-10-20 16:33:17 +0900636 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
637 if stubs {
638 return
639 }
640 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900641 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."
642
643 switch myLinkType {
644 case javaCore:
645 if otherLinkType != javaCore {
646 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 +0900647 ctx.OtherModuleName(to))
648 }
Jiyong Park2d492942018-03-05 17:44:10 +0900649 break
650 case javaSdk:
651 if otherLinkType != javaCore && otherLinkType != javaSdk {
652 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
653 ctx.OtherModuleName(to))
654 }
655 break
656 case javaSystem:
657 if otherLinkType == javaPlatform {
658 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
659 ctx.OtherModuleName(to))
660 }
661 break
662 case javaPlatform:
663 // no restriction on link-type
664 break
Jiyong Park750e5572018-01-31 00:20:13 +0900665 }
666}
667
Colin Cross32f676a2017-09-06 13:41:06 -0700668func (j *Module) collectDeps(ctx android.ModuleContext) deps {
669 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700670
Colin Cross300f0382018-03-06 13:11:51 -0800671 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700672 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800673 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700674 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800675 } else if sdkDep.useFiles {
676 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700677 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800678 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
679 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700680 }
681
Colin Crossd11fcda2017-10-23 17:59:01 -0700682 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700683 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700684 tag := ctx.OtherModuleDependencyTag(module)
685
Colin Crossa4f08812018-10-02 22:03:40 -0700686 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700687 // Handled by AndroidApp.collectAppDeps
688 return
689 }
690 if tag == certificateTag {
691 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700692 return
693 }
694
Jiyong Park750e5572018-01-31 00:20:13 +0900695 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700696 switch tag {
697 case bootClasspathTag, libTag, staticLibTag:
698 checkLinkType(ctx, j, to, tag.(dependencyTag))
699 }
Jiyong Park750e5572018-01-31 00:20:13 +0900700 }
Colin Cross54250902017-12-05 09:28:08 -0800701 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800702 case SdkLibraryDependency:
703 switch tag {
704 case libTag:
705 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
706 // names of sdk libs that are directly depended are exported
707 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
708 default:
709 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
710 }
Colin Cross54250902017-12-05 09:28:08 -0800711 case Dependency:
712 switch tag {
713 case bootClasspathTag:
714 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700715 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800716 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900717 // sdk lib names from dependencies are re-exported
718 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800719 case staticLibTag:
720 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
721 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
722 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700723 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900724 // sdk lib names from dependencies are re-exported
725 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800726 case pluginTag:
727 if plugin, ok := dep.(*Plugin); ok {
728 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
729 if plugin.pluginProperties.Processor_class != nil {
730 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
731 }
732 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
733 } else {
734 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
735 }
Colin Cross54250902017-12-05 09:28:08 -0800736 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100737 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800738 // framework.jar has a one-off dependency on the R.java and Manifest.java files
739 // generated by framework-res.apk
740 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
741 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800742 case frameworkApkTag:
743 if ctx.ModuleName() == "android_stubs_current" ||
744 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700745 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800746 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
747 // resource files out of there for aapt.
748 //
749 // Normally the package rule runs aapt, which includes the resource,
750 // but we're not running that in our package rule so just copy in the
751 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700752 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800753 }
Colin Cross54250902017-12-05 09:28:08 -0800754 case kotlinStdlibTag:
755 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800756 case kotlinAnnotationsTag:
757 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800758 }
759
760 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
761 case android.SourceFileProducer:
762 switch tag {
763 case libTag:
764 checkProducesJars(ctx, dep)
765 deps.classpath = append(deps.classpath, dep.Srcs()...)
766 case staticLibTag:
767 checkProducesJars(ctx, dep)
768 deps.classpath = append(deps.classpath, dep.Srcs()...)
769 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
770 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
771 case android.DefaultsDepTag, android.SourceDepTag:
772 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900773 case publicApiFileTag, systemApiFileTag, testApiFileTag:
774 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800775 default:
776 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
777 }
778 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700779 switch tag {
780 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700781 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700782 case systemModulesTag:
783 if deps.systemModules != nil {
784 panic("Found two system module dependencies")
785 }
786 sm := module.(*SystemModules)
787 if sm.outputFile == nil {
788 panic("Missing directory for system module dependency")
789 }
790 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700791 default:
792 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700793 }
Colin Crossec7a0422017-07-07 14:47:12 -0700794 }
Colin Cross2fe66872015-03-30 17:20:39 -0700795 })
796
Jiyong Park1be96912018-05-28 18:02:19 +0900797 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
798
Colin Cross32f676a2017-09-06 13:41:06 -0700799 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700800}
801
Colin Cross83bb3162018-06-25 15:48:06 -0700802func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700803 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800804 v := sdkContext.sdkVersion()
805 // For PDK builds, use the latest SDK version instead of "current"
806 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
807 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
808 latestSdkVersion := 0
809 if len(sdkVersions) > 0 {
810 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
811 }
812 v = strconv.Itoa(latestSdkVersion)
813 }
814
815 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700816 if err != nil {
817 ctx.PropertyErrorf("sdk_version", "%s", err)
818 }
Nan Zhang357466b2018-04-17 17:38:36 -0700819 if javaVersion != "" {
820 ret = javaVersion
821 } else if ctx.Device() && sdk <= 23 {
822 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900823 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700824 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700825 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700826 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
827 ret = "1.8"
828 } else {
829 ret = "1.9"
830 }
831
832 return ret
833}
834
Nan Zhanged19fc32017-10-19 13:06:22 -0700835func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700836
Colin Crossf03c82b2015-04-13 13:53:40 -0700837 var flags javaBuilderFlags
838
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100839 // javaVersion flag.
840 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
841
Nan Zhanged19fc32017-10-19 13:06:22 -0700842 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700843 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100844 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700845 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700846 }
Colin Cross6510f912017-11-29 00:27:14 -0800847 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700848 // Override the -g flag passed globally to remove local variable debug info to reduce
849 // disk and memory usage.
850 javacFlags = append(javacFlags, "-g:source,lines")
851 }
Colin Cross64162712017-08-08 13:17:59 -0700852
Colin Cross66548102018-06-19 22:47:35 -0700853 if ctx.Config().RunErrorProne() {
854 if config.ErrorProneClasspath == nil {
855 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
856 }
857
858 errorProneFlags := []string{
859 "-Xplugin:ErrorProne",
860 "${config.ErrorProneChecks}",
861 }
862 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
863
864 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
865 "'" + strings.Join(errorProneFlags, " ") + "'"
866 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800867 }
868
Nan Zhanged19fc32017-10-19 13:06:22 -0700869 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800870 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
871 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700872 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800873
Colin Crossbe9cdb82019-01-21 21:37:16 -0800874 flags.processor = strings.Join(deps.processorClasses, ",")
875
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100876 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800877 !Bool(j.properties.No_standard_libs) &&
878 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
879 // Give host-side tools a version of OpenJDK's standard libraries
880 // close to what they're targeting. As of Dec 2017, AOSP is only
881 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
882 //
883 // When building with OpenJDK 8, the following should have no
884 // effect since those jars would be available by default.
885 //
886 // When building with OpenJDK 9 but targeting a version < 1.8,
887 // putting them on the bootclasspath means that:
888 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
889 // b) references to existing APIs are not reinterpreted in an
890 // OpenJDK 9-specific way, eg. calls to subclasses of
891 // java.nio.Buffer as in http://b/70862583
892 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
893 flags.bootClasspath = append(flags.bootClasspath,
894 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
895 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800896 if Bool(j.properties.Use_tools_jar) {
897 flags.bootClasspath = append(flags.bootClasspath,
898 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
899 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800900 }
901
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100902 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800903 // Manually specify build directory in case it is not under the repo root.
904 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
905 // just adding a symlink under the root doesn't help.)
906 patchPaths := ".:" + ctx.Config().BuildDir()
907 classPath := flags.classpath.FormJavaClassPath("")
908 if classPath != "" {
909 patchPaths += ":" + classPath
910 }
911 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700912 }
913
Nan Zhanged19fc32017-10-19 13:06:22 -0700914 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700915 if deps.systemModules != nil {
916 flags.systemModules = append(flags.systemModules, deps.systemModules)
917 }
918
Nan Zhanged19fc32017-10-19 13:06:22 -0700919 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700920 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700921 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700922 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700923 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
924 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700925 }
926
Colin Cross81440082018-08-15 20:21:55 -0700927 if len(javacFlags) > 0 {
928 // optimization.
929 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
930 flags.javacFlags = "$javacFlags"
931 }
932
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 return flags
934}
Colin Crossc0b06f12015-04-08 13:03:43 -0700935
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800936func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700937
Colin Crossebe1a512017-11-14 13:12:14 -0800938 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700939
940 deps := j.collectDeps(ctx)
941 flags := j.collectBuilderFlags(ctx, deps)
942
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100943 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700944 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
945 }
Colin Cross8a497952019-03-05 22:25:09 -0800946 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700947 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800948 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700949 }
950
Colin Crossaf050172017-11-15 23:01:59 -0800951 srcFiles = j.genSources(ctx, srcFiles, flags)
952
953 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700954 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800955 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700956
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700957 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
958 // that IDEInfo struct will use
959 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
960
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800961 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800962 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800963 }
964
Colin Cross1ee23172017-10-18 14:44:18 -0700965 jarName := ctx.ModuleName() + ".jar"
966
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000967 javaSrcFiles := srcFiles.FilterByExt(".java")
968 var uniqueSrcFiles android.Paths
969 set := make(map[string]bool)
970 for _, v := range javaSrcFiles {
971 if _, found := set[v.String()]; !found {
972 set[v.String()] = true
973 uniqueSrcFiles = append(uniqueSrcFiles, v)
974 }
975 }
976
Colin Cross55f63ea2018-08-27 12:37:09 -0700977 var kotlinJars android.Paths
978
Colin Cross93e85952017-08-15 13:34:18 -0700979 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200980 // user defined kotlin flags.
981 kotlincFlags := j.properties.Kotlincflags
982 CheckKotlincFlags(ctx, kotlincFlags)
983
Colin Cross93e85952017-08-15 13:34:18 -0700984 // If there are kotlin files, compile them first but pass all the kotlin and java files
985 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
986 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200987 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700988 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200989 kotlincFlags = append(kotlincFlags, "-no-jdk")
990 }
991 if len(kotlincFlags) > 0 {
992 // optimization.
993 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
994 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700995 }
996
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000997 var kotlinSrcFiles android.Paths
998 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
999 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1000
Colin Crossafbb1732019-01-17 15:42:52 -08001001 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1002 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1003
1004 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1005 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1006
1007 if len(flags.processorPath) > 0 {
1008 // Use kapt for annotation processing
1009 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1010 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1011 srcJars = append(srcJars, kaptSrcJar)
1012 // Disable annotation processing in javac, it's already been handled by kapt
1013 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001014 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001015 }
Colin Cross93e85952017-08-15 13:34:18 -07001016
Colin Cross1ee23172017-10-18 14:44:18 -07001017 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001018 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001019 if ctx.Failed() {
1020 return
1021 }
1022
1023 // Make javac rule depend on the kotlinc rule
1024 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001025
Colin Cross93e85952017-08-15 13:34:18 -07001026 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001027 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001028 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001029 }
1030
Colin Cross55f63ea2018-08-27 12:37:09 -07001031 jars := append(android.Paths(nil), kotlinJars...)
1032
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001033 // Store the list of .java files that was passed to javac
1034 j.compiledJavaSrcs = uniqueSrcFiles
1035 j.compiledSrcJars = srcJars
1036
Nan Zhang61eaedb2017-11-02 13:28:15 -07001037 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001038 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001039 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1040 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001041 // Formerly, there was a check here that prevented annotation processors
1042 // from being used when sharding was enabled, as some annotation processors
1043 // do not function correctly in sharded environments. It was removed to
1044 // allow for the use of annotation processors that do function correctly
1045 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001046 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001047 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001048 if ctx.Failed() {
1049 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001050 }
1051 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001052 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001053 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001054 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001055 // If error-prone is enabled, add an additional rule to compile the java files into
1056 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001057 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001058 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1059 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001060 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001061 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001062 extraJarDeps = append(extraJarDeps, errorprone)
1063 }
1064
Nan Zhang61eaedb2017-11-02 13:28:15 -07001065 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001066 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001067 shardSize := int(*(j.properties.Javac_shard_size))
1068 var shardSrcs []android.Paths
1069 if len(uniqueSrcFiles) > 0 {
1070 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1071 for idx, shardSrc := range shardSrcs {
1072 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1073 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1074 jars = append(jars, classes)
1075 }
1076 }
1077 if len(srcJars) > 0 {
1078 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1079 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1080 jars = append(jars, classes)
1081 }
1082 } else {
1083 classes := android.PathForModuleOut(ctx, "javac", jarName)
1084 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1085 jars = append(jars, classes)
1086 }
Colin Crossd6891432017-09-27 17:39:56 -07001087 if ctx.Failed() {
1088 return
1089 }
Colin Cross2fe66872015-03-30 17:20:39 -07001090 }
1091
Colin Crosscedd4762018-09-13 11:26:19 -07001092 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1093 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001094 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1095
1096 var resArgs []string
1097 var resDeps android.Paths
1098
1099 resArgs = append(resArgs, dirArgs...)
1100 resDeps = append(resDeps, dirDeps...)
1101
1102 resArgs = append(resArgs, fileArgs...)
1103 resDeps = append(resDeps, fileDeps...)
1104
Colin Crossff3ae9d2018-04-10 16:15:18 -07001105 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001106 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001107 resArgs = append(resArgs, srcArgs...)
1108 resDeps = append(resDeps, srcDeps...)
1109 }
Colin Cross40a36712017-09-27 17:41:35 -07001110
1111 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001112 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001113 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001114 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001115 if ctx.Failed() {
1116 return
1117 }
1118 }
1119
Colin Cross331a1212018-08-15 20:40:52 -07001120 if len(deps.staticResourceJars) > 0 {
1121 var jars android.Paths
1122 if j.resourceJar != nil {
1123 jars = append(jars, j.resourceJar)
1124 }
1125 jars = append(jars, deps.staticResourceJars...)
1126
1127 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1128 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1129 false, nil, nil)
1130 j.resourceJar = combinedJar
1131 }
1132
Colin Cross32f676a2017-09-06 13:41:06 -07001133 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001134
Colin Cross094054a2018-10-17 15:10:48 -07001135 manifest := j.overrideManifest
1136 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001137 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001138 }
Colin Cross635acc92017-09-12 22:50:46 -07001139
Colin Cross8a497952019-03-05 22:25:09 -08001140 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001141 if len(services) > 0 {
1142 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1143 var zipargs []string
1144 for _, file := range services {
1145 serviceFile := file.String()
1146 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1147 }
1148 ctx.Build(pctx, android.BuildParams{
1149 Rule: zip,
1150 Output: servicesJar,
1151 Implicits: services,
1152 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001153 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001154 },
1155 })
1156 jars = append(jars, servicesJar)
1157 }
1158
Colin Cross0a6e0072017-08-30 14:24:55 -07001159 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001160 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001161 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001162
1163 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001164 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1165 // Optimization: skip the combine step if there is nothing to do
1166 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1167 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1168 // any if len(jars) == 1.
1169 outputFile = moduleOutPath
1170 } else {
1171 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1172 ctx.Build(pctx, android.BuildParams{
1173 Rule: android.Cp,
1174 Input: jars[0],
1175 Output: combinedJar,
1176 })
1177 outputFile = combinedJar
1178 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001179 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001180 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001181 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001182 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001183 outputFile = combinedJar
1184 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001185
Colin Cross331a1212018-08-15 20:40:52 -07001186 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001187 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001188 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001189 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001190 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001191 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001192
1193 // jarjar resource jar if necessary
1194 if j.resourceJar != nil {
1195 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001196 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001197 j.resourceJar = resourceJarJarFile
1198 }
1199
Colin Cross0a6e0072017-08-30 14:24:55 -07001200 if ctx.Failed() {
1201 return
1202 }
1203 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001204 j.implementationJarFile = outputFile
1205 if j.headerJarFile == nil {
1206 j.headerJarFile = j.implementationJarFile
1207 }
Colin Cross2fe66872015-03-30 17:20:39 -07001208
Colin Cross6510f912017-11-29 00:27:14 -08001209 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001210 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1211 j.properties.Instrument = true
1212 }
1213 }
1214
Colin Cross3144dfc2018-01-03 15:06:47 -08001215 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001216 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1217 }
1218
Colin Cross331a1212018-08-15 20:40:52 -07001219 // merge implementation jar with resources if necessary
1220 implementationAndResourcesJar := outputFile
1221 if j.resourceJar != nil {
1222 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1223 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1224 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1225 false, nil, nil)
1226 implementationAndResourcesJar = combinedJar
1227 }
1228
1229 j.implementationAndResourcesJar = implementationAndResourcesJar
1230
Colin Cross9ae1b922018-06-26 17:59:05 -07001231 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001232 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001233 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001234 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001235 if ctx.Failed() {
1236 return
1237 }
Colin Cross331a1212018-08-15 20:40:52 -07001238
Colin Cross8faf8fc2019-01-16 15:15:52 -08001239 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001240 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1241 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001242
Colin Cross331a1212018-08-15 20:40:52 -07001243 // merge dex jar with resources if necessary
1244 if j.resourceJar != nil {
1245 jars := android.Paths{dexOutputFile, j.resourceJar}
1246 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1247 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1248 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001249 if j.deviceProperties.UncompressDex {
1250 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1251 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1252 dexOutputFile = combinedAlignedJar
1253 } else {
1254 dexOutputFile = combinedJar
1255 }
Colin Cross331a1212018-08-15 20:40:52 -07001256 }
1257
1258 j.dexJarFile = dexOutputFile
1259
Colin Cross8faf8fc2019-01-16 15:15:52 -08001260 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001261 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1262
1263 j.maybeStrippedDexJarFile = dexOutputFile
1264
Colin Cross3063b782018-08-15 11:19:12 -07001265 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001266
1267 if ctx.Failed() {
1268 return
1269 }
Colin Cross331a1212018-08-15 20:40:52 -07001270 } else {
1271 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001272 }
Colin Cross331a1212018-08-15 20:40:52 -07001273
Colin Crossb7a63242015-04-16 14:09:14 -07001274 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001275
1276 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1277 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001278}
1279
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001280// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1281// since some of these flags may be used internally.
1282func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1283 for _, flag := range flags {
1284 flag = strings.TrimSpace(flag)
1285
1286 if !strings.HasPrefix(flag, "-") {
1287 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1288 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1289 ctx.PropertyErrorf("kotlincflags",
1290 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1291 } else if inList(flag, config.KotlincIllegalFlags) {
1292 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1293 } else if flag == "-include-runtime" {
1294 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1295 } else {
1296 args := strings.Split(flag, " ")
1297 if args[0] == "-kotlin-home" {
1298 ctx.PropertyErrorf("kotlincflags",
1299 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1300 }
1301 }
1302 }
1303}
1304
Colin Cross8eadbf02017-10-24 17:46:00 -07001305func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001306 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001307
1308 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001309 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001310 // Compile java sources into turbine.jar.
1311 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1312 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1313 if ctx.Failed() {
1314 return nil
1315 }
1316 jars = append(jars, turbineJar)
1317 }
1318
Colin Cross55f63ea2018-08-27 12:37:09 -07001319 jars = append(jars, extraJars...)
1320
Nan Zhanged19fc32017-10-19 13:06:22 -07001321 // Combine any static header libraries into classes-header.jar. If there is only
1322 // one input jar this step will be skipped.
1323 var headerJar android.Path
1324 jars = append(jars, deps.staticHeaderJars...)
1325
Colin Cross5c6ecc12017-10-23 18:12:27 -07001326 // we cannot skip the combine step for now if there is only one jar
1327 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1328 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001329 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1330 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001331 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001332
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001333 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001334 // Transform classes.jar into classes-jarjar.jar
1335 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001336 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001337 headerJar = jarjarFile
1338 if ctx.Failed() {
1339 return nil
1340 }
1341 }
1342
1343 return headerJar
1344}
1345
Colin Crosscb933592017-11-22 13:49:43 -08001346func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001347 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001348
Colin Cross7a3139e2017-12-19 13:57:50 -08001349 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001350
Colin Cross84c38822018-01-03 15:59:46 -08001351 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001352 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1353
1354 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1355
1356 j.jacocoReportClassesFile = jacocoReportClassesFile
1357
1358 return instrumentedJar
1359}
1360
albaltai36ff7dc2018-12-25 14:35:23 +08001361var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001362
Nan Zhanged19fc32017-10-19 13:06:22 -07001363func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001364 if j.headerJarFile == nil {
1365 return nil
1366 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001367 return android.Paths{j.headerJarFile}
1368}
1369
1370func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001371 if j.implementationJarFile == nil {
1372 return nil
1373 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001374 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001375}
1376
Colin Crossf24a22a2019-01-31 14:12:44 -08001377func (j *Module) DexJar() android.Path {
1378 return j.dexJarFile
1379}
1380
Colin Cross331a1212018-08-15 20:40:52 -07001381func (j *Module) ResourceJars() android.Paths {
1382 if j.resourceJar == nil {
1383 return nil
1384 }
1385 return android.Paths{j.resourceJar}
1386}
1387
1388func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001389 if j.implementationAndResourcesJar == nil {
1390 return nil
1391 }
Colin Cross331a1212018-08-15 20:40:52 -07001392 return android.Paths{j.implementationAndResourcesJar}
1393}
1394
Colin Cross46c9b8b2017-06-22 16:51:17 -07001395func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001396 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001397 return j.exportAidlIncludeDirs
1398}
1399
Jiyong Park1be96912018-05-28 18:02:19 +09001400func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001401 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001402 return j.exportedSdkLibs
1403}
1404
Colin Cross46c9b8b2017-06-22 16:51:17 -07001405var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001406
Colin Cross46c9b8b2017-06-22 16:51:17 -07001407func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001408 return j.logtagsSrcs
1409}
1410
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001411// Collect information for opening IDE project files in java/jdeps.go.
1412func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1413 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1414 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1415 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001416 if j.expandJarjarRules != nil {
1417 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001418 }
1419}
1420
1421func (j *Module) CompilerDeps() []string {
1422 jdeps := []string{}
1423 jdeps = append(jdeps, j.properties.Libs...)
1424 jdeps = append(jdeps, j.properties.Static_libs...)
1425 return jdeps
1426}
1427
Colin Cross2fe66872015-03-30 17:20:39 -07001428//
1429// Java libraries (.jar file)
1430//
1431
Colin Crossf506d872017-07-19 15:53:04 -07001432type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001433 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001434}
1435
Colin Cross42be7612019-02-21 18:12:14 -08001436func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001437 // Store uncompressed (and do not strip) dex files from boot class path jars.
1438 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1439 return true
1440 }
1441
1442 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001443 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001444 return true
1445 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001446 if ctx.Config().UncompressPrivAppDex() &&
1447 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1448 return true
1449 }
1450
Colin Cross2fc72f62018-12-21 12:59:54 -08001451 return false
1452}
1453
Colin Crossf506d872017-07-19 15:53:04 -07001454func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001455 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1456 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001457 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001458 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001459 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001460 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001461
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001462 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001463 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1464 ctx.ModuleName()+".jar", j.outputFile)
1465 }
Colin Crossb7a63242015-04-16 14:09:14 -07001466}
1467
Colin Crossf506d872017-07-19 15:53:04 -07001468func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001469 j.deps(ctx)
1470}
1471
Colin Cross1b16b0e2019-02-12 14:41:32 -08001472// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1473//
1474// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1475// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1476// as a `static_libs` dependency of another module.
1477//
1478// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1479// a device.
1480//
1481// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1482// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001483func LibraryFactory() android.Module {
1484 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001485
Colin Cross9ae1b922018-06-26 17:59:05 -07001486 module.AddProperties(
1487 &module.Module.properties,
1488 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001489 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001490 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001491
Colin Cross9ae1b922018-06-26 17:59:05 -07001492 InitJavaModule(module, android.HostAndDeviceSupported)
1493 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001494}
1495
Colin Cross1b16b0e2019-02-12 14:41:32 -08001496// java_library_static is an obsolete alias for java_library.
1497func LibraryStaticFactory() android.Module {
1498 return LibraryFactory()
1499}
1500
1501// java_library_host builds and links sources into a `.jar` file for the host.
1502//
1503// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1504// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001505func LibraryHostFactory() android.Module {
1506 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001507
Colin Cross6af17aa2017-09-20 12:59:05 -07001508 module.AddProperties(
1509 &module.Module.properties,
1510 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001511
Colin Cross9ae1b922018-06-26 17:59:05 -07001512 module.Module.properties.Installable = proptools.BoolPtr(true)
1513
Colin Cross89536d42017-07-07 14:35:50 -07001514 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001515 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001516}
1517
1518//
Colin Crossb628ea52018-08-14 16:42:33 -07001519// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001520//
1521
1522type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001523 // list of compatibility suites (for example "cts", "vts") that the module should be
1524 // installed into.
1525 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001526
1527 // the name of the test configuration (for example "AndroidTest.xml") that should be
1528 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001529 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001530
Jack He33338892018-09-19 02:21:28 -07001531 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1532 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001533 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001534
Colin Crossd96ca352018-08-10 16:06:24 -07001535 // list of files or filegroup modules that provide data that should be installed alongside
1536 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001537 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001538}
1539
Paul Duffin42df1442019-03-20 12:45:53 +00001540type testHelperLibraryProperties struct {
1541 // list of compatibility suites (for example "cts", "vts") that the module should be
1542 // installed into.
1543 Test_suites []string `android:"arch_variant"`
1544}
1545
Colin Cross05638fc2018-04-09 18:40:24 -07001546type Test struct {
1547 Library
1548
1549 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001550
1551 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001552 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001553}
1554
Paul Duffin42df1442019-03-20 12:45:53 +00001555type TestHelperLibrary struct {
1556 Library
1557
1558 testHelperLibraryProperties testHelperLibraryProperties
1559}
1560
Colin Cross303e21f2018-08-07 16:49:25 -07001561func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001562 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 -08001563 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001564
1565 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001566}
1567
Paul Duffin42df1442019-03-20 12:45:53 +00001568func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1569 j.Library.GenerateAndroidBuildActions(ctx)
1570}
1571
Colin Cross1b16b0e2019-02-12 14:41:32 -08001572// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1573// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1574//
1575// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1576// compiled against the device bootclasspath.
1577//
1578// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1579// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001580func TestFactory() android.Module {
1581 module := &Test{}
1582
1583 module.AddProperties(
1584 &module.Module.properties,
1585 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001586 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001587 &module.Module.protoProperties,
1588 &module.testProperties)
1589
Colin Cross9ae1b922018-06-26 17:59:05 -07001590 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001591 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001592
Colin Cross05638fc2018-04-09 18:40:24 -07001593 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001594 return module
1595}
1596
Paul Duffin42df1442019-03-20 12:45:53 +00001597// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1598func TestHelperLibraryFactory() android.Module {
1599 module := &TestHelperLibrary{}
1600
1601 module.AddProperties(
1602 &module.Module.properties,
1603 &module.Module.deviceProperties,
1604 &module.Module.dexpreoptProperties,
1605 &module.Module.protoProperties,
1606 &module.testHelperLibraryProperties)
1607
1608 InitJavaModule(module, android.HostAndDeviceSupported)
1609 return module
1610}
1611
Colin Cross1b16b0e2019-02-12 14:41:32 -08001612// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1613// allow running the test with `atest` or a `TEST_MAPPING` file.
1614//
1615// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1616// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001617func TestHostFactory() android.Module {
1618 module := &Test{}
1619
1620 module.AddProperties(
1621 &module.Module.properties,
1622 &module.Module.protoProperties,
1623 &module.testProperties)
1624
Colin Cross9ae1b922018-06-26 17:59:05 -07001625 module.Module.properties.Installable = proptools.BoolPtr(true)
1626
Colin Cross05638fc2018-04-09 18:40:24 -07001627 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001628 return module
1629}
1630
1631//
Colin Cross2fe66872015-03-30 17:20:39 -07001632// Java Binaries (.jar file plus wrapper script)
1633//
1634
Colin Crossf506d872017-07-19 15:53:04 -07001635type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001636 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001637 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001638
1639 // Name of the class containing main to be inserted into the manifest as Main-Class.
1640 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001641}
1642
Colin Crossf506d872017-07-19 15:53:04 -07001643type Binary struct {
1644 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001645
Colin Crossf506d872017-07-19 15:53:04 -07001646 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001647
Colin Cross6b4a32d2017-12-05 13:42:45 -08001648 isWrapperVariant bool
1649
Colin Crossc3315992017-12-08 19:12:36 -08001650 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001651 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001652}
1653
Alex Light24237172017-10-26 09:46:21 -07001654func (j *Binary) HostToolPath() android.OptionalPath {
1655 return android.OptionalPathForPath(j.binaryFile)
1656}
1657
Colin Crossf506d872017-07-19 15:53:04 -07001658func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001659 if ctx.Arch().ArchType == android.Common {
1660 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001661 if j.binaryProperties.Main_class != nil {
1662 if j.properties.Manifest != nil {
1663 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1664 }
1665 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1666 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1667 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1668 }
1669
Colin Cross6b4a32d2017-12-05 13:42:45 -08001670 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001671 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001672 // Handle the binary wrapper
1673 j.isWrapperVariant = true
1674
Colin Cross366938f2017-12-11 16:29:02 -08001675 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001676 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001677 } else {
1678 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1679 }
1680
1681 // Depend on the installed jar so that the wrapper doesn't get executed by
1682 // another build rule before the jar has been installed.
1683 jarFile := ctx.PrimaryModule().(*Binary).installFile
1684
1685 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1686 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001687 }
Colin Cross2fe66872015-03-30 17:20:39 -07001688}
1689
Colin Crossf506d872017-07-19 15:53:04 -07001690func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001691 if ctx.Arch().ArchType == android.Common {
1692 j.deps(ctx)
1693 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001694}
1695
Colin Cross1b16b0e2019-02-12 14:41:32 -08001696// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1697// as well.
1698//
1699// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1700// compiled against the device bootclasspath.
1701//
1702// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1703// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001704func BinaryFactory() android.Module {
1705 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001706
Colin Cross36242852017-06-23 15:06:31 -07001707 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001708 &module.Module.properties,
1709 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001710 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001711 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001712 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001713
Colin Cross9ae1b922018-06-26 17:59:05 -07001714 module.Module.properties.Installable = proptools.BoolPtr(true)
1715
Colin Cross6b4a32d2017-12-05 13:42:45 -08001716 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1717 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001718 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001719}
1720
Colin Cross1b16b0e2019-02-12 14:41:32 -08001721// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1722//
1723// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1724// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001725func BinaryHostFactory() android.Module {
1726 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001727
Colin Cross36242852017-06-23 15:06:31 -07001728 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001729 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001730 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001731 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001732
Colin Cross9ae1b922018-06-26 17:59:05 -07001733 module.Module.properties.Installable = proptools.BoolPtr(true)
1734
Colin Cross6b4a32d2017-12-05 13:42:45 -08001735 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1736 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001737 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001738}
1739
1740//
1741// Java prebuilts
1742//
1743
Colin Cross74d73e22017-08-02 11:05:49 -07001744type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001745 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001746
Nan Zhangea568a42017-11-08 21:20:04 -08001747 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001748
1749 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001750
1751 // List of shared java libs that this module has dependencies to
1752 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001753
1754 // List of files to remove from the jar file(s)
1755 Exclude_files []string
1756
1757 // List of directories to remove from the jar file(s)
1758 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001759
1760 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001761 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001762}
1763
1764type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001765 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001766 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001767 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001768
Colin Cross74d73e22017-08-02 11:05:49 -07001769 properties ImportProperties
1770
Colin Cross0a6e0072017-08-30 14:24:55 -07001771 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001772 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001773}
1774
Colin Cross83bb3162018-06-25 15:48:06 -07001775func (j *Import) sdkVersion() string {
1776 return String(j.properties.Sdk_version)
1777}
1778
1779func (j *Import) minSdkVersion() string {
1780 return j.sdkVersion()
1781}
1782
Colin Cross74d73e22017-08-02 11:05:49 -07001783func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001784 return &j.prebuilt
1785}
1786
Colin Cross74d73e22017-08-02 11:05:49 -07001787func (j *Import) PrebuiltSrcs() []string {
1788 return j.properties.Jars
1789}
1790
1791func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001792 return j.prebuilt.Name(j.ModuleBase.Name())
1793}
1794
Colin Cross74d73e22017-08-02 11:05:49 -07001795func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001796 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001797}
1798
Colin Cross74d73e22017-08-02 11:05:49 -07001799func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001800 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001801
Nan Zhang4c819fb2018-08-27 18:31:46 -07001802 jarName := ctx.ModuleName() + ".jar"
1803 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001804 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1805 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001806 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001807 inputFile := outputFile
1808 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1809 TransformJetifier(ctx, outputFile, inputFile)
1810 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001811 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001812
1813 ctx.VisitDirectDeps(func(module android.Module) {
1814 otherName := ctx.OtherModuleName(module)
1815 tag := ctx.OtherModuleDependencyTag(module)
1816
1817 switch dep := module.(type) {
1818 case Dependency:
1819 switch tag {
1820 case libTag, staticLibTag:
1821 // sdk lib names from dependencies are re-exported
1822 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1823 }
1824 case SdkLibraryDependency:
1825 switch tag {
1826 case libTag:
1827 // names of sdk libs that are directly depended are exported
1828 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1829 }
1830 }
1831 })
1832
1833 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001834 if Bool(j.properties.Installable) {
1835 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1836 ctx.ModuleName()+".jar", outputFile)
1837 }
Colin Cross2fe66872015-03-30 17:20:39 -07001838}
1839
Colin Cross74d73e22017-08-02 11:05:49 -07001840var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001841
Nan Zhanged19fc32017-10-19 13:06:22 -07001842func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001843 if j.combinedClasspathFile == nil {
1844 return nil
1845 }
Colin Cross37f6d792018-07-12 12:28:41 -07001846 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001847}
1848
1849func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001850 if j.combinedClasspathFile == nil {
1851 return nil
1852 }
Colin Cross37f6d792018-07-12 12:28:41 -07001853 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001854}
1855
Colin Cross331a1212018-08-15 20:40:52 -07001856func (j *Import) ResourceJars() android.Paths {
1857 return nil
1858}
1859
1860func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001861 if j.combinedClasspathFile == nil {
1862 return nil
1863 }
Colin Cross331a1212018-08-15 20:40:52 -07001864 return android.Paths{j.combinedClasspathFile}
1865}
1866
Colin Crossf24a22a2019-01-31 14:12:44 -08001867func (j *Import) DexJar() android.Path {
1868 return nil
1869}
1870
Colin Cross74d73e22017-08-02 11:05:49 -07001871func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001872 return nil
1873}
1874
Jiyong Park1be96912018-05-28 18:02:19 +09001875func (j *Import) ExportedSdkLibs() []string {
1876 return j.exportedSdkLibs
1877}
1878
albaltai36ff7dc2018-12-25 14:35:23 +08001879// Add compile time check for interface implementation
1880var _ android.IDEInfo = (*Import)(nil)
1881var _ android.IDECustomizedModuleName = (*Import)(nil)
1882
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001883// Collect information for opening IDE project files in java/jdeps.go.
1884const (
1885 removedPrefix = "prebuilt_"
1886)
1887
1888func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1889 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1890}
1891
1892func (j *Import) IDECustomizedModuleName() string {
1893 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1894 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1895 // solution to get the Import name.
1896 name := j.Name()
1897 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001898 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001899 }
1900 return name
1901}
1902
Colin Cross74d73e22017-08-02 11:05:49 -07001903var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001904
Colin Cross1b16b0e2019-02-12 14:41:32 -08001905// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1906//
1907// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1908// compiled against an Android classpath.
1909//
1910// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1911// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001912func ImportFactory() android.Module {
1913 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001914
Colin Cross74d73e22017-08-02 11:05:49 -07001915 module.AddProperties(&module.properties)
1916
1917 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001918 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001919 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001920}
1921
Colin Cross1b16b0e2019-02-12 14:41:32 -08001922// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1923// module.
1924//
1925// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1926// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001927func ImportFactoryHost() android.Module {
1928 module := &Import{}
1929
1930 module.AddProperties(&module.properties)
1931
1932 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001933 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001934 return module
1935}
1936
Colin Cross42be7612019-02-21 18:12:14 -08001937// dex_import module
1938
1939type DexImportProperties struct {
1940 Jars []string
1941}
1942
1943type DexImport struct {
1944 android.ModuleBase
1945 android.DefaultableModuleBase
1946 prebuilt android.Prebuilt
1947
1948 properties DexImportProperties
1949
1950 dexJarFile android.Path
1951 maybeStrippedDexJarFile android.Path
1952
1953 dexpreopter
1954}
1955
1956func (j *DexImport) Prebuilt() *android.Prebuilt {
1957 return &j.prebuilt
1958}
1959
1960func (j *DexImport) PrebuiltSrcs() []string {
1961 return j.properties.Jars
1962}
1963
1964func (j *DexImport) Name() string {
1965 return j.prebuilt.Name(j.ModuleBase.Name())
1966}
1967
1968func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1969 android.ExtractSourcesDeps(ctx, j.properties.Jars)
1970}
1971
1972func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1973 if len(j.properties.Jars) != 1 {
1974 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1975 }
1976
1977 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1978 j.dexpreopter.isInstallable = true
1979 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1980
1981 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1982 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1983
1984 if j.dexpreopter.uncompressedDex {
1985 rule := android.NewRuleBuilder()
1986
1987 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1988 rule.Temporary(temporary)
1989
1990 // use zip2zip to uncompress classes*.dex files
1991 rule.Command().
1992 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
1993 FlagWithInput("-i ", inputJar).
1994 FlagWithOutput("-o ", temporary).
1995 FlagWithArg("-0 ", "'classes*.dex'")
1996
1997 // use zipalign to align uncompressed classes*.dex files
1998 rule.Command().
1999 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2000 Flag("-f").
2001 Text("4").
2002 Input(temporary).
2003 Output(dexOutputFile)
2004
2005 rule.DeleteTemporaryFiles()
2006
2007 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2008 } else {
2009 ctx.Build(pctx, android.BuildParams{
2010 Rule: android.Cp,
2011 Input: inputJar,
2012 Output: dexOutputFile,
2013 })
2014 }
2015
2016 j.dexJarFile = dexOutputFile
2017
2018 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2019
2020 j.maybeStrippedDexJarFile = dexOutputFile
2021
2022 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2023 ctx.ModuleName()+".jar", dexOutputFile)
2024}
2025
2026func (j *DexImport) DexJar() android.Path {
2027 return j.dexJarFile
2028}
2029
2030// dex_import imports a `.jar` file containing classes.dex files.
2031//
2032// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2033// to the device.
2034func DexImportFactory() android.Module {
2035 module := &DexImport{}
2036
2037 module.AddProperties(&module.properties)
2038
2039 android.InitPrebuiltModule(module, &module.properties.Jars)
2040 InitJavaModule(module, android.DeviceSupported)
2041 return module
2042}
2043
Colin Cross89536d42017-07-07 14:35:50 -07002044//
2045// Defaults
2046//
2047type Defaults struct {
2048 android.ModuleBase
2049 android.DefaultsModuleBase
2050}
2051
2052func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2053}
2054
Colin Cross1b16b0e2019-02-12 14:41:32 -08002055// java_defaults provides a set of properties that can be inherited by other java or android modules.
2056//
2057// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2058// property in the defaults module that exists in the depending module will be prepended to the depending module's
2059// value for that property.
2060//
2061// Example:
2062//
2063// java_defaults {
2064// name: "example_defaults",
2065// srcs: ["common/**/*.java"],
2066// javacflags: ["-Xlint:all"],
2067// aaptflags: ["--auto-add-overlay"],
2068// }
2069//
2070// java_library {
2071// name: "example",
2072// defaults: ["example_defaults"],
2073// srcs: ["example/**/*.java"],
2074// }
2075//
2076// is functionally identical to:
2077//
2078// java_library {
2079// name: "example",
2080// srcs: [
2081// "common/**/*.java",
2082// "example/**/*.java",
2083// ],
2084// javacflags: ["-Xlint:all"],
2085// }
Colin Cross89536d42017-07-07 14:35:50 -07002086func defaultsFactory() android.Module {
2087 return DefaultsFactory()
2088}
2089
2090func DefaultsFactory(props ...interface{}) android.Module {
2091 module := &Defaults{}
2092
2093 module.AddProperties(props...)
2094 module.AddProperties(
2095 &CompilerProperties{},
2096 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002097 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002098 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002099 &aaptProperties{},
2100 &androidLibraryProperties{},
2101 &appProperties{},
2102 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002103 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002104 &ImportProperties{},
2105 &AARImportProperties{},
2106 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002107 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002108 )
2109
2110 android.InitDefaultsModule(module)
2111
2112 return module
2113}
Nan Zhangea568a42017-11-08 21:20:04 -08002114
2115var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002116var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002117var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002118var inList = android.InList