blob: 1e5c6286706edd85f08f4181d8eddf87904a1c80 [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)
39 android.RegisterModuleType("java_library_static", LibraryFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
44 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070045 android.RegisterModuleType("java_import", ImportFactory)
46 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070047
Colin Cross798bfce2016-10-12 14:28:16 -070048 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070049}
50
Colin Cross2fe66872015-03-30 17:20:39 -070051// TODO:
52// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070053// Renderscript
54// Post-jar passes:
55// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070056// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070057// DroidDoc
58// Findbugs
59
Colin Cross89536d42017-07-07 14:35:50 -070060type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070061 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
62 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070063 Srcs []string `android:"arch_variant"`
64
65 // list of source files that should not be used to build the Java module.
66 // This is most useful in the arch/multilib variants to remove non-common files
67 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
69 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070070 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
Colin Cross86a63ff2017-09-27 17:33:10 -070072 // list of directories that should be excluded from java_resource_dirs
73 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070074
Colin Cross0f37af02017-09-27 17:42:05 -070075 // list of files to use as Java resources
76 Java_resources []string `android:"arch_variant"`
77
Colin Crosscedd4762018-09-13 11:26:19 -070078 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross0f37af02017-09-27 17:42:05 -070079 Exclude_java_resources []string `android:"arch_variant"`
80
Colin Crossfa5eb232017-10-01 20:33:03 -070081 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070082 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070083 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070084
Colin Crossfa5eb232017-10-01 20:33:03 -070085 // don't build against the framework libraries (legacy-test, core-junit,
86 // ext, and framework for device targets)
87 No_framework_libs *bool
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of module-specific flags that will be used for javac compiles
90 Javacflags []string `android:"arch_variant"`
91
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020092 // list of module-specific flags that will be used for kotlinc compiles
93 Kotlincflags []string `android:"arch_variant"`
94
Colin Cross7d5136f2015-05-11 13:39:40 -070095 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross540eff82017-06-22 17:01:52 -0700104 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700105 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700106
107 // If not blank, set the java version passed to javac as -source and -target
108 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700109
Colin Cross9ae1b922018-06-26 17:59:05 -0700110 // If set to true, allow this module to be dexed and installed on devices. Has no
111 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700112 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700113
Colin Cross0f37af02017-09-27 17:42:05 -0700114 // If set to true, include sources used to compile the module in to the final jar
115 Include_srcs *bool
116
Colin Cross32f676a2017-09-06 13:41:06 -0700117 // List of modules to use as annotation processors
118 Annotation_processors []string
119
120 // List of classes to pass to javac to use as annotation processors
121 Annotation_processor_classes []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
131 Srcs []string
132
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"`
Colin Cross540eff82017-06-22 17:01:52 -0700175}
176
Colin Cross89536d42017-07-07 14:35:50 -0700177type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700178 // list of module-specific flags that will be used for dex compiles
179 Dxflags []string `android:"arch_variant"`
180
Colin Cross83bb3162018-06-25 15:48:06 -0700181 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
182 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800183 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700184
Colin Cross83bb3162018-06-25 15:48:06 -0700185 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
186 // Defaults to sdk_version if not set.
187 Min_sdk_version *string
188
Dan Willemsen419290a2018-10-31 15:28:47 -0700189 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
190 // Defaults to sdk_version if not set.
191 Target_sdk_version *string
192
Colin Cross6af2e492018-05-22 11:12:33 -0700193 // if true, compile against the platform APIs instead of an SDK.
194 Platform_apis *bool
195
Colin Crossebe1a512017-11-14 13:12:14 -0800196 Aidl struct {
197 // Top level directories to pass to aidl tool
198 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700199
Colin Crossebe1a512017-11-14 13:12:14 -0800200 // Directories rooted at the Android.bp file to pass to aidl tool
201 Local_include_dirs []string
202
203 // directories that should be added as include directories for any aidl sources of modules
204 // that depend on this module, as well as to aidl for this module.
205 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100206
207 // whether to generate traces (for systrace) for this interface
208 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100209
210 // whether to generate Binder#GetTransaction name method.
211 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800212 }
Colin Cross92430102017-10-09 14:59:32 -0700213
214 // If true, export a copy of the module as a -hostdex module for host testing.
215 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700216
David Brazdil17ef5632018-06-27 10:27:45 +0100217 // If set to true, compile dex regardless of installable. Defaults to false.
218 Compile_dex *bool
219
Colin Cross66dbc0b2017-12-28 12:23:20 -0800220 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700221 // If false, disable all optimization. Defaults to true for android_app and android_test
222 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800223 Enabled *bool
224
225 // If true, optimize for size by removing unused code. Defaults to true for apps,
226 // false for libraries and tests.
227 Shrink *bool
228
229 // If true, optimize bytecode. Defaults to false.
230 Optimize *bool
231
232 // If true, obfuscate bytecode. Defaults to false.
233 Obfuscate *bool
234
235 // If true, do not use the flag files generated by aapt that automatically keep
236 // classes referenced by the app manifest. Defaults to false.
237 No_aapt_flags *bool
238
239 // Flags to pass to proguard.
240 Proguard_flags []string
241
242 // Specifies the locations of files containing proguard flags.
243 Proguard_flags_files []string
244 }
245
Colin Cross1369cdb2017-09-29 17:58:17 -0700246 // When targeting 1.9, override the modules to use with --system
247 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700248
249 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800250 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700251}
252
Colin Cross46c9b8b2017-06-22 16:51:17 -0700253// Module contains the properties and members used by all java module types
254type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700255 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700256 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700257
Colin Cross89536d42017-07-07 14:35:50 -0700258 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700259 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700260 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700261
Colin Cross331a1212018-08-15 20:40:52 -0700262 // jar file containing header classes including static library dependencies, suitable for
263 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700264 headerJarFile android.Path
265
Colin Cross331a1212018-08-15 20:40:52 -0700266 // jar file containing implementation classes including static library dependencies but no
267 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700268 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700269
Colin Cross331a1212018-08-15 20:40:52 -0700270 // jar file containing only resources including from static library dependencies
271 resourceJar android.Path
272
273 // jar file containing implementation classes and resources including static library
274 // dependencies
275 implementationAndResourcesJar android.Path
276
277 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700278 dexJarFile android.Path
279
Colin Cross43f08db2018-11-12 10:13:39 -0800280 // output file that contains classes.dex if it should be in the output file
281 maybeStrippedDexJarFile android.Path
282
Colin Crosscb933592017-11-22 13:49:43 -0800283 // output file containing uninstrumented classes that will be instrumented by jacoco
284 jacocoReportClassesFile android.Path
285
Colin Cross66dbc0b2017-12-28 12:23:20 -0800286 // output file containing mapping of obfuscated names
287 proguardDictionary android.Path
288
Colin Cross331a1212018-08-15 20:40:52 -0700289 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700290 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700291
Colin Cross635c3b02016-05-18 15:37:25 -0700292 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700293
Colin Cross635c3b02016-05-18 15:37:25 -0700294 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700295
Colin Cross2fe66872015-03-30 17:20:39 -0700296 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700297 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800298
299 // list of .java files and srcjars that was passed to javac
300 compiledJavaSrcs android.Paths
301 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800302
303 // list of extra progurad flag files
304 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900305
Colin Cross094054a2018-10-17 15:10:48 -0700306 // manifest file to use instead of properties.Manifest
307 overrideManifest android.OptionalPath
308
Jiyong Park1be96912018-05-28 18:02:19 +0900309 // list of SDK lib names that this java moudule is exporting
310 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700311
312 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
313 // filter out Exclude_srcs, will be used by android.IDEInfo struct
314 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800315
316 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700317}
318
Colin Cross54250902017-12-05 09:28:08 -0800319func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700320 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800321}
322
Jiyong Park8fd61922018-11-08 02:50:25 +0900323func (j *Module) DexJarFile() android.Path {
324 return j.dexJarFile
325}
326
Colin Cross54250902017-12-05 09:28:08 -0800327var _ android.SourceFileProducer = (*Module)(nil)
328
Colin Crossf506d872017-07-19 15:53:04 -0700329type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700330 HeaderJars() android.Paths
331 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700332 ResourceJars() android.Paths
333 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700334 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900335 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700336}
337
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338type SdkLibraryDependency interface {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900339 HeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
340 ImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341}
342
Nan Zhangb2b33de2018-02-23 11:18:47 -0800343type SrcDependency interface {
344 CompiledSrcs() android.Paths
345 CompiledSrcJars() android.Paths
346}
347
348func (j *Module) CompiledSrcs() android.Paths {
349 return j.compiledJavaSrcs
350}
351
352func (j *Module) CompiledSrcJars() android.Paths {
353 return j.compiledSrcJars
354}
355
356var _ SrcDependency = (*Module)(nil)
357
Colin Cross89536d42017-07-07 14:35:50 -0700358func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
359 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
360 android.InitDefaultableModule(module)
361}
362
Colin Crossbe1da472017-07-07 15:59:46 -0700363type dependencyTag struct {
364 blueprint.BaseDependencyTag
365 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700366}
367
Colin Crossa4f08812018-10-02 22:03:40 -0700368type jniDependencyTag struct {
369 blueprint.BaseDependencyTag
370 target android.Target
371}
372
Colin Crossbe1da472017-07-07 15:59:46 -0700373var (
Colin Cross4b964c02018-10-15 16:18:06 -0700374 staticLibTag = dependencyTag{name: "staticlib"}
375 libTag = dependencyTag{name: "javalib"}
376 annoTag = dependencyTag{name: "annotation processor"}
377 bootClasspathTag = dependencyTag{name: "bootclasspath"}
378 systemModulesTag = dependencyTag{name: "system modules"}
379 frameworkResTag = dependencyTag{name: "framework-res"}
380 frameworkApkTag = dependencyTag{name: "framework-apk"}
381 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800382 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700383 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
384 certificateTag = dependencyTag{name: "certificate"}
385 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700386)
Colin Cross2fe66872015-03-30 17:20:39 -0700387
Colin Crossfc3674a2017-09-18 17:41:52 -0700388type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700389 useModule, useFiles, useDefaultLibs, invalidVersion bool
390
Colin Cross86a60ae2018-05-29 14:44:55 -0700391 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700392 systemModules string
393
Colin Crossa97c5d32018-03-28 14:58:31 -0700394 frameworkResModule string
395
Colin Cross86a60ae2018-05-29 14:44:55 -0700396 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700397 aidl android.Path
398}
399
Colin Crossa4f08812018-10-02 22:03:40 -0700400type jniLib struct {
401 name string
402 path android.Path
403 target android.Target
404}
405
Colin Cross3144dfc2018-01-03 15:06:47 -0800406func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
407 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
408}
409
410func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
411 return j.shouldInstrument(ctx) &&
412 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
413 ctx.Config().UnbundledBuild())
414}
415
Colin Cross83bb3162018-06-25 15:48:06 -0700416func (j *Module) sdkVersion() string {
417 return String(j.deviceProperties.Sdk_version)
418}
419
420func (j *Module) minSdkVersion() string {
421 if j.deviceProperties.Min_sdk_version != nil {
422 return *j.deviceProperties.Min_sdk_version
423 }
424 return j.sdkVersion()
425}
426
Dan Willemsen419290a2018-10-31 15:28:47 -0700427func (j *Module) targetSdkVersion() string {
428 if j.deviceProperties.Target_sdk_version != nil {
429 return *j.deviceProperties.Target_sdk_version
430 }
431 return j.sdkVersion()
432}
433
Colin Crossbe1da472017-07-07 15:59:46 -0700434func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700435 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700436 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700437 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700438 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700439 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100440 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700441 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700442 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700443 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700444 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100445 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700446 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800447 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700448 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
449 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800450 }
Colin Crossbe1da472017-07-07 15:59:46 -0700451 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700452 } else if j.deviceProperties.System_modules == nil {
453 ctx.PropertyErrorf("no_standard_libs",
454 "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 +0100455 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700456 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700457 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100458 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700459 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800460 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800461 if ctx.ModuleName() == "android_stubs_current" ||
462 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700463 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700464 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800465 }
Colin Cross2fe66872015-03-30 17:20:39 -0700466 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700467
Colin Cross42d48b72018-08-29 14:10:52 -0700468 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
469 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100470 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700471 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100472 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700473
Colin Cross7f9036c2017-08-30 13:27:57 -0700474 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000475 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700476 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800477 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700478
479 if j.hasSrcExt(".proto") {
480 protoDeps(ctx, &j.protoProperties)
481 }
Colin Cross93e85952017-08-15 13:34:18 -0700482
483 if j.hasSrcExt(".kt") {
484 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
485 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700486 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Crossafbb1732019-01-17 15:42:52 -0800487 if len(j.properties.Annotation_processors) > 0 {
488 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
489 }
Colin Cross93e85952017-08-15 13:34:18 -0700490 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800491
492 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700493 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800494 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700495}
496
497func hasSrcExt(srcs []string, ext string) bool {
498 for _, src := range srcs {
499 if filepath.Ext(src) == ext {
500 return true
501 }
502 }
503
504 return false
505}
506
Nan Zhang61eaedb2017-11-02 13:28:15 -0700507func shardPaths(paths android.Paths, shardSize int) []android.Paths {
508 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
509 for len(paths) > shardSize {
510 ret = append(ret, paths[0:shardSize])
511 paths = paths[shardSize:]
512 }
513 if len(paths) > 0 {
514 ret = append(ret, paths)
515 }
516 return ret
517}
518
Colin Cross6af17aa2017-09-20 12:59:05 -0700519func (j *Module) hasSrcExt(ext string) bool {
520 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700521}
522
Colin Cross46c9b8b2017-06-22 16:51:17 -0700523func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700524 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700525
Colin Crossebe1a512017-11-14 13:12:14 -0800526 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
527 aidlIncludes = append(aidlIncludes,
528 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
529 aidlIncludes = append(aidlIncludes,
530 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700531
Steven Moreland667f6882018-07-26 12:55:08 -0700532 flags := []string{"-b"}
533
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700534 if aidlPreprocess.Valid() {
535 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700536 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700537 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700538 }
539
Colin Cross635c3b02016-05-18 15:37:25 -0700540 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800541 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700542 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800543 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700544 flags = append(flags, "-I"+src.String())
545 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700546
Martijn Coeneneab15642018-03-09 09:29:59 +0100547 if Bool(j.deviceProperties.Aidl.Generate_traces) {
548 flags = append(flags, "-t")
549 }
550
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100551 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
552 flags = append(flags, "--transaction_names")
553 }
554
Colin Crossf03c82b2015-04-13 13:53:40 -0700555 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700556}
557
Colin Cross32f676a2017-09-06 13:41:06 -0700558type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800559 classpath classpath
560 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700561 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700562 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700563 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700564 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700565 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800566 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700567 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700568 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700569 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700570 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800571 kotlinAnnotations android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700572}
Colin Cross2fe66872015-03-30 17:20:39 -0700573
Colin Cross54250902017-12-05 09:28:08 -0800574func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
575 for _, f := range dep.Srcs() {
576 if f.Ext() != ".jar" {
577 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
578 ctx.OtherModuleName(dep.(blueprint.Module)))
579 }
580 }
581}
582
Jiyong Park2d492942018-03-05 17:44:10 +0900583type linkType int
584
585const (
586 javaCore linkType = iota
587 javaSdk
588 javaSystem
589 javaPlatform
590)
591
Jiyong Park46f78fb2018-10-20 16:33:17 +0900592func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700593 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700594 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900595 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
596 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
597 name == "core-lambda-stubs":
598 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100599 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900600 return javaCore, false
601 case name == "android_system_stubs_current":
602 return javaSystem, true
603 case strings.HasPrefix(ver, "system_"):
604 return javaSystem, false
605 case name == "android_test_stubs_current":
606 return javaSystem, true
607 case strings.HasPrefix(ver, "test_"):
608 return javaPlatform, false
609 case name == "android_stubs_current":
610 return javaSdk, true
611 case ver == "current":
612 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700613 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900614 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700615 default:
616 if _, err := strconv.Atoi(ver); err != nil {
617 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
618 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900619 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900620 }
621}
622
Jiyong Park750e5572018-01-31 00:20:13 +0900623func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700624 if ctx.Host() {
625 return
626 }
627
Jiyong Park46f78fb2018-10-20 16:33:17 +0900628 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
629 if stubs {
630 return
631 }
632 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900633 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."
634
635 switch myLinkType {
636 case javaCore:
637 if otherLinkType != javaCore {
638 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 +0900639 ctx.OtherModuleName(to))
640 }
Jiyong Park2d492942018-03-05 17:44:10 +0900641 break
642 case javaSdk:
643 if otherLinkType != javaCore && otherLinkType != javaSdk {
644 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
645 ctx.OtherModuleName(to))
646 }
647 break
648 case javaSystem:
649 if otherLinkType == javaPlatform {
650 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
651 ctx.OtherModuleName(to))
652 }
653 break
654 case javaPlatform:
655 // no restriction on link-type
656 break
Jiyong Park750e5572018-01-31 00:20:13 +0900657 }
658}
659
Colin Cross32f676a2017-09-06 13:41:06 -0700660func (j *Module) collectDeps(ctx android.ModuleContext) deps {
661 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700662
Colin Cross300f0382018-03-06 13:11:51 -0800663 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700664 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800665 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700666 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800667 } else if sdkDep.useFiles {
668 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700669 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800670 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
671 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700672 }
673
Colin Crossd11fcda2017-10-23 17:59:01 -0700674 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700675 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700676 tag := ctx.OtherModuleDependencyTag(module)
677
Colin Crossa4f08812018-10-02 22:03:40 -0700678 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700679 // Handled by AndroidApp.collectAppDeps
680 return
681 }
682 if tag == certificateTag {
683 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700684 return
685 }
686
Jiyong Park750e5572018-01-31 00:20:13 +0900687 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700688 switch tag {
689 case bootClasspathTag, libTag, staticLibTag:
690 checkLinkType(ctx, j, to, tag.(dependencyTag))
691 }
Jiyong Park750e5572018-01-31 00:20:13 +0900692 }
Colin Cross54250902017-12-05 09:28:08 -0800693 switch dep := module.(type) {
694 case Dependency:
695 switch tag {
696 case bootClasspathTag:
697 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700698 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800699 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900700 // sdk lib names from dependencies are re-exported
701 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800702 case staticLibTag:
703 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
704 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
705 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700706 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900707 // sdk lib names from dependencies are re-exported
708 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700709 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700710 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800711 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100712 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800713 // framework.jar has a one-off dependency on the R.java and Manifest.java files
714 // generated by framework-res.apk
715 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
716 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800717 case frameworkApkTag:
718 if ctx.ModuleName() == "android_stubs_current" ||
719 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700720 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800721 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
722 // resource files out of there for aapt.
723 //
724 // Normally the package rule runs aapt, which includes the resource,
725 // but we're not running that in our package rule so just copy in the
726 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700727 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800728 }
Colin Cross54250902017-12-05 09:28:08 -0800729 case kotlinStdlibTag:
730 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800731 case kotlinAnnotationsTag:
732 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800733 }
734
735 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900736 case SdkLibraryDependency:
737 switch tag {
738 case libTag:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900739 deps.classpath = append(deps.classpath, dep.HeaderJars(ctx, j.sdkVersion())...)
Jiyong Park1be96912018-05-28 18:02:19 +0900740 // names of sdk libs that are directly depended are exported
741 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900742 default:
743 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
744 }
Colin Cross54250902017-12-05 09:28:08 -0800745 case android.SourceFileProducer:
746 switch tag {
747 case libTag:
748 checkProducesJars(ctx, dep)
749 deps.classpath = append(deps.classpath, dep.Srcs()...)
750 case staticLibTag:
751 checkProducesJars(ctx, dep)
752 deps.classpath = append(deps.classpath, dep.Srcs()...)
753 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
754 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
755 case android.DefaultsDepTag, android.SourceDepTag:
756 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900757 case publicApiFileTag, systemApiFileTag, testApiFileTag:
758 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800759 default:
760 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
761 }
762 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700763 switch tag {
764 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700765 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700766 case systemModulesTag:
767 if deps.systemModules != nil {
768 panic("Found two system module dependencies")
769 }
770 sm := module.(*SystemModules)
771 if sm.outputFile == nil {
772 panic("Missing directory for system module dependency")
773 }
774 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700775 default:
776 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700777 }
Colin Crossec7a0422017-07-07 14:47:12 -0700778 }
Colin Cross2fe66872015-03-30 17:20:39 -0700779 })
780
Jiyong Park1be96912018-05-28 18:02:19 +0900781 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
782
Colin Cross32f676a2017-09-06 13:41:06 -0700783 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700784}
785
Colin Cross83bb3162018-06-25 15:48:06 -0700786func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700787 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800788 v := sdkContext.sdkVersion()
789 // For PDK builds, use the latest SDK version instead of "current"
790 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
791 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
792 latestSdkVersion := 0
793 if len(sdkVersions) > 0 {
794 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
795 }
796 v = strconv.Itoa(latestSdkVersion)
797 }
798
799 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700800 if err != nil {
801 ctx.PropertyErrorf("sdk_version", "%s", err)
802 }
Nan Zhang357466b2018-04-17 17:38:36 -0700803 if javaVersion != "" {
804 ret = javaVersion
805 } else if ctx.Device() && sdk <= 23 {
806 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900807 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700808 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700809 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700810 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
811 ret = "1.8"
812 } else {
813 ret = "1.9"
814 }
815
816 return ret
817}
818
Nan Zhanged19fc32017-10-19 13:06:22 -0700819func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700820
Colin Crossf03c82b2015-04-13 13:53:40 -0700821 var flags javaBuilderFlags
822
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100823 // javaVersion flag.
824 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
825
Nan Zhanged19fc32017-10-19 13:06:22 -0700826 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700827 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100828 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700829 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700830 }
Colin Cross6510f912017-11-29 00:27:14 -0800831 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700832 // Override the -g flag passed globally to remove local variable debug info to reduce
833 // disk and memory usage.
834 javacFlags = append(javacFlags, "-g:source,lines")
835 }
Colin Cross64162712017-08-08 13:17:59 -0700836
Colin Cross66548102018-06-19 22:47:35 -0700837 if ctx.Config().RunErrorProne() {
838 if config.ErrorProneClasspath == nil {
839 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
840 }
841
842 errorProneFlags := []string{
843 "-Xplugin:ErrorProne",
844 "${config.ErrorProneChecks}",
845 }
846 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
847
848 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
849 "'" + strings.Join(errorProneFlags, " ") + "'"
850 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800851 }
852
Nan Zhanged19fc32017-10-19 13:06:22 -0700853 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800854 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
855 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700856 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800857
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100858 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800859 !Bool(j.properties.No_standard_libs) &&
860 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
861 // Give host-side tools a version of OpenJDK's standard libraries
862 // close to what they're targeting. As of Dec 2017, AOSP is only
863 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
864 //
865 // When building with OpenJDK 8, the following should have no
866 // effect since those jars would be available by default.
867 //
868 // When building with OpenJDK 9 but targeting a version < 1.8,
869 // putting them on the bootclasspath means that:
870 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
871 // b) references to existing APIs are not reinterpreted in an
872 // OpenJDK 9-specific way, eg. calls to subclasses of
873 // java.nio.Buffer as in http://b/70862583
874 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
875 flags.bootClasspath = append(flags.bootClasspath,
876 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
877 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800878 if Bool(j.properties.Use_tools_jar) {
879 flags.bootClasspath = append(flags.bootClasspath,
880 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
881 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800882 }
883
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100884 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800885 // Manually specify build directory in case it is not under the repo root.
886 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
887 // just adding a symlink under the root doesn't help.)
888 patchPaths := ".:" + ctx.Config().BuildDir()
889 classPath := flags.classpath.FormJavaClassPath("")
890 if classPath != "" {
891 patchPaths += ":" + classPath
892 }
893 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700894 }
895
Nan Zhanged19fc32017-10-19 13:06:22 -0700896 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700897 if deps.systemModules != nil {
898 flags.systemModules = append(flags.systemModules, deps.systemModules)
899 }
900
Nan Zhanged19fc32017-10-19 13:06:22 -0700901 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700902 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700903 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700904 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700905 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
906 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700907 }
908
Colin Cross81440082018-08-15 20:21:55 -0700909 if len(javacFlags) > 0 {
910 // optimization.
911 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
912 flags.javacFlags = "$javacFlags"
913 }
914
Nan Zhanged19fc32017-10-19 13:06:22 -0700915 return flags
916}
Colin Crossc0b06f12015-04-08 13:03:43 -0700917
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800918func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700919
Colin Crossebe1a512017-11-14 13:12:14 -0800920 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700921
922 deps := j.collectDeps(ctx)
923 flags := j.collectBuilderFlags(ctx, deps)
924
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100925 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700926 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
927 }
928 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700929 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800930 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700931 }
932
Colin Crossaf050172017-11-15 23:01:59 -0800933 srcFiles = j.genSources(ctx, srcFiles, flags)
934
935 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700936 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800937 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700938
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700939 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
940 // that IDEInfo struct will use
941 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
942
Colin Cross1ee23172017-10-18 14:44:18 -0700943 jarName := ctx.ModuleName() + ".jar"
944
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000945 javaSrcFiles := srcFiles.FilterByExt(".java")
946 var uniqueSrcFiles android.Paths
947 set := make(map[string]bool)
948 for _, v := range javaSrcFiles {
949 if _, found := set[v.String()]; !found {
950 set[v.String()] = true
951 uniqueSrcFiles = append(uniqueSrcFiles, v)
952 }
953 }
954
Colin Cross55f63ea2018-08-27 12:37:09 -0700955 var kotlinJars android.Paths
956
Colin Cross93e85952017-08-15 13:34:18 -0700957 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200958 // user defined kotlin flags.
959 kotlincFlags := j.properties.Kotlincflags
960 CheckKotlincFlags(ctx, kotlincFlags)
961
Colin Cross93e85952017-08-15 13:34:18 -0700962 // If there are kotlin files, compile them first but pass all the kotlin and java files
963 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
964 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200965 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700966 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200967 kotlincFlags = append(kotlincFlags, "-no-jdk")
968 }
969 if len(kotlincFlags) > 0 {
970 // optimization.
971 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
972 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700973 }
974
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000975 var kotlinSrcFiles android.Paths
976 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
977 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
978
Colin Crossafbb1732019-01-17 15:42:52 -0800979 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
980 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
981
982 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
983 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
984
985 if len(flags.processorPath) > 0 {
986 // Use kapt for annotation processing
987 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
988 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
989 srcJars = append(srcJars, kaptSrcJar)
990 // Disable annotation processing in javac, it's already been handled by kapt
991 flags.processorPath = nil
992 }
Colin Cross93e85952017-08-15 13:34:18 -0700993
Colin Cross1ee23172017-10-18 14:44:18 -0700994 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -0800995 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700996 if ctx.Failed() {
997 return
998 }
999
1000 // Make javac rule depend on the kotlinc rule
1001 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001002
Colin Cross93e85952017-08-15 13:34:18 -07001003 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001004 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001005 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001006 }
1007
Colin Cross55f63ea2018-08-27 12:37:09 -07001008 jars := append(android.Paths(nil), kotlinJars...)
1009
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001010 // Store the list of .java files that was passed to javac
1011 j.compiledJavaSrcs = uniqueSrcFiles
1012 j.compiledSrcJars = srcJars
1013
Nan Zhang61eaedb2017-11-02 13:28:15 -07001014 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001015 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001016 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1017 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001018 // Formerly, there was a check here that prevented annotation processors
1019 // from being used when sharding was enabled, as some annotation processors
1020 // do not function correctly in sharded environments. It was removed to
1021 // allow for the use of annotation processors that do function correctly
1022 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001023 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001024 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001025 if ctx.Failed() {
1026 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001027 }
1028 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001029 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001030 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001031 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001032 // If error-prone is enabled, add an additional rule to compile the java files into
1033 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001034 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001035 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1036 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001037 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001038 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001039 extraJarDeps = append(extraJarDeps, errorprone)
1040 }
1041
Nan Zhang61eaedb2017-11-02 13:28:15 -07001042 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001043 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001044 shardSize := int(*(j.properties.Javac_shard_size))
1045 var shardSrcs []android.Paths
1046 if len(uniqueSrcFiles) > 0 {
1047 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1048 for idx, shardSrc := range shardSrcs {
1049 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1050 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1051 jars = append(jars, classes)
1052 }
1053 }
1054 if len(srcJars) > 0 {
1055 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1056 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1057 jars = append(jars, classes)
1058 }
1059 } else {
1060 classes := android.PathForModuleOut(ctx, "javac", jarName)
1061 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1062 jars = append(jars, classes)
1063 }
Colin Crossd6891432017-09-27 17:39:56 -07001064 if ctx.Failed() {
1065 return
1066 }
Colin Cross2fe66872015-03-30 17:20:39 -07001067 }
1068
Colin Crosscedd4762018-09-13 11:26:19 -07001069 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1070 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001071 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1072
1073 var resArgs []string
1074 var resDeps android.Paths
1075
1076 resArgs = append(resArgs, dirArgs...)
1077 resDeps = append(resDeps, dirDeps...)
1078
1079 resArgs = append(resArgs, fileArgs...)
1080 resDeps = append(resDeps, fileDeps...)
1081
Colin Crossff3ae9d2018-04-10 16:15:18 -07001082 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001083 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001084 resArgs = append(resArgs, srcArgs...)
1085 resDeps = append(resDeps, srcDeps...)
1086 }
Colin Cross40a36712017-09-27 17:41:35 -07001087
1088 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001089 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001090 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001091 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001092 if ctx.Failed() {
1093 return
1094 }
1095 }
1096
Colin Cross331a1212018-08-15 20:40:52 -07001097 if len(deps.staticResourceJars) > 0 {
1098 var jars android.Paths
1099 if j.resourceJar != nil {
1100 jars = append(jars, j.resourceJar)
1101 }
1102 jars = append(jars, deps.staticResourceJars...)
1103
1104 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1105 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1106 false, nil, nil)
1107 j.resourceJar = combinedJar
1108 }
1109
Colin Cross32f676a2017-09-06 13:41:06 -07001110 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001111 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001112
Colin Cross094054a2018-10-17 15:10:48 -07001113 manifest := j.overrideManifest
1114 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001115 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1116 }
Colin Cross635acc92017-09-12 22:50:46 -07001117
Colin Cross0a6e0072017-08-30 14:24:55 -07001118 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001119 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001120 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001121
1122 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001123 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1124 // Optimization: skip the combine step if there is nothing to do
1125 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1126 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1127 // any if len(jars) == 1.
1128 outputFile = moduleOutPath
1129 } else {
1130 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1131 ctx.Build(pctx, android.BuildParams{
1132 Rule: android.Cp,
1133 Input: jars[0],
1134 Output: combinedJar,
1135 })
1136 outputFile = combinedJar
1137 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001138 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001139 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001140 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001141 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001142 outputFile = combinedJar
1143 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001144
Colin Cross331a1212018-08-15 20:40:52 -07001145 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001146 if j.properties.Jarjar_rules != nil {
1147 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001148 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001149 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001150 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1151 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001152
1153 // jarjar resource jar if necessary
1154 if j.resourceJar != nil {
1155 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1156 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1157 j.resourceJar = resourceJarJarFile
1158 }
1159
Colin Cross0a6e0072017-08-30 14:24:55 -07001160 if ctx.Failed() {
1161 return
1162 }
1163 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001164 j.implementationJarFile = outputFile
1165 if j.headerJarFile == nil {
1166 j.headerJarFile = j.implementationJarFile
1167 }
Colin Cross2fe66872015-03-30 17:20:39 -07001168
Colin Cross6510f912017-11-29 00:27:14 -08001169 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001170 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1171 j.properties.Instrument = true
1172 }
1173 }
1174
Colin Cross3144dfc2018-01-03 15:06:47 -08001175 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001176 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1177 }
1178
Colin Cross331a1212018-08-15 20:40:52 -07001179 // merge implementation jar with resources if necessary
1180 implementationAndResourcesJar := outputFile
1181 if j.resourceJar != nil {
1182 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1183 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1184 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1185 false, nil, nil)
1186 implementationAndResourcesJar = combinedJar
1187 }
1188
1189 j.implementationAndResourcesJar = implementationAndResourcesJar
1190
Colin Cross9ae1b922018-06-26 17:59:05 -07001191 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001192 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001193 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001194 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001195 if ctx.Failed() {
1196 return
1197 }
Colin Cross331a1212018-08-15 20:40:52 -07001198
Colin Cross8faf8fc2019-01-16 15:15:52 -08001199 // Hidden API CSV generation and dex encoding
David Brazdil9fc36a62019-01-18 11:12:05 +00001200 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
1201 isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars())
1202 if isBootJar || inList(ctx.ModuleName(), ctx.Config().HiddenAPIExtraAppUsageJars()) {
1203 // Derive the greylist from classes jar.
1204 hiddenAPIGenerateCSV(ctx, j.implementationJarFile)
1205 }
1206 if isBootJar {
1207 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", jarName)
Colin Crosscd964b32019-01-18 22:03:02 -08001208 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexOutputFile, j.deviceProperties.UncompressDex)
David Brazdil9fc36a62019-01-18 11:12:05 +00001209 dexOutputFile = hiddenAPIJar
1210 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001211 }
1212
Colin Cross331a1212018-08-15 20:40:52 -07001213 // merge dex jar with resources if necessary
1214 if j.resourceJar != nil {
1215 jars := android.Paths{dexOutputFile, j.resourceJar}
1216 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1217 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1218 false, nil, nil)
1219 dexOutputFile = combinedJar
1220 }
1221
1222 j.dexJarFile = dexOutputFile
1223
Colin Cross8faf8fc2019-01-16 15:15:52 -08001224 // Dexpreopting
Colin Crossdc2da912019-01-05 22:13:05 -08001225 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross2fc72f62018-12-21 12:59:54 -08001226 j.dexpreopter.uncompressedDex = j.deviceProperties.UncompressDex
Colin Cross43f08db2018-11-12 10:13:39 -08001227 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1228
1229 j.maybeStrippedDexJarFile = dexOutputFile
1230
Colin Cross3063b782018-08-15 11:19:12 -07001231 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001232
1233 if ctx.Failed() {
1234 return
1235 }
Colin Cross331a1212018-08-15 20:40:52 -07001236 } else {
1237 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001238 }
Colin Cross331a1212018-08-15 20:40:52 -07001239
Colin Crossb7a63242015-04-16 14:09:14 -07001240 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001241
1242 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1243 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001244}
1245
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001246// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1247// since some of these flags may be used internally.
1248func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1249 for _, flag := range flags {
1250 flag = strings.TrimSpace(flag)
1251
1252 if !strings.HasPrefix(flag, "-") {
1253 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1254 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1255 ctx.PropertyErrorf("kotlincflags",
1256 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1257 } else if inList(flag, config.KotlincIllegalFlags) {
1258 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1259 } else if flag == "-include-runtime" {
1260 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1261 } else {
1262 args := strings.Split(flag, " ")
1263 if args[0] == "-kotlin-home" {
1264 ctx.PropertyErrorf("kotlincflags",
1265 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1266 }
1267 }
1268 }
1269}
1270
Colin Cross8eadbf02017-10-24 17:46:00 -07001271func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001272 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001273
1274 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001275 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001276 // Compile java sources into turbine.jar.
1277 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1278 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1279 if ctx.Failed() {
1280 return nil
1281 }
1282 jars = append(jars, turbineJar)
1283 }
1284
Colin Cross55f63ea2018-08-27 12:37:09 -07001285 jars = append(jars, extraJars...)
1286
Nan Zhanged19fc32017-10-19 13:06:22 -07001287 // Combine any static header libraries into classes-header.jar. If there is only
1288 // one input jar this step will be skipped.
1289 var headerJar android.Path
1290 jars = append(jars, deps.staticHeaderJars...)
1291
Colin Cross5c6ecc12017-10-23 18:12:27 -07001292 // we cannot skip the combine step for now if there is only one jar
1293 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1294 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001295 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1296 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001297 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001298
1299 if j.properties.Jarjar_rules != nil {
1300 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1301 // Transform classes.jar into classes-jarjar.jar
1302 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1303 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1304 headerJar = jarjarFile
1305 if ctx.Failed() {
1306 return nil
1307 }
1308 }
1309
1310 return headerJar
1311}
1312
Colin Crosscb933592017-11-22 13:49:43 -08001313func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001314 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001315
Colin Cross7a3139e2017-12-19 13:57:50 -08001316 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001317
Colin Cross84c38822018-01-03 15:59:46 -08001318 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001319 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1320
1321 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1322
1323 j.jacocoReportClassesFile = jacocoReportClassesFile
1324
1325 return instrumentedJar
1326}
1327
albaltai36ff7dc2018-12-25 14:35:23 +08001328var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001329
Nan Zhanged19fc32017-10-19 13:06:22 -07001330func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001331 if j.headerJarFile == nil {
1332 return nil
1333 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001334 return android.Paths{j.headerJarFile}
1335}
1336
1337func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001338 if j.implementationJarFile == nil {
1339 return nil
1340 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001341 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001342}
1343
Colin Cross331a1212018-08-15 20:40:52 -07001344func (j *Module) ResourceJars() android.Paths {
1345 if j.resourceJar == nil {
1346 return nil
1347 }
1348 return android.Paths{j.resourceJar}
1349}
1350
1351func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001352 if j.implementationAndResourcesJar == nil {
1353 return nil
1354 }
Colin Cross331a1212018-08-15 20:40:52 -07001355 return android.Paths{j.implementationAndResourcesJar}
1356}
1357
Colin Cross46c9b8b2017-06-22 16:51:17 -07001358func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001359 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001360 return j.exportAidlIncludeDirs
1361}
1362
Jiyong Park1be96912018-05-28 18:02:19 +09001363func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001364 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001365 return j.exportedSdkLibs
1366}
1367
Colin Cross46c9b8b2017-06-22 16:51:17 -07001368var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001369
Colin Cross46c9b8b2017-06-22 16:51:17 -07001370func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001371 return j.logtagsSrcs
1372}
1373
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001374// Collect information for opening IDE project files in java/jdeps.go.
1375func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1376 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1377 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1378 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1379 if j.properties.Jarjar_rules != nil {
1380 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1381 }
1382}
1383
1384func (j *Module) CompilerDeps() []string {
1385 jdeps := []string{}
1386 jdeps = append(jdeps, j.properties.Libs...)
1387 jdeps = append(jdeps, j.properties.Static_libs...)
1388 return jdeps
1389}
1390
Colin Cross2fe66872015-03-30 17:20:39 -07001391//
1392// Java libraries (.jar file)
1393//
1394
Colin Crossf506d872017-07-19 15:53:04 -07001395type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001396 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001397}
1398
Colin Cross2fc72f62018-12-21 12:59:54 -08001399func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001400 // Store uncompressed (and do not strip) dex files from boot class path jars that are not
1401 // part of the boot image.
1402 if inList(ctx.ModuleName(), ctx.Config().BootJars()) &&
1403 !inList(ctx.ModuleName(), ctx.Config().PreoptBootJars()) {
1404 return true
1405 }
Colin Cross2fc72f62018-12-21 12:59:54 -08001406 return false
1407}
1408
Colin Crossf506d872017-07-19 15:53:04 -07001409func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001410 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1411 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross2fc72f62018-12-21 12:59:54 -08001412 j.deviceProperties.UncompressDex = j.shouldUncompressDex(ctx)
Colin Cross46c9b8b2017-06-22 16:51:17 -07001413 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001414
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001415 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001416 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1417 ctx.ModuleName()+".jar", j.outputFile)
1418 }
Colin Crossb7a63242015-04-16 14:09:14 -07001419}
1420
Colin Crossf506d872017-07-19 15:53:04 -07001421func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001422 j.deps(ctx)
1423}
1424
Colin Cross9ae1b922018-06-26 17:59:05 -07001425func LibraryFactory() android.Module {
1426 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001427
Colin Cross9ae1b922018-06-26 17:59:05 -07001428 module.AddProperties(
1429 &module.Module.properties,
1430 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001431 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001432 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001433
Colin Cross9ae1b922018-06-26 17:59:05 -07001434 InitJavaModule(module, android.HostAndDeviceSupported)
1435 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001436}
1437
Colin Crossf506d872017-07-19 15:53:04 -07001438func LibraryHostFactory() android.Module {
1439 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001440
Colin Cross6af17aa2017-09-20 12:59:05 -07001441 module.AddProperties(
1442 &module.Module.properties,
1443 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001444
Colin Cross9ae1b922018-06-26 17:59:05 -07001445 module.Module.properties.Installable = proptools.BoolPtr(true)
1446
Colin Cross89536d42017-07-07 14:35:50 -07001447 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001448 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001449}
1450
1451//
Colin Crossb628ea52018-08-14 16:42:33 -07001452// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001453//
1454
1455type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001456 // list of compatibility suites (for example "cts", "vts") that the module should be
1457 // installed into.
1458 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001459
1460 // the name of the test configuration (for example "AndroidTest.xml") that should be
1461 // installed with the module.
1462 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001463
Jack He33338892018-09-19 02:21:28 -07001464 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1465 // should be installed with the module.
1466 Test_config_template *string `android:"arch_variant"`
1467
Colin Crossd96ca352018-08-10 16:06:24 -07001468 // list of files or filegroup modules that provide data that should be installed alongside
1469 // the test
1470 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001471}
1472
1473type Test struct {
1474 Library
1475
1476 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001477
1478 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001479 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001480}
1481
1482func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001483 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001484 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001485
1486 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001487}
1488
1489func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1490 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001491 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001492 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001493 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001494}
1495
1496func TestFactory() android.Module {
1497 module := &Test{}
1498
1499 module.AddProperties(
1500 &module.Module.properties,
1501 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001502 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001503 &module.Module.protoProperties,
1504 &module.testProperties)
1505
Colin Cross9ae1b922018-06-26 17:59:05 -07001506 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001507 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001508
Colin Cross05638fc2018-04-09 18:40:24 -07001509 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001510 return module
1511}
1512
1513func TestHostFactory() android.Module {
1514 module := &Test{}
1515
1516 module.AddProperties(
1517 &module.Module.properties,
1518 &module.Module.protoProperties,
1519 &module.testProperties)
1520
Colin Cross9ae1b922018-06-26 17:59:05 -07001521 module.Module.properties.Installable = proptools.BoolPtr(true)
1522
Colin Cross05638fc2018-04-09 18:40:24 -07001523 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001524 return module
1525}
1526
1527//
Colin Cross2fe66872015-03-30 17:20:39 -07001528// Java Binaries (.jar file plus wrapper script)
1529//
1530
Colin Crossf506d872017-07-19 15:53:04 -07001531type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001532 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001533 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001534
1535 // Name of the class containing main to be inserted into the manifest as Main-Class.
1536 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001537}
1538
Colin Crossf506d872017-07-19 15:53:04 -07001539type Binary struct {
1540 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001541
Colin Crossf506d872017-07-19 15:53:04 -07001542 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001543
Colin Cross6b4a32d2017-12-05 13:42:45 -08001544 isWrapperVariant bool
1545
Colin Crossc3315992017-12-08 19:12:36 -08001546 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001547 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001548}
1549
Alex Light24237172017-10-26 09:46:21 -07001550func (j *Binary) HostToolPath() android.OptionalPath {
1551 return android.OptionalPathForPath(j.binaryFile)
1552}
1553
Colin Crossf506d872017-07-19 15:53:04 -07001554func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001555 if ctx.Arch().ArchType == android.Common {
1556 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001557 if j.binaryProperties.Main_class != nil {
1558 if j.properties.Manifest != nil {
1559 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1560 }
1561 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1562 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1563 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1564 }
1565
Colin Cross6b4a32d2017-12-05 13:42:45 -08001566 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001567 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001568 // Handle the binary wrapper
1569 j.isWrapperVariant = true
1570
Colin Cross366938f2017-12-11 16:29:02 -08001571 if j.binaryProperties.Wrapper != nil {
1572 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001573 } else {
1574 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1575 }
1576
1577 // Depend on the installed jar so that the wrapper doesn't get executed by
1578 // another build rule before the jar has been installed.
1579 jarFile := ctx.PrimaryModule().(*Binary).installFile
1580
1581 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1582 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001583 }
Colin Cross2fe66872015-03-30 17:20:39 -07001584}
1585
Colin Crossf506d872017-07-19 15:53:04 -07001586func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001587 if ctx.Arch().ArchType == android.Common {
1588 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001589 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001590 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001591 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001592}
1593
Colin Crossf506d872017-07-19 15:53:04 -07001594func BinaryFactory() android.Module {
1595 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001596
Colin Cross36242852017-06-23 15:06:31 -07001597 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001598 &module.Module.properties,
1599 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001600 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001601 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001602 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001603
Colin Cross9ae1b922018-06-26 17:59:05 -07001604 module.Module.properties.Installable = proptools.BoolPtr(true)
1605
Colin Cross6b4a32d2017-12-05 13:42:45 -08001606 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1607 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001608 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001609}
1610
Colin Crossf506d872017-07-19 15:53:04 -07001611func BinaryHostFactory() android.Module {
1612 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001613
Colin Cross36242852017-06-23 15:06:31 -07001614 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001615 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001616 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001617 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001618
Colin Cross9ae1b922018-06-26 17:59:05 -07001619 module.Module.properties.Installable = proptools.BoolPtr(true)
1620
Colin Cross6b4a32d2017-12-05 13:42:45 -08001621 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1622 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001623 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001624}
1625
1626//
1627// Java prebuilts
1628//
1629
Colin Cross74d73e22017-08-02 11:05:49 -07001630type ImportProperties struct {
1631 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001632
Nan Zhangea568a42017-11-08 21:20:04 -08001633 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001634
1635 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001636
1637 // List of shared java libs that this module has dependencies to
1638 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001639
1640 // List of files to remove from the jar file(s)
1641 Exclude_files []string
1642
1643 // List of directories to remove from the jar file(s)
1644 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001645
1646 // if set to true, run Jetifier against .jar file. Defaults to false.
1647 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001648}
1649
1650type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001651 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001652 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001653 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001654
Colin Cross74d73e22017-08-02 11:05:49 -07001655 properties ImportProperties
1656
Colin Cross0a6e0072017-08-30 14:24:55 -07001657 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001658 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001659}
1660
Colin Cross83bb3162018-06-25 15:48:06 -07001661func (j *Import) sdkVersion() string {
1662 return String(j.properties.Sdk_version)
1663}
1664
1665func (j *Import) minSdkVersion() string {
1666 return j.sdkVersion()
1667}
1668
Colin Cross74d73e22017-08-02 11:05:49 -07001669func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001670 return &j.prebuilt
1671}
1672
Colin Cross74d73e22017-08-02 11:05:49 -07001673func (j *Import) PrebuiltSrcs() []string {
1674 return j.properties.Jars
1675}
1676
1677func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001678 return j.prebuilt.Name(j.ModuleBase.Name())
1679}
1680
Colin Cross74d73e22017-08-02 11:05:49 -07001681func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001682 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001683 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001684}
1685
Colin Cross74d73e22017-08-02 11:05:49 -07001686func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001687 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001688
Nan Zhang4c819fb2018-08-27 18:31:46 -07001689 jarName := ctx.ModuleName() + ".jar"
1690 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001691 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1692 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001693 if Bool(j.properties.Jetifier_enabled) {
1694 inputFile := outputFile
1695 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1696 TransformJetifier(ctx, outputFile, inputFile)
1697 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001698 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001699
1700 ctx.VisitDirectDeps(func(module android.Module) {
1701 otherName := ctx.OtherModuleName(module)
1702 tag := ctx.OtherModuleDependencyTag(module)
1703
1704 switch dep := module.(type) {
1705 case Dependency:
1706 switch tag {
1707 case libTag, staticLibTag:
1708 // sdk lib names from dependencies are re-exported
1709 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1710 }
1711 case SdkLibraryDependency:
1712 switch tag {
1713 case libTag:
1714 // names of sdk libs that are directly depended are exported
1715 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1716 }
1717 }
1718 })
1719
1720 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001721 if Bool(j.properties.Installable) {
1722 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1723 ctx.ModuleName()+".jar", outputFile)
1724 }
Colin Cross2fe66872015-03-30 17:20:39 -07001725}
1726
Colin Cross74d73e22017-08-02 11:05:49 -07001727var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001728
Nan Zhanged19fc32017-10-19 13:06:22 -07001729func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001730 if j.combinedClasspathFile == nil {
1731 return nil
1732 }
Colin Cross37f6d792018-07-12 12:28:41 -07001733 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001734}
1735
1736func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001737 if j.combinedClasspathFile == nil {
1738 return nil
1739 }
Colin Cross37f6d792018-07-12 12:28:41 -07001740 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001741}
1742
Colin Cross331a1212018-08-15 20:40:52 -07001743func (j *Import) ResourceJars() android.Paths {
1744 return nil
1745}
1746
1747func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001748 if j.combinedClasspathFile == nil {
1749 return nil
1750 }
Colin Cross331a1212018-08-15 20:40:52 -07001751 return android.Paths{j.combinedClasspathFile}
1752}
1753
Colin Cross74d73e22017-08-02 11:05:49 -07001754func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001755 return nil
1756}
1757
Jiyong Park1be96912018-05-28 18:02:19 +09001758func (j *Import) ExportedSdkLibs() []string {
1759 return j.exportedSdkLibs
1760}
1761
albaltai36ff7dc2018-12-25 14:35:23 +08001762// Add compile time check for interface implementation
1763var _ android.IDEInfo = (*Import)(nil)
1764var _ android.IDECustomizedModuleName = (*Import)(nil)
1765
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001766// Collect information for opening IDE project files in java/jdeps.go.
1767const (
1768 removedPrefix = "prebuilt_"
1769)
1770
1771func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1772 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1773}
1774
1775func (j *Import) IDECustomizedModuleName() string {
1776 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1777 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1778 // solution to get the Import name.
1779 name := j.Name()
1780 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001781 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001782 }
1783 return name
1784}
1785
Colin Cross74d73e22017-08-02 11:05:49 -07001786var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001787
Colin Cross74d73e22017-08-02 11:05:49 -07001788func ImportFactory() android.Module {
1789 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001790
Colin Cross74d73e22017-08-02 11:05:49 -07001791 module.AddProperties(&module.properties)
1792
1793 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001794 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001795 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001796}
1797
Colin Cross74d73e22017-08-02 11:05:49 -07001798func ImportFactoryHost() android.Module {
1799 module := &Import{}
1800
1801 module.AddProperties(&module.properties)
1802
1803 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001804 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001805 return module
1806}
1807
Colin Cross89536d42017-07-07 14:35:50 -07001808//
1809// Defaults
1810//
1811type Defaults struct {
1812 android.ModuleBase
1813 android.DefaultsModuleBase
1814}
1815
1816func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1817}
1818
1819func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1820}
1821
1822func defaultsFactory() android.Module {
1823 return DefaultsFactory()
1824}
1825
1826func DefaultsFactory(props ...interface{}) android.Module {
1827 module := &Defaults{}
1828
1829 module.AddProperties(props...)
1830 module.AddProperties(
1831 &CompilerProperties{},
1832 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001833 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001834 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001835 &aaptProperties{},
1836 &androidLibraryProperties{},
1837 &appProperties{},
1838 &appTestProperties{},
1839 &ImportProperties{},
1840 &AARImportProperties{},
1841 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001842 )
1843
1844 android.InitDefaultsModule(module)
1845
1846 return module
1847}
Nan Zhangea568a42017-11-08 21:20:04 -08001848
1849var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001850var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001851var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001852var inList = android.InList