blob: 0417dee825135448e0460f368fe92d28f4e75718 [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 Crossfe17f6f2019-03-28 19:30:56 -0700484 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700485 if j.hasSrcExt(".proto") {
486 protoDeps(ctx, &j.protoProperties)
487 }
Colin Cross93e85952017-08-15 13:34:18 -0700488
489 if j.hasSrcExt(".kt") {
490 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
491 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700492 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800493 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800494 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
495 }
Colin Cross93e85952017-08-15 13:34:18 -0700496 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800497
498 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700499 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800500 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700501}
502
503func hasSrcExt(srcs []string, ext string) bool {
504 for _, src := range srcs {
505 if filepath.Ext(src) == ext {
506 return true
507 }
508 }
509
510 return false
511}
512
Nan Zhang61eaedb2017-11-02 13:28:15 -0700513func shardPaths(paths android.Paths, shardSize int) []android.Paths {
514 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
515 for len(paths) > shardSize {
516 ret = append(ret, paths[0:shardSize])
517 paths = paths[shardSize:]
518 }
519 if len(paths) > 0 {
520 ret = append(ret, paths)
521 }
522 return ret
523}
524
Colin Cross6af17aa2017-09-20 12:59:05 -0700525func (j *Module) hasSrcExt(ext string) bool {
526 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700527}
528
Colin Cross46c9b8b2017-06-22 16:51:17 -0700529func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700530 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700531
Colin Crossebe1a512017-11-14 13:12:14 -0800532 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
533 aidlIncludes = append(aidlIncludes,
534 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
535 aidlIncludes = append(aidlIncludes,
536 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700537
Steven Moreland36b130f2019-02-05 17:02:55 -0800538 flags := []string{}
Steven Moreland667f6882018-07-26 12:55:08 -0700539
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700540 if aidlPreprocess.Valid() {
541 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700542 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700543 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700544 }
545
Colin Cross635c3b02016-05-18 15:37:25 -0700546 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800547 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700548 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800549 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700550 flags = append(flags, "-I"+src.String())
551 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700552
Martijn Coeneneab15642018-03-09 09:29:59 +0100553 if Bool(j.deviceProperties.Aidl.Generate_traces) {
554 flags = append(flags, "-t")
555 }
556
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100557 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
558 flags = append(flags, "--transaction_names")
559 }
560
Colin Crossf03c82b2015-04-13 13:53:40 -0700561 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700562}
563
Colin Cross32f676a2017-09-06 13:41:06 -0700564type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800565 classpath classpath
566 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700567 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800568 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700569 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700570 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700571 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700572 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800573 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700574 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700575 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700576 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700577 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800578 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800579
580 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700581}
Colin Cross2fe66872015-03-30 17:20:39 -0700582
Colin Cross54250902017-12-05 09:28:08 -0800583func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
584 for _, f := range dep.Srcs() {
585 if f.Ext() != ".jar" {
586 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
587 ctx.OtherModuleName(dep.(blueprint.Module)))
588 }
589 }
590}
591
Jiyong Park2d492942018-03-05 17:44:10 +0900592type linkType int
593
594const (
595 javaCore linkType = iota
596 javaSdk
597 javaSystem
598 javaPlatform
599)
600
Jiyong Park46f78fb2018-10-20 16:33:17 +0900601func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700602 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700603 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900604 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
605 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
606 name == "core-lambda-stubs":
607 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100608 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900609 return javaCore, false
610 case name == "android_system_stubs_current":
611 return javaSystem, true
612 case strings.HasPrefix(ver, "system_"):
613 return javaSystem, false
614 case name == "android_test_stubs_current":
615 return javaSystem, true
616 case strings.HasPrefix(ver, "test_"):
617 return javaPlatform, false
618 case name == "android_stubs_current":
619 return javaSdk, true
620 case ver == "current":
621 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700622 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900623 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700624 default:
625 if _, err := strconv.Atoi(ver); err != nil {
626 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
627 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900628 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900629 }
630}
631
Jiyong Park750e5572018-01-31 00:20:13 +0900632func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700633 if ctx.Host() {
634 return
635 }
636
Jiyong Park46f78fb2018-10-20 16:33:17 +0900637 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
638 if stubs {
639 return
640 }
641 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900642 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."
643
644 switch myLinkType {
645 case javaCore:
646 if otherLinkType != javaCore {
647 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 +0900648 ctx.OtherModuleName(to))
649 }
Jiyong Park2d492942018-03-05 17:44:10 +0900650 break
651 case javaSdk:
652 if otherLinkType != javaCore && otherLinkType != javaSdk {
653 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
654 ctx.OtherModuleName(to))
655 }
656 break
657 case javaSystem:
658 if otherLinkType == javaPlatform {
659 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
660 ctx.OtherModuleName(to))
661 }
662 break
663 case javaPlatform:
664 // no restriction on link-type
665 break
Jiyong Park750e5572018-01-31 00:20:13 +0900666 }
667}
668
Colin Cross32f676a2017-09-06 13:41:06 -0700669func (j *Module) collectDeps(ctx android.ModuleContext) deps {
670 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700671
Colin Cross300f0382018-03-06 13:11:51 -0800672 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700673 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800674 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700675 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800676 } else if sdkDep.useFiles {
677 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700678 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800679 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
680 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700681 }
682
Colin Crossd11fcda2017-10-23 17:59:01 -0700683 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700684 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700685 tag := ctx.OtherModuleDependencyTag(module)
686
Colin Crossa4f08812018-10-02 22:03:40 -0700687 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700688 // Handled by AndroidApp.collectAppDeps
689 return
690 }
691 if tag == certificateTag {
692 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700693 return
694 }
695
Jiyong Park750e5572018-01-31 00:20:13 +0900696 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700697 switch tag {
698 case bootClasspathTag, libTag, staticLibTag:
699 checkLinkType(ctx, j, to, tag.(dependencyTag))
700 }
Jiyong Park750e5572018-01-31 00:20:13 +0900701 }
Colin Cross54250902017-12-05 09:28:08 -0800702 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800703 case SdkLibraryDependency:
704 switch tag {
705 case libTag:
706 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
707 // names of sdk libs that are directly depended are exported
708 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
709 default:
710 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
711 }
Colin Cross54250902017-12-05 09:28:08 -0800712 case Dependency:
713 switch tag {
714 case bootClasspathTag:
715 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700716 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800717 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900718 // sdk lib names from dependencies are re-exported
719 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800720 case staticLibTag:
721 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
722 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
723 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700724 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900725 // sdk lib names from dependencies are re-exported
726 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800727 case pluginTag:
728 if plugin, ok := dep.(*Plugin); ok {
729 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
730 if plugin.pluginProperties.Processor_class != nil {
731 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
732 }
733 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
734 } else {
735 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
736 }
Colin Cross54250902017-12-05 09:28:08 -0800737 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100738 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800739 // framework.jar has a one-off dependency on the R.java and Manifest.java files
740 // generated by framework-res.apk
741 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
742 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800743 case frameworkApkTag:
744 if ctx.ModuleName() == "android_stubs_current" ||
745 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700746 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800747 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
748 // resource files out of there for aapt.
749 //
750 // Normally the package rule runs aapt, which includes the resource,
751 // but we're not running that in our package rule so just copy in the
752 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700753 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800754 }
Colin Cross54250902017-12-05 09:28:08 -0800755 case kotlinStdlibTag:
756 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800757 case kotlinAnnotationsTag:
758 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800759 }
760
761 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
762 case android.SourceFileProducer:
763 switch tag {
764 case libTag:
765 checkProducesJars(ctx, dep)
766 deps.classpath = append(deps.classpath, dep.Srcs()...)
767 case staticLibTag:
768 checkProducesJars(ctx, dep)
769 deps.classpath = append(deps.classpath, dep.Srcs()...)
770 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
771 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800772 }
773 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700774 switch tag {
775 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700776 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700777 case systemModulesTag:
778 if deps.systemModules != nil {
779 panic("Found two system module dependencies")
780 }
781 sm := module.(*SystemModules)
782 if sm.outputFile == nil {
783 panic("Missing directory for system module dependency")
784 }
785 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700786 default:
787 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700788 }
Colin Crossec7a0422017-07-07 14:47:12 -0700789 }
Colin Cross2fe66872015-03-30 17:20:39 -0700790 })
791
Jiyong Park1be96912018-05-28 18:02:19 +0900792 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
793
Colin Cross32f676a2017-09-06 13:41:06 -0700794 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700795}
796
Colin Cross83bb3162018-06-25 15:48:06 -0700797func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700798 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800799 v := sdkContext.sdkVersion()
800 // For PDK builds, use the latest SDK version instead of "current"
801 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
802 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
803 latestSdkVersion := 0
804 if len(sdkVersions) > 0 {
805 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
806 }
807 v = strconv.Itoa(latestSdkVersion)
808 }
809
810 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700811 if err != nil {
812 ctx.PropertyErrorf("sdk_version", "%s", err)
813 }
Nan Zhang357466b2018-04-17 17:38:36 -0700814 if javaVersion != "" {
815 ret = javaVersion
816 } else if ctx.Device() && sdk <= 23 {
817 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900818 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700819 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700820 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700821 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
822 ret = "1.8"
823 } else {
824 ret = "1.9"
825 }
826
827 return ret
828}
829
Nan Zhanged19fc32017-10-19 13:06:22 -0700830func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700831
Colin Crossf03c82b2015-04-13 13:53:40 -0700832 var flags javaBuilderFlags
833
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100834 // javaVersion flag.
835 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
836
Nan Zhanged19fc32017-10-19 13:06:22 -0700837 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700838 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100839 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700840 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700841 }
Colin Cross6510f912017-11-29 00:27:14 -0800842 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700843 // Override the -g flag passed globally to remove local variable debug info to reduce
844 // disk and memory usage.
845 javacFlags = append(javacFlags, "-g:source,lines")
846 }
Colin Cross64162712017-08-08 13:17:59 -0700847
Colin Cross66548102018-06-19 22:47:35 -0700848 if ctx.Config().RunErrorProne() {
849 if config.ErrorProneClasspath == nil {
850 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
851 }
852
853 errorProneFlags := []string{
854 "-Xplugin:ErrorProne",
855 "${config.ErrorProneChecks}",
856 }
857 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
858
859 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
860 "'" + strings.Join(errorProneFlags, " ") + "'"
861 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800862 }
863
Nan Zhanged19fc32017-10-19 13:06:22 -0700864 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800865 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
866 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700867 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800868
Colin Crossbe9cdb82019-01-21 21:37:16 -0800869 flags.processor = strings.Join(deps.processorClasses, ",")
870
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100871 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800872 !Bool(j.properties.No_standard_libs) &&
873 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
874 // Give host-side tools a version of OpenJDK's standard libraries
875 // close to what they're targeting. As of Dec 2017, AOSP is only
876 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
877 //
878 // When building with OpenJDK 8, the following should have no
879 // effect since those jars would be available by default.
880 //
881 // When building with OpenJDK 9 but targeting a version < 1.8,
882 // putting them on the bootclasspath means that:
883 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
884 // b) references to existing APIs are not reinterpreted in an
885 // OpenJDK 9-specific way, eg. calls to subclasses of
886 // java.nio.Buffer as in http://b/70862583
887 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
888 flags.bootClasspath = append(flags.bootClasspath,
889 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
890 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800891 if Bool(j.properties.Use_tools_jar) {
892 flags.bootClasspath = append(flags.bootClasspath,
893 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
894 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800895 }
896
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100897 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800898 // Manually specify build directory in case it is not under the repo root.
899 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
900 // just adding a symlink under the root doesn't help.)
901 patchPaths := ".:" + ctx.Config().BuildDir()
902 classPath := flags.classpath.FormJavaClassPath("")
903 if classPath != "" {
904 patchPaths += ":" + classPath
905 }
906 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700907 }
908
Nan Zhanged19fc32017-10-19 13:06:22 -0700909 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700910 if deps.systemModules != nil {
911 flags.systemModules = append(flags.systemModules, deps.systemModules)
912 }
913
Nan Zhanged19fc32017-10-19 13:06:22 -0700914 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700915 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700916 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700917 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700918 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
919 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700920 }
921
Colin Cross81440082018-08-15 20:21:55 -0700922 if len(javacFlags) > 0 {
923 // optimization.
924 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
925 flags.javacFlags = "$javacFlags"
926 }
927
Nan Zhanged19fc32017-10-19 13:06:22 -0700928 return flags
929}
Colin Crossc0b06f12015-04-08 13:03:43 -0700930
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800931func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700932
Colin Crossebe1a512017-11-14 13:12:14 -0800933 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700934
935 deps := j.collectDeps(ctx)
936 flags := j.collectBuilderFlags(ctx, deps)
937
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100938 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700939 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
940 }
Colin Cross8a497952019-03-05 22:25:09 -0800941 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700942 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800943 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700944 }
945
Colin Crossaf050172017-11-15 23:01:59 -0800946 srcFiles = j.genSources(ctx, srcFiles, flags)
947
948 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700949 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800950 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700951
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700952 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
953 // that IDEInfo struct will use
954 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
955
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800956 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800957 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800958 }
959
Colin Cross1ee23172017-10-18 14:44:18 -0700960 jarName := ctx.ModuleName() + ".jar"
961
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000962 javaSrcFiles := srcFiles.FilterByExt(".java")
963 var uniqueSrcFiles android.Paths
964 set := make(map[string]bool)
965 for _, v := range javaSrcFiles {
966 if _, found := set[v.String()]; !found {
967 set[v.String()] = true
968 uniqueSrcFiles = append(uniqueSrcFiles, v)
969 }
970 }
971
Colin Cross55f63ea2018-08-27 12:37:09 -0700972 var kotlinJars android.Paths
973
Colin Cross93e85952017-08-15 13:34:18 -0700974 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200975 // user defined kotlin flags.
976 kotlincFlags := j.properties.Kotlincflags
977 CheckKotlincFlags(ctx, kotlincFlags)
978
Colin Cross93e85952017-08-15 13:34:18 -0700979 // If there are kotlin files, compile them first but pass all the kotlin and java files
980 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
981 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200982 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700983 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200984 kotlincFlags = append(kotlincFlags, "-no-jdk")
985 }
986 if len(kotlincFlags) > 0 {
987 // optimization.
988 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
989 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700990 }
991
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000992 var kotlinSrcFiles android.Paths
993 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
994 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
995
Colin Crossafbb1732019-01-17 15:42:52 -0800996 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
997 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
998
999 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1000 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1001
1002 if len(flags.processorPath) > 0 {
1003 // Use kapt for annotation processing
1004 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1005 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1006 srcJars = append(srcJars, kaptSrcJar)
1007 // Disable annotation processing in javac, it's already been handled by kapt
1008 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001009 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001010 }
Colin Cross93e85952017-08-15 13:34:18 -07001011
Colin Cross1ee23172017-10-18 14:44:18 -07001012 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001013 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001014 if ctx.Failed() {
1015 return
1016 }
1017
1018 // Make javac rule depend on the kotlinc rule
1019 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001020
Colin Cross93e85952017-08-15 13:34:18 -07001021 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001022 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001023 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001024 }
1025
Colin Cross55f63ea2018-08-27 12:37:09 -07001026 jars := append(android.Paths(nil), kotlinJars...)
1027
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001028 // Store the list of .java files that was passed to javac
1029 j.compiledJavaSrcs = uniqueSrcFiles
1030 j.compiledSrcJars = srcJars
1031
Nan Zhang61eaedb2017-11-02 13:28:15 -07001032 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001033 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001034 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1035 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001036 // Formerly, there was a check here that prevented annotation processors
1037 // from being used when sharding was enabled, as some annotation processors
1038 // do not function correctly in sharded environments. It was removed to
1039 // allow for the use of annotation processors that do function correctly
1040 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001041 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001042 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001043 if ctx.Failed() {
1044 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001045 }
1046 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001047 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001048 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001049 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001050 // If error-prone is enabled, add an additional rule to compile the java files into
1051 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001052 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001053 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1054 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001055 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001056 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001057 extraJarDeps = append(extraJarDeps, errorprone)
1058 }
1059
Nan Zhang61eaedb2017-11-02 13:28:15 -07001060 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001061 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001062 shardSize := int(*(j.properties.Javac_shard_size))
1063 var shardSrcs []android.Paths
1064 if len(uniqueSrcFiles) > 0 {
1065 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1066 for idx, shardSrc := range shardSrcs {
1067 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1068 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1069 jars = append(jars, classes)
1070 }
1071 }
1072 if len(srcJars) > 0 {
1073 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1074 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1075 jars = append(jars, classes)
1076 }
1077 } else {
1078 classes := android.PathForModuleOut(ctx, "javac", jarName)
1079 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1080 jars = append(jars, classes)
1081 }
Colin Crossd6891432017-09-27 17:39:56 -07001082 if ctx.Failed() {
1083 return
1084 }
Colin Cross2fe66872015-03-30 17:20:39 -07001085 }
1086
Colin Crosscedd4762018-09-13 11:26:19 -07001087 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1088 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001089 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1090
1091 var resArgs []string
1092 var resDeps android.Paths
1093
1094 resArgs = append(resArgs, dirArgs...)
1095 resDeps = append(resDeps, dirDeps...)
1096
1097 resArgs = append(resArgs, fileArgs...)
1098 resDeps = append(resDeps, fileDeps...)
1099
Colin Crossff3ae9d2018-04-10 16:15:18 -07001100 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001101 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001102 resArgs = append(resArgs, srcArgs...)
1103 resDeps = append(resDeps, srcDeps...)
1104 }
Colin Cross40a36712017-09-27 17:41:35 -07001105
1106 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001107 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001108 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001109 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001110 if ctx.Failed() {
1111 return
1112 }
1113 }
1114
Colin Cross331a1212018-08-15 20:40:52 -07001115 if len(deps.staticResourceJars) > 0 {
1116 var jars android.Paths
1117 if j.resourceJar != nil {
1118 jars = append(jars, j.resourceJar)
1119 }
1120 jars = append(jars, deps.staticResourceJars...)
1121
1122 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1123 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1124 false, nil, nil)
1125 j.resourceJar = combinedJar
1126 }
1127
Colin Cross32f676a2017-09-06 13:41:06 -07001128 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001129
Colin Cross094054a2018-10-17 15:10:48 -07001130 manifest := j.overrideManifest
1131 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001132 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001133 }
Colin Cross635acc92017-09-12 22:50:46 -07001134
Colin Cross8a497952019-03-05 22:25:09 -08001135 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001136 if len(services) > 0 {
1137 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1138 var zipargs []string
1139 for _, file := range services {
1140 serviceFile := file.String()
1141 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1142 }
1143 ctx.Build(pctx, android.BuildParams{
1144 Rule: zip,
1145 Output: servicesJar,
1146 Implicits: services,
1147 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001148 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001149 },
1150 })
1151 jars = append(jars, servicesJar)
1152 }
1153
Colin Cross0a6e0072017-08-30 14:24:55 -07001154 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001155 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001156 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001157
1158 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001159 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1160 // Optimization: skip the combine step if there is nothing to do
1161 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1162 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1163 // any if len(jars) == 1.
1164 outputFile = moduleOutPath
1165 } else {
1166 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1167 ctx.Build(pctx, android.BuildParams{
1168 Rule: android.Cp,
1169 Input: jars[0],
1170 Output: combinedJar,
1171 })
1172 outputFile = combinedJar
1173 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001174 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001175 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001176 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001177 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001178 outputFile = combinedJar
1179 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001180
Colin Cross331a1212018-08-15 20:40:52 -07001181 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001182 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001183 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001184 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001185 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001186 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001187
1188 // jarjar resource jar if necessary
1189 if j.resourceJar != nil {
1190 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001191 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001192 j.resourceJar = resourceJarJarFile
1193 }
1194
Colin Cross0a6e0072017-08-30 14:24:55 -07001195 if ctx.Failed() {
1196 return
1197 }
1198 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001199 j.implementationJarFile = outputFile
1200 if j.headerJarFile == nil {
1201 j.headerJarFile = j.implementationJarFile
1202 }
Colin Cross2fe66872015-03-30 17:20:39 -07001203
Colin Cross6510f912017-11-29 00:27:14 -08001204 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001205 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1206 j.properties.Instrument = true
1207 }
1208 }
1209
Colin Cross3144dfc2018-01-03 15:06:47 -08001210 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001211 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1212 }
1213
Colin Cross331a1212018-08-15 20:40:52 -07001214 // merge implementation jar with resources if necessary
1215 implementationAndResourcesJar := outputFile
1216 if j.resourceJar != nil {
1217 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1218 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1219 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1220 false, nil, nil)
1221 implementationAndResourcesJar = combinedJar
1222 }
1223
1224 j.implementationAndResourcesJar = implementationAndResourcesJar
1225
Colin Cross9ae1b922018-06-26 17:59:05 -07001226 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001227 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001228 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001229 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001230 if ctx.Failed() {
1231 return
1232 }
Colin Cross331a1212018-08-15 20:40:52 -07001233
Colin Cross8faf8fc2019-01-16 15:15:52 -08001234 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001235 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1236 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001237
Colin Cross331a1212018-08-15 20:40:52 -07001238 // merge dex jar with resources if necessary
1239 if j.resourceJar != nil {
1240 jars := android.Paths{dexOutputFile, j.resourceJar}
1241 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1242 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1243 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001244 if j.deviceProperties.UncompressDex {
1245 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1246 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1247 dexOutputFile = combinedAlignedJar
1248 } else {
1249 dexOutputFile = combinedJar
1250 }
Colin Cross331a1212018-08-15 20:40:52 -07001251 }
1252
1253 j.dexJarFile = dexOutputFile
1254
Colin Cross8faf8fc2019-01-16 15:15:52 -08001255 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001256 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1257
1258 j.maybeStrippedDexJarFile = dexOutputFile
1259
Colin Cross3063b782018-08-15 11:19:12 -07001260 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001261
1262 if ctx.Failed() {
1263 return
1264 }
Colin Cross331a1212018-08-15 20:40:52 -07001265 } else {
1266 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001267 }
Colin Cross331a1212018-08-15 20:40:52 -07001268
Colin Crossb7a63242015-04-16 14:09:14 -07001269 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001270
1271 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1272 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001273}
1274
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001275// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1276// since some of these flags may be used internally.
1277func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1278 for _, flag := range flags {
1279 flag = strings.TrimSpace(flag)
1280
1281 if !strings.HasPrefix(flag, "-") {
1282 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1283 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1284 ctx.PropertyErrorf("kotlincflags",
1285 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1286 } else if inList(flag, config.KotlincIllegalFlags) {
1287 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1288 } else if flag == "-include-runtime" {
1289 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1290 } else {
1291 args := strings.Split(flag, " ")
1292 if args[0] == "-kotlin-home" {
1293 ctx.PropertyErrorf("kotlincflags",
1294 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1295 }
1296 }
1297 }
1298}
1299
Colin Cross8eadbf02017-10-24 17:46:00 -07001300func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001301 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001302
1303 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001304 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001305 // Compile java sources into turbine.jar.
1306 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1307 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1308 if ctx.Failed() {
1309 return nil
1310 }
1311 jars = append(jars, turbineJar)
1312 }
1313
Colin Cross55f63ea2018-08-27 12:37:09 -07001314 jars = append(jars, extraJars...)
1315
Nan Zhanged19fc32017-10-19 13:06:22 -07001316 // Combine any static header libraries into classes-header.jar. If there is only
1317 // one input jar this step will be skipped.
1318 var headerJar android.Path
1319 jars = append(jars, deps.staticHeaderJars...)
1320
Colin Cross5c6ecc12017-10-23 18:12:27 -07001321 // we cannot skip the combine step for now if there is only one jar
1322 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1323 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001324 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1325 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001326 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001327
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001328 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001329 // Transform classes.jar into classes-jarjar.jar
1330 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001331 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001332 headerJar = jarjarFile
1333 if ctx.Failed() {
1334 return nil
1335 }
1336 }
1337
1338 return headerJar
1339}
1340
Colin Crosscb933592017-11-22 13:49:43 -08001341func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001342 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001343
Colin Cross7a3139e2017-12-19 13:57:50 -08001344 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001345
Colin Cross84c38822018-01-03 15:59:46 -08001346 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001347 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1348
1349 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1350
1351 j.jacocoReportClassesFile = jacocoReportClassesFile
1352
1353 return instrumentedJar
1354}
1355
albaltai36ff7dc2018-12-25 14:35:23 +08001356var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001357
Nan Zhanged19fc32017-10-19 13:06:22 -07001358func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001359 if j.headerJarFile == nil {
1360 return nil
1361 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001362 return android.Paths{j.headerJarFile}
1363}
1364
1365func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001366 if j.implementationJarFile == nil {
1367 return nil
1368 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001369 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001370}
1371
Colin Crossf24a22a2019-01-31 14:12:44 -08001372func (j *Module) DexJar() android.Path {
1373 return j.dexJarFile
1374}
1375
Colin Cross331a1212018-08-15 20:40:52 -07001376func (j *Module) ResourceJars() android.Paths {
1377 if j.resourceJar == nil {
1378 return nil
1379 }
1380 return android.Paths{j.resourceJar}
1381}
1382
1383func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001384 if j.implementationAndResourcesJar == nil {
1385 return nil
1386 }
Colin Cross331a1212018-08-15 20:40:52 -07001387 return android.Paths{j.implementationAndResourcesJar}
1388}
1389
Colin Cross46c9b8b2017-06-22 16:51:17 -07001390func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001391 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001392 return j.exportAidlIncludeDirs
1393}
1394
Jiyong Park1be96912018-05-28 18:02:19 +09001395func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001396 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001397 return j.exportedSdkLibs
1398}
1399
Colin Cross46c9b8b2017-06-22 16:51:17 -07001400var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001401
Colin Cross46c9b8b2017-06-22 16:51:17 -07001402func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001403 return j.logtagsSrcs
1404}
1405
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001406// Collect information for opening IDE project files in java/jdeps.go.
1407func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1408 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1409 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1410 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001411 if j.expandJarjarRules != nil {
1412 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001413 }
1414}
1415
1416func (j *Module) CompilerDeps() []string {
1417 jdeps := []string{}
1418 jdeps = append(jdeps, j.properties.Libs...)
1419 jdeps = append(jdeps, j.properties.Static_libs...)
1420 return jdeps
1421}
1422
Colin Cross2fe66872015-03-30 17:20:39 -07001423//
1424// Java libraries (.jar file)
1425//
1426
Colin Crossf506d872017-07-19 15:53:04 -07001427type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001428 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001429}
1430
Colin Cross42be7612019-02-21 18:12:14 -08001431func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001432 // Store uncompressed (and do not strip) dex files from boot class path jars.
1433 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1434 return true
1435 }
1436
1437 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001438 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001439 return true
1440 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001441 if ctx.Config().UncompressPrivAppDex() &&
1442 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1443 return true
1444 }
1445
Colin Cross2fc72f62018-12-21 12:59:54 -08001446 return false
1447}
1448
Colin Crossf506d872017-07-19 15:53:04 -07001449func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001450 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1451 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001452 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001453 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001454 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001455 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001456
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001457 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001458 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1459 ctx.ModuleName()+".jar", j.outputFile)
1460 }
Colin Crossb7a63242015-04-16 14:09:14 -07001461}
1462
Colin Crossf506d872017-07-19 15:53:04 -07001463func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001464 j.deps(ctx)
1465}
1466
Colin Cross1b16b0e2019-02-12 14:41:32 -08001467// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1468//
1469// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1470// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1471// as a `static_libs` dependency of another module.
1472//
1473// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1474// a device.
1475//
1476// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1477// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001478func LibraryFactory() android.Module {
1479 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001480
Colin Cross9ae1b922018-06-26 17:59:05 -07001481 module.AddProperties(
1482 &module.Module.properties,
1483 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001484 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001485 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001486
Colin Cross9ae1b922018-06-26 17:59:05 -07001487 InitJavaModule(module, android.HostAndDeviceSupported)
1488 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001489}
1490
Colin Cross1b16b0e2019-02-12 14:41:32 -08001491// java_library_static is an obsolete alias for java_library.
1492func LibraryStaticFactory() android.Module {
1493 return LibraryFactory()
1494}
1495
1496// java_library_host builds and links sources into a `.jar` file for the host.
1497//
1498// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1499// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001500func LibraryHostFactory() android.Module {
1501 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001502
Colin Cross6af17aa2017-09-20 12:59:05 -07001503 module.AddProperties(
1504 &module.Module.properties,
1505 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001506
Colin Cross9ae1b922018-06-26 17:59:05 -07001507 module.Module.properties.Installable = proptools.BoolPtr(true)
1508
Colin Cross89536d42017-07-07 14:35:50 -07001509 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001510 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001511}
1512
1513//
Colin Crossb628ea52018-08-14 16:42:33 -07001514// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001515//
1516
1517type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001518 // list of compatibility suites (for example "cts", "vts") that the module should be
1519 // installed into.
1520 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001521
1522 // the name of the test configuration (for example "AndroidTest.xml") that should be
1523 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001524 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001525
Jack He33338892018-09-19 02:21:28 -07001526 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1527 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001528 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001529
Colin Crossd96ca352018-08-10 16:06:24 -07001530 // list of files or filegroup modules that provide data that should be installed alongside
1531 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001532 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001533}
1534
Paul Duffin42df1442019-03-20 12:45:53 +00001535type testHelperLibraryProperties struct {
1536 // list of compatibility suites (for example "cts", "vts") that the module should be
1537 // installed into.
1538 Test_suites []string `android:"arch_variant"`
1539}
1540
Colin Cross05638fc2018-04-09 18:40:24 -07001541type Test struct {
1542 Library
1543
1544 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001545
1546 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001547 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001548}
1549
Paul Duffin42df1442019-03-20 12:45:53 +00001550type TestHelperLibrary struct {
1551 Library
1552
1553 testHelperLibraryProperties testHelperLibraryProperties
1554}
1555
Colin Cross303e21f2018-08-07 16:49:25 -07001556func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001557 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 -08001558 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001559
1560 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001561}
1562
Paul Duffin42df1442019-03-20 12:45:53 +00001563func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1564 j.Library.GenerateAndroidBuildActions(ctx)
1565}
1566
Colin Cross1b16b0e2019-02-12 14:41:32 -08001567// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1568// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1569//
1570// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1571// compiled against the device bootclasspath.
1572//
1573// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1574// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001575func TestFactory() android.Module {
1576 module := &Test{}
1577
1578 module.AddProperties(
1579 &module.Module.properties,
1580 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001581 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001582 &module.Module.protoProperties,
1583 &module.testProperties)
1584
Colin Cross9ae1b922018-06-26 17:59:05 -07001585 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001586 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001587
Colin Cross05638fc2018-04-09 18:40:24 -07001588 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001589 return module
1590}
1591
Paul Duffin42df1442019-03-20 12:45:53 +00001592// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1593func TestHelperLibraryFactory() android.Module {
1594 module := &TestHelperLibrary{}
1595
1596 module.AddProperties(
1597 &module.Module.properties,
1598 &module.Module.deviceProperties,
1599 &module.Module.dexpreoptProperties,
1600 &module.Module.protoProperties,
1601 &module.testHelperLibraryProperties)
1602
1603 InitJavaModule(module, android.HostAndDeviceSupported)
1604 return module
1605}
1606
Colin Cross1b16b0e2019-02-12 14:41:32 -08001607// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1608// allow running the test with `atest` or a `TEST_MAPPING` file.
1609//
1610// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1611// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001612func TestHostFactory() android.Module {
1613 module := &Test{}
1614
1615 module.AddProperties(
1616 &module.Module.properties,
1617 &module.Module.protoProperties,
1618 &module.testProperties)
1619
Colin Cross9ae1b922018-06-26 17:59:05 -07001620 module.Module.properties.Installable = proptools.BoolPtr(true)
1621
Colin Cross05638fc2018-04-09 18:40:24 -07001622 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001623 return module
1624}
1625
1626//
Colin Cross2fe66872015-03-30 17:20:39 -07001627// Java Binaries (.jar file plus wrapper script)
1628//
1629
Colin Crossf506d872017-07-19 15:53:04 -07001630type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001631 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001632 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001633
1634 // Name of the class containing main to be inserted into the manifest as Main-Class.
1635 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001636}
1637
Colin Crossf506d872017-07-19 15:53:04 -07001638type Binary struct {
1639 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001640
Colin Crossf506d872017-07-19 15:53:04 -07001641 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001642
Colin Cross6b4a32d2017-12-05 13:42:45 -08001643 isWrapperVariant bool
1644
Colin Crossc3315992017-12-08 19:12:36 -08001645 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001646 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001647}
1648
Alex Light24237172017-10-26 09:46:21 -07001649func (j *Binary) HostToolPath() android.OptionalPath {
1650 return android.OptionalPathForPath(j.binaryFile)
1651}
1652
Colin Crossf506d872017-07-19 15:53:04 -07001653func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001654 if ctx.Arch().ArchType == android.Common {
1655 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001656 if j.binaryProperties.Main_class != nil {
1657 if j.properties.Manifest != nil {
1658 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1659 }
1660 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1661 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1662 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1663 }
1664
Colin Cross6b4a32d2017-12-05 13:42:45 -08001665 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001666 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001667 // Handle the binary wrapper
1668 j.isWrapperVariant = true
1669
Colin Cross366938f2017-12-11 16:29:02 -08001670 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001671 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001672 } else {
1673 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1674 }
1675
1676 // Depend on the installed jar so that the wrapper doesn't get executed by
1677 // another build rule before the jar has been installed.
1678 jarFile := ctx.PrimaryModule().(*Binary).installFile
1679
1680 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1681 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001682 }
Colin Cross2fe66872015-03-30 17:20:39 -07001683}
1684
Colin Crossf506d872017-07-19 15:53:04 -07001685func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001686 if ctx.Arch().ArchType == android.Common {
1687 j.deps(ctx)
1688 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001689}
1690
Colin Cross1b16b0e2019-02-12 14:41:32 -08001691// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1692// as well.
1693//
1694// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1695// compiled against the device bootclasspath.
1696//
1697// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1698// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001699func BinaryFactory() android.Module {
1700 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001701
Colin Cross36242852017-06-23 15:06:31 -07001702 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001703 &module.Module.properties,
1704 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001705 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001706 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001707 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001708
Colin Cross9ae1b922018-06-26 17:59:05 -07001709 module.Module.properties.Installable = proptools.BoolPtr(true)
1710
Colin Cross6b4a32d2017-12-05 13:42:45 -08001711 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1712 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001713 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001714}
1715
Colin Cross1b16b0e2019-02-12 14:41:32 -08001716// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1717//
1718// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1719// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001720func BinaryHostFactory() android.Module {
1721 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001722
Colin Cross36242852017-06-23 15:06:31 -07001723 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001724 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001725 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001726 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001727
Colin Cross9ae1b922018-06-26 17:59:05 -07001728 module.Module.properties.Installable = proptools.BoolPtr(true)
1729
Colin Cross6b4a32d2017-12-05 13:42:45 -08001730 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1731 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001732 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001733}
1734
1735//
1736// Java prebuilts
1737//
1738
Colin Cross74d73e22017-08-02 11:05:49 -07001739type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001740 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001741
Nan Zhangea568a42017-11-08 21:20:04 -08001742 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001743
1744 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001745
1746 // List of shared java libs that this module has dependencies to
1747 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001748
1749 // List of files to remove from the jar file(s)
1750 Exclude_files []string
1751
1752 // List of directories to remove from the jar file(s)
1753 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001754
1755 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001756 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001757}
1758
1759type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001760 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001761 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001762 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001763
Colin Cross74d73e22017-08-02 11:05:49 -07001764 properties ImportProperties
1765
Colin Cross0a6e0072017-08-30 14:24:55 -07001766 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001767 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001768}
1769
Colin Cross83bb3162018-06-25 15:48:06 -07001770func (j *Import) sdkVersion() string {
1771 return String(j.properties.Sdk_version)
1772}
1773
1774func (j *Import) minSdkVersion() string {
1775 return j.sdkVersion()
1776}
1777
Colin Cross74d73e22017-08-02 11:05:49 -07001778func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001779 return &j.prebuilt
1780}
1781
Colin Cross74d73e22017-08-02 11:05:49 -07001782func (j *Import) PrebuiltSrcs() []string {
1783 return j.properties.Jars
1784}
1785
1786func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001787 return j.prebuilt.Name(j.ModuleBase.Name())
1788}
1789
Colin Cross74d73e22017-08-02 11:05:49 -07001790func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001791 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001792}
1793
Colin Cross74d73e22017-08-02 11:05:49 -07001794func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001795 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001796
Nan Zhang4c819fb2018-08-27 18:31:46 -07001797 jarName := ctx.ModuleName() + ".jar"
1798 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001799 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1800 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001801 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001802 inputFile := outputFile
1803 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1804 TransformJetifier(ctx, outputFile, inputFile)
1805 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001806 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001807
1808 ctx.VisitDirectDeps(func(module android.Module) {
1809 otherName := ctx.OtherModuleName(module)
1810 tag := ctx.OtherModuleDependencyTag(module)
1811
1812 switch dep := module.(type) {
1813 case Dependency:
1814 switch tag {
1815 case libTag, staticLibTag:
1816 // sdk lib names from dependencies are re-exported
1817 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1818 }
1819 case SdkLibraryDependency:
1820 switch tag {
1821 case libTag:
1822 // names of sdk libs that are directly depended are exported
1823 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1824 }
1825 }
1826 })
1827
1828 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001829 if Bool(j.properties.Installable) {
1830 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1831 ctx.ModuleName()+".jar", outputFile)
1832 }
Colin Cross2fe66872015-03-30 17:20:39 -07001833}
1834
Colin Cross74d73e22017-08-02 11:05:49 -07001835var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001836
Nan Zhanged19fc32017-10-19 13:06:22 -07001837func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001838 if j.combinedClasspathFile == nil {
1839 return nil
1840 }
Colin Cross37f6d792018-07-12 12:28:41 -07001841 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001842}
1843
1844func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001845 if j.combinedClasspathFile == nil {
1846 return nil
1847 }
Colin Cross37f6d792018-07-12 12:28:41 -07001848 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001849}
1850
Colin Cross331a1212018-08-15 20:40:52 -07001851func (j *Import) ResourceJars() android.Paths {
1852 return nil
1853}
1854
1855func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001856 if j.combinedClasspathFile == nil {
1857 return nil
1858 }
Colin Cross331a1212018-08-15 20:40:52 -07001859 return android.Paths{j.combinedClasspathFile}
1860}
1861
Colin Crossf24a22a2019-01-31 14:12:44 -08001862func (j *Import) DexJar() android.Path {
1863 return nil
1864}
1865
Colin Cross74d73e22017-08-02 11:05:49 -07001866func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001867 return nil
1868}
1869
Jiyong Park1be96912018-05-28 18:02:19 +09001870func (j *Import) ExportedSdkLibs() []string {
1871 return j.exportedSdkLibs
1872}
1873
albaltai36ff7dc2018-12-25 14:35:23 +08001874// Add compile time check for interface implementation
1875var _ android.IDEInfo = (*Import)(nil)
1876var _ android.IDECustomizedModuleName = (*Import)(nil)
1877
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001878// Collect information for opening IDE project files in java/jdeps.go.
1879const (
1880 removedPrefix = "prebuilt_"
1881)
1882
1883func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1884 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1885}
1886
1887func (j *Import) IDECustomizedModuleName() string {
1888 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1889 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1890 // solution to get the Import name.
1891 name := j.Name()
1892 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001893 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001894 }
1895 return name
1896}
1897
Colin Cross74d73e22017-08-02 11:05:49 -07001898var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001899
Colin Cross1b16b0e2019-02-12 14:41:32 -08001900// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1901//
1902// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1903// compiled against an Android classpath.
1904//
1905// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1906// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001907func ImportFactory() android.Module {
1908 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001909
Colin Cross74d73e22017-08-02 11:05:49 -07001910 module.AddProperties(&module.properties)
1911
1912 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001913 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001914 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001915}
1916
Colin Cross1b16b0e2019-02-12 14:41:32 -08001917// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1918// module.
1919//
1920// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1921// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001922func ImportFactoryHost() android.Module {
1923 module := &Import{}
1924
1925 module.AddProperties(&module.properties)
1926
1927 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001928 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001929 return module
1930}
1931
Colin Cross42be7612019-02-21 18:12:14 -08001932// dex_import module
1933
1934type DexImportProperties struct {
1935 Jars []string
1936}
1937
1938type DexImport struct {
1939 android.ModuleBase
1940 android.DefaultableModuleBase
1941 prebuilt android.Prebuilt
1942
1943 properties DexImportProperties
1944
1945 dexJarFile android.Path
1946 maybeStrippedDexJarFile android.Path
1947
1948 dexpreopter
1949}
1950
1951func (j *DexImport) Prebuilt() *android.Prebuilt {
1952 return &j.prebuilt
1953}
1954
1955func (j *DexImport) PrebuiltSrcs() []string {
1956 return j.properties.Jars
1957}
1958
1959func (j *DexImport) Name() string {
1960 return j.prebuilt.Name(j.ModuleBase.Name())
1961}
1962
1963func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
1964 android.ExtractSourcesDeps(ctx, j.properties.Jars)
1965}
1966
1967func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1968 if len(j.properties.Jars) != 1 {
1969 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1970 }
1971
1972 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1973 j.dexpreopter.isInstallable = true
1974 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1975
1976 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1977 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1978
1979 if j.dexpreopter.uncompressedDex {
1980 rule := android.NewRuleBuilder()
1981
1982 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1983 rule.Temporary(temporary)
1984
1985 // use zip2zip to uncompress classes*.dex files
1986 rule.Command().
1987 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
1988 FlagWithInput("-i ", inputJar).
1989 FlagWithOutput("-o ", temporary).
1990 FlagWithArg("-0 ", "'classes*.dex'")
1991
1992 // use zipalign to align uncompressed classes*.dex files
1993 rule.Command().
1994 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
1995 Flag("-f").
1996 Text("4").
1997 Input(temporary).
1998 Output(dexOutputFile)
1999
2000 rule.DeleteTemporaryFiles()
2001
2002 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2003 } else {
2004 ctx.Build(pctx, android.BuildParams{
2005 Rule: android.Cp,
2006 Input: inputJar,
2007 Output: dexOutputFile,
2008 })
2009 }
2010
2011 j.dexJarFile = dexOutputFile
2012
2013 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2014
2015 j.maybeStrippedDexJarFile = dexOutputFile
2016
2017 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2018 ctx.ModuleName()+".jar", dexOutputFile)
2019}
2020
2021func (j *DexImport) DexJar() android.Path {
2022 return j.dexJarFile
2023}
2024
2025// dex_import imports a `.jar` file containing classes.dex files.
2026//
2027// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2028// to the device.
2029func DexImportFactory() android.Module {
2030 module := &DexImport{}
2031
2032 module.AddProperties(&module.properties)
2033
2034 android.InitPrebuiltModule(module, &module.properties.Jars)
2035 InitJavaModule(module, android.DeviceSupported)
2036 return module
2037}
2038
Colin Cross89536d42017-07-07 14:35:50 -07002039//
2040// Defaults
2041//
2042type Defaults struct {
2043 android.ModuleBase
2044 android.DefaultsModuleBase
2045}
2046
2047func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2048}
2049
Colin Cross1b16b0e2019-02-12 14:41:32 -08002050// java_defaults provides a set of properties that can be inherited by other java or android modules.
2051//
2052// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2053// property in the defaults module that exists in the depending module will be prepended to the depending module's
2054// value for that property.
2055//
2056// Example:
2057//
2058// java_defaults {
2059// name: "example_defaults",
2060// srcs: ["common/**/*.java"],
2061// javacflags: ["-Xlint:all"],
2062// aaptflags: ["--auto-add-overlay"],
2063// }
2064//
2065// java_library {
2066// name: "example",
2067// defaults: ["example_defaults"],
2068// srcs: ["example/**/*.java"],
2069// }
2070//
2071// is functionally identical to:
2072//
2073// java_library {
2074// name: "example",
2075// srcs: [
2076// "common/**/*.java",
2077// "example/**/*.java",
2078// ],
2079// javacflags: ["-Xlint:all"],
2080// }
Colin Cross89536d42017-07-07 14:35:50 -07002081func defaultsFactory() android.Module {
2082 return DefaultsFactory()
2083}
2084
2085func DefaultsFactory(props ...interface{}) android.Module {
2086 module := &Defaults{}
2087
2088 module.AddProperties(props...)
2089 module.AddProperties(
2090 &CompilerProperties{},
2091 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002092 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002093 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002094 &aaptProperties{},
2095 &androidLibraryProperties{},
2096 &appProperties{},
2097 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002098 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002099 &ImportProperties{},
2100 &AARImportProperties{},
2101 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002102 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002103 )
2104
2105 android.InitDefaultsModule(module)
2106
2107 return module
2108}
Nan Zhangea568a42017-11-08 21:20:04 -08002109
2110var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002111var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002112var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002113var inList = android.InList