blob: aea1ef90da5ec9e5adc072b2bbfd721d4d4f1375 [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 {
339 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900340 ImplementationJars(linkType linkType) 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"}
382 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
383 certificateTag = dependencyTag{name: "certificate"}
384 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700385)
Colin Cross2fe66872015-03-30 17:20:39 -0700386
Colin Crossfc3674a2017-09-18 17:41:52 -0700387type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700388 useModule, useFiles, useDefaultLibs, invalidVersion bool
389
Colin Cross86a60ae2018-05-29 14:44:55 -0700390 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700391 systemModules string
392
Colin Crossa97c5d32018-03-28 14:58:31 -0700393 frameworkResModule string
394
Colin Cross86a60ae2018-05-29 14:44:55 -0700395 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700396 aidl android.Path
397}
398
Colin Crossa4f08812018-10-02 22:03:40 -0700399type jniLib struct {
400 name string
401 path android.Path
402 target android.Target
403}
404
Colin Cross3144dfc2018-01-03 15:06:47 -0800405func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
406 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
407}
408
409func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
410 return j.shouldInstrument(ctx) &&
411 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
412 ctx.Config().UnbundledBuild())
413}
414
Colin Cross83bb3162018-06-25 15:48:06 -0700415func (j *Module) sdkVersion() string {
416 return String(j.deviceProperties.Sdk_version)
417}
418
419func (j *Module) minSdkVersion() string {
420 if j.deviceProperties.Min_sdk_version != nil {
421 return *j.deviceProperties.Min_sdk_version
422 }
423 return j.sdkVersion()
424}
425
Dan Willemsen419290a2018-10-31 15:28:47 -0700426func (j *Module) targetSdkVersion() string {
427 if j.deviceProperties.Target_sdk_version != nil {
428 return *j.deviceProperties.Target_sdk_version
429 }
430 return j.sdkVersion()
431}
432
Colin Cross83bb3162018-06-25 15:48:06 -0700433type sdkContext interface {
434 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
435 sdkVersion() string
436 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
437 minSdkVersion() string
Dan Willemsen419290a2018-10-31 15:28:47 -0700438 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
439 targetSdkVersion() string
Colin Cross83bb3162018-06-25 15:48:06 -0700440}
441
442func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
443 switch v {
Neil Fuller401eeba2018-10-18 19:48:58 +0100444 case "", "current", "system_current", "test_current", "core_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700445 return ctx.Config().DefaultAppTargetSdk()
446 default:
447 return v
448 }
449}
450
451// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
452// it returns android.FutureApiLevel (10000).
453func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
454 switch v {
Neil Fuller401eeba2018-10-18 19:48:58 +0100455 case "", "current", "test_current", "system_current", "core_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700456 return ctx.Config().DefaultAppTargetSdkInt(), nil
457 default:
458 n := android.GetNumericSdkVersion(v)
459 if i, err := strconv.Atoi(n); err != nil {
460 return -1, fmt.Errorf("invalid sdk version %q", n)
461 } else {
462 return i, nil
463 }
464 }
465}
466
467func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
468 n, err := sdkVersionToNumber(ctx, v)
469 if err != nil {
470 return "", err
471 }
472 return strconv.Itoa(n), nil
473}
474
475func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
476 v := sdkContext.sdkVersion()
477 i, err := sdkVersionToNumber(ctx, v)
478 if err != nil {
479 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700480 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700481 }
482
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900483 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
484 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
485 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
486 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
487 if ctx.DeviceSpecific() || ctx.SocSpecific() {
488 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
489 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
490 }
491 }
492 version := strings.TrimPrefix(v, "system_")
493 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
494 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
495 v, allowed_versions)
496 }
497 }
498
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100499 toPrebuilt := func(sdk string) sdkDep {
500 var api, v string
501 if strings.Contains(sdk, "_") {
502 t := strings.Split(sdk, "_")
503 api = t[0]
504 v = t[1]
505 } else {
506 api = "public"
507 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900508 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100509 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700510 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100511 // There's no aidl for other SDKs yet.
512 // TODO(77525052): Add aidl files for other SDKs too.
513 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
514 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800515 jarPath := android.ExistentPathForSource(ctx, jar)
516 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700517 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700518
Colin Cross6510f912017-11-29 00:27:14 -0800519 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700520 return sdkDep{
521 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700522 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700523 }
524 }
525
Colin Crossfc3674a2017-09-18 17:41:52 -0700526 if !jarPath.Valid() {
527 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
528 return sdkDep{}
529 }
Colin Cross47ff2522017-10-02 14:22:08 -0700530
Colin Crossfc3674a2017-09-18 17:41:52 -0700531 if !aidlPath.Valid() {
532 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
533 return sdkDep{}
534 }
Colin Cross47ff2522017-10-02 14:22:08 -0700535
Colin Crossfc3674a2017-09-18 17:41:52 -0700536 return sdkDep{
537 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700538 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700539 aidl: aidlPath.Path(),
540 }
541 }
542
Colin Crossa97c5d32018-03-28 14:58:31 -0700543 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700544 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700545 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700546 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700547 systemModules: m + "_system_modules",
548 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700549 }
550 if m == "core.current.stubs" {
551 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100552 } else if m == "core.platform.api.stubs" {
553 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700554 }
555 return ret
556 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700557
Colin Cross6510f912017-11-29 00:27:14 -0800558 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100559 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700560 }
561
562 switch v {
563 case "":
564 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700565 useDefaultLibs: true,
566 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700567 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700568 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700569 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700570 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700571 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700572 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700573 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700574 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700575 return toModule("core.current.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700576 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100577 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700578 }
579}
580
Colin Crossbe1da472017-07-07 15:59:46 -0700581func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700582 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700583 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700584 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700585 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700586 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100587 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700588 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700589 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700590 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700591 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100592 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700593 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800594 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700595 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
596 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800597 }
Colin Crossbe1da472017-07-07 15:59:46 -0700598 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700599 } else if j.deviceProperties.System_modules == nil {
600 ctx.PropertyErrorf("no_standard_libs",
601 "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 +0100602 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700603 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700604 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100605 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700606 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800607 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800608 if ctx.ModuleName() == "android_stubs_current" ||
609 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700610 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700611 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800612 }
Colin Cross2fe66872015-03-30 17:20:39 -0700613 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700614
Colin Cross42d48b72018-08-29 14:10:52 -0700615 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
616 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100617 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700618 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100619 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700620
Colin Cross7f9036c2017-08-30 13:27:57 -0700621 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000622 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700623 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800624 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700625
626 if j.hasSrcExt(".proto") {
627 protoDeps(ctx, &j.protoProperties)
628 }
Colin Cross93e85952017-08-15 13:34:18 -0700629
630 if j.hasSrcExt(".kt") {
631 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
632 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700633 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700634 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800635
636 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700637 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800638 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700639}
640
641func hasSrcExt(srcs []string, ext string) bool {
642 for _, src := range srcs {
643 if filepath.Ext(src) == ext {
644 return true
645 }
646 }
647
648 return false
649}
650
Nan Zhang61eaedb2017-11-02 13:28:15 -0700651func shardPaths(paths android.Paths, shardSize int) []android.Paths {
652 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
653 for len(paths) > shardSize {
654 ret = append(ret, paths[0:shardSize])
655 paths = paths[shardSize:]
656 }
657 if len(paths) > 0 {
658 ret = append(ret, paths)
659 }
660 return ret
661}
662
Colin Cross6af17aa2017-09-20 12:59:05 -0700663func (j *Module) hasSrcExt(ext string) bool {
664 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700665}
666
Colin Cross46c9b8b2017-06-22 16:51:17 -0700667func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700668 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700669
Colin Crossebe1a512017-11-14 13:12:14 -0800670 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
671 aidlIncludes = append(aidlIncludes,
672 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
673 aidlIncludes = append(aidlIncludes,
674 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700675
Steven Moreland667f6882018-07-26 12:55:08 -0700676 flags := []string{"-b"}
677
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700678 if aidlPreprocess.Valid() {
679 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700680 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700681 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700682 }
683
Colin Cross635c3b02016-05-18 15:37:25 -0700684 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800685 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700686 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800687 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700688 flags = append(flags, "-I"+src.String())
689 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700690
Martijn Coeneneab15642018-03-09 09:29:59 +0100691 if Bool(j.deviceProperties.Aidl.Generate_traces) {
692 flags = append(flags, "-t")
693 }
694
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100695 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
696 flags = append(flags, "--transaction_names")
697 }
698
Colin Crossf03c82b2015-04-13 13:53:40 -0700699 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700700}
701
Colin Cross32f676a2017-09-06 13:41:06 -0700702type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800703 classpath classpath
704 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700705 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700706 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700707 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700708 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700709 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800710 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700711 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700712 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700713 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700714 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700715}
Colin Cross2fe66872015-03-30 17:20:39 -0700716
Colin Cross54250902017-12-05 09:28:08 -0800717func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
718 for _, f := range dep.Srcs() {
719 if f.Ext() != ".jar" {
720 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
721 ctx.OtherModuleName(dep.(blueprint.Module)))
722 }
723 }
724}
725
Jiyong Park2d492942018-03-05 17:44:10 +0900726type linkType int
727
728const (
729 javaCore linkType = iota
730 javaSdk
731 javaSystem
732 javaPlatform
733)
734
Jiyong Park46f78fb2018-10-20 16:33:17 +0900735func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700736 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700737 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900738 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
739 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
740 name == "core-lambda-stubs":
741 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100742 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900743 return javaCore, false
744 case name == "android_system_stubs_current":
745 return javaSystem, true
746 case strings.HasPrefix(ver, "system_"):
747 return javaSystem, false
748 case name == "android_test_stubs_current":
749 return javaSystem, true
750 case strings.HasPrefix(ver, "test_"):
751 return javaPlatform, false
752 case name == "android_stubs_current":
753 return javaSdk, true
754 case ver == "current":
755 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700756 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900757 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700758 default:
759 if _, err := strconv.Atoi(ver); err != nil {
760 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
761 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900762 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900763 }
764}
765
Jiyong Park750e5572018-01-31 00:20:13 +0900766func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700767 if ctx.Host() {
768 return
769 }
770
Jiyong Park46f78fb2018-10-20 16:33:17 +0900771 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
772 if stubs {
773 return
774 }
775 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900776 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."
777
778 switch myLinkType {
779 case javaCore:
780 if otherLinkType != javaCore {
781 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 +0900782 ctx.OtherModuleName(to))
783 }
Jiyong Park2d492942018-03-05 17:44:10 +0900784 break
785 case javaSdk:
786 if otherLinkType != javaCore && otherLinkType != javaSdk {
787 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
788 ctx.OtherModuleName(to))
789 }
790 break
791 case javaSystem:
792 if otherLinkType == javaPlatform {
793 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
794 ctx.OtherModuleName(to))
795 }
796 break
797 case javaPlatform:
798 // no restriction on link-type
799 break
Jiyong Park750e5572018-01-31 00:20:13 +0900800 }
801}
802
Colin Cross32f676a2017-09-06 13:41:06 -0700803func (j *Module) collectDeps(ctx android.ModuleContext) deps {
804 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700805
Colin Cross300f0382018-03-06 13:11:51 -0800806 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700807 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800808 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700809 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800810 } else if sdkDep.useFiles {
811 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700812 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800813 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
814 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700815 }
816
Colin Crossd11fcda2017-10-23 17:59:01 -0700817 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700818 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700819 tag := ctx.OtherModuleDependencyTag(module)
820
Colin Crossa4f08812018-10-02 22:03:40 -0700821 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700822 // Handled by AndroidApp.collectAppDeps
823 return
824 }
825 if tag == certificateTag {
826 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700827 return
828 }
829
Jiyong Park750e5572018-01-31 00:20:13 +0900830 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700831 switch tag {
832 case bootClasspathTag, libTag, staticLibTag:
833 checkLinkType(ctx, j, to, tag.(dependencyTag))
834 }
Jiyong Park750e5572018-01-31 00:20:13 +0900835 }
Colin Cross54250902017-12-05 09:28:08 -0800836 switch dep := module.(type) {
837 case Dependency:
838 switch tag {
839 case bootClasspathTag:
840 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700841 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800842 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900843 // sdk lib names from dependencies are re-exported
844 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800845 case staticLibTag:
846 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
847 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
848 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700849 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900850 // sdk lib names from dependencies are re-exported
851 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700852 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700853 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800854 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100855 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800856 // framework.jar has a one-off dependency on the R.java and Manifest.java files
857 // generated by framework-res.apk
858 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
859 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800860 case frameworkApkTag:
861 if ctx.ModuleName() == "android_stubs_current" ||
862 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700863 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800864 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
865 // resource files out of there for aapt.
866 //
867 // Normally the package rule runs aapt, which includes the resource,
868 // but we're not running that in our package rule so just copy in the
869 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700870 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800871 }
Colin Cross54250902017-12-05 09:28:08 -0800872 case kotlinStdlibTag:
873 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800874 }
875
876 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900877 case SdkLibraryDependency:
878 switch tag {
879 case libTag:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900880 linkType, _ := getLinkType(j, ctx.ModuleName())
881 deps.classpath = append(deps.classpath, dep.HeaderJars(linkType)...)
Jiyong Park1be96912018-05-28 18:02:19 +0900882 // names of sdk libs that are directly depended are exported
883 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900884 default:
885 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
886 }
Colin Cross54250902017-12-05 09:28:08 -0800887 case android.SourceFileProducer:
888 switch tag {
889 case libTag:
890 checkProducesJars(ctx, dep)
891 deps.classpath = append(deps.classpath, dep.Srcs()...)
892 case staticLibTag:
893 checkProducesJars(ctx, dep)
894 deps.classpath = append(deps.classpath, dep.Srcs()...)
895 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
896 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
897 case android.DefaultsDepTag, android.SourceDepTag:
898 // Nothing to do
899 default:
900 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
901 }
902 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700903 switch tag {
904 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700905 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700906 case systemModulesTag:
907 if deps.systemModules != nil {
908 panic("Found two system module dependencies")
909 }
910 sm := module.(*SystemModules)
911 if sm.outputFile == nil {
912 panic("Missing directory for system module dependency")
913 }
914 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700915 default:
916 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700917 }
Colin Crossec7a0422017-07-07 14:47:12 -0700918 }
Colin Cross2fe66872015-03-30 17:20:39 -0700919 })
920
Jiyong Park1be96912018-05-28 18:02:19 +0900921 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
922
Colin Cross32f676a2017-09-06 13:41:06 -0700923 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700924}
925
Colin Cross83bb3162018-06-25 15:48:06 -0700926func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700927 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700928 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
929 if err != nil {
930 ctx.PropertyErrorf("sdk_version", "%s", err)
931 }
Nan Zhang357466b2018-04-17 17:38:36 -0700932 if javaVersion != "" {
933 ret = javaVersion
934 } else if ctx.Device() && sdk <= 23 {
935 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900936 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700937 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700938 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700939 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
940 ret = "1.8"
941 } else {
942 ret = "1.9"
943 }
944
945 return ret
946}
947
Nan Zhanged19fc32017-10-19 13:06:22 -0700948func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700949
Colin Crossf03c82b2015-04-13 13:53:40 -0700950 var flags javaBuilderFlags
951
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100952 // javaVersion flag.
953 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
954
Nan Zhanged19fc32017-10-19 13:06:22 -0700955 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700956 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100957 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700958 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700959 }
Colin Cross6510f912017-11-29 00:27:14 -0800960 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700961 // Override the -g flag passed globally to remove local variable debug info to reduce
962 // disk and memory usage.
963 javacFlags = append(javacFlags, "-g:source,lines")
964 }
Colin Cross64162712017-08-08 13:17:59 -0700965
Colin Cross66548102018-06-19 22:47:35 -0700966 if ctx.Config().RunErrorProne() {
967 if config.ErrorProneClasspath == nil {
968 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
969 }
970
971 errorProneFlags := []string{
972 "-Xplugin:ErrorProne",
973 "${config.ErrorProneChecks}",
974 }
975 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
976
977 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
978 "'" + strings.Join(errorProneFlags, " ") + "'"
979 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800980 }
981
Nan Zhanged19fc32017-10-19 13:06:22 -0700982 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800983 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
984 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700985 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800986
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100987 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800988 !Bool(j.properties.No_standard_libs) &&
989 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
990 // Give host-side tools a version of OpenJDK's standard libraries
991 // close to what they're targeting. As of Dec 2017, AOSP is only
992 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
993 //
994 // When building with OpenJDK 8, the following should have no
995 // effect since those jars would be available by default.
996 //
997 // When building with OpenJDK 9 but targeting a version < 1.8,
998 // putting them on the bootclasspath means that:
999 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1000 // b) references to existing APIs are not reinterpreted in an
1001 // OpenJDK 9-specific way, eg. calls to subclasses of
1002 // java.nio.Buffer as in http://b/70862583
1003 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1004 flags.bootClasspath = append(flags.bootClasspath,
1005 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1006 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001007 if Bool(j.properties.Use_tools_jar) {
1008 flags.bootClasspath = append(flags.bootClasspath,
1009 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1010 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001011 }
1012
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001013 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -07001014 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
1015 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
1016 }
1017
Nan Zhanged19fc32017-10-19 13:06:22 -07001018 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001019 if deps.systemModules != nil {
1020 flags.systemModules = append(flags.systemModules, deps.systemModules)
1021 }
1022
Nan Zhanged19fc32017-10-19 13:06:22 -07001023 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -07001024 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -07001025 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001026 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001027 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1028 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001029 }
1030
Colin Cross81440082018-08-15 20:21:55 -07001031 if len(javacFlags) > 0 {
1032 // optimization.
1033 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1034 flags.javacFlags = "$javacFlags"
1035 }
1036
Nan Zhanged19fc32017-10-19 13:06:22 -07001037 return flags
1038}
Colin Crossc0b06f12015-04-08 13:03:43 -07001039
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001040func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001041
Colin Crossebe1a512017-11-14 13:12:14 -08001042 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001043
1044 deps := j.collectDeps(ctx)
1045 flags := j.collectBuilderFlags(ctx, deps)
1046
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001047 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001048 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1049 }
1050 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001051 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001052 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001053 }
1054
Colin Crossaf050172017-11-15 23:01:59 -08001055 srcFiles = j.genSources(ctx, srcFiles, flags)
1056
1057 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001058 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001059 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001060
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001061 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1062 // that IDEInfo struct will use
1063 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1064
Colin Cross1ee23172017-10-18 14:44:18 -07001065 jarName := ctx.ModuleName() + ".jar"
1066
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001067 javaSrcFiles := srcFiles.FilterByExt(".java")
1068 var uniqueSrcFiles android.Paths
1069 set := make(map[string]bool)
1070 for _, v := range javaSrcFiles {
1071 if _, found := set[v.String()]; !found {
1072 set[v.String()] = true
1073 uniqueSrcFiles = append(uniqueSrcFiles, v)
1074 }
1075 }
1076
Colin Cross55f63ea2018-08-27 12:37:09 -07001077 var kotlinJars android.Paths
1078
Colin Cross93e85952017-08-15 13:34:18 -07001079 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001080 // user defined kotlin flags.
1081 kotlincFlags := j.properties.Kotlincflags
1082 CheckKotlincFlags(ctx, kotlincFlags)
1083
Colin Cross93e85952017-08-15 13:34:18 -07001084 // If there are kotlin files, compile them first but pass all the kotlin and java files
1085 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1086 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001087 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001088 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001089 kotlincFlags = append(kotlincFlags, "-no-jdk")
1090 }
1091 if len(kotlincFlags) > 0 {
1092 // optimization.
1093 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1094 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001095 }
1096
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001097 var kotlinSrcFiles android.Paths
1098 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1099 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1100
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001101 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001102 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1103 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1104
Colin Cross1ee23172017-10-18 14:44:18 -07001105 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001106 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001107 if ctx.Failed() {
1108 return
1109 }
1110
1111 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001112 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001113 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001114
Colin Cross93e85952017-08-15 13:34:18 -07001115 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001116 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001117 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001118 }
1119
Colin Cross55f63ea2018-08-27 12:37:09 -07001120 jars := append(android.Paths(nil), kotlinJars...)
1121
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001122 // Store the list of .java files that was passed to javac
1123 j.compiledJavaSrcs = uniqueSrcFiles
1124 j.compiledSrcJars = srcJars
1125
Nan Zhang61eaedb2017-11-02 13:28:15 -07001126 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001127 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001128 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1129 enable_sharding = true
1130 if len(j.properties.Annotation_processors) != 0 ||
1131 len(j.properties.Annotation_processor_classes) != 0 {
1132 ctx.PropertyErrorf("javac_shard_size",
1133 "%q cannot be set when annotation processors are enabled.",
1134 j.properties.Javac_shard_size)
1135 }
1136 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001137 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001138 if ctx.Failed() {
1139 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001140 }
1141 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001142 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001143 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001144 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001145 // If error-prone is enabled, add an additional rule to compile the java files into
1146 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001147 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001148 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1149 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001150 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001151 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001152 extraJarDeps = append(extraJarDeps, errorprone)
1153 }
1154
Nan Zhang61eaedb2017-11-02 13:28:15 -07001155 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001156 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001157 shardSize := int(*(j.properties.Javac_shard_size))
1158 var shardSrcs []android.Paths
1159 if len(uniqueSrcFiles) > 0 {
1160 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1161 for idx, shardSrc := range shardSrcs {
1162 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1163 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1164 jars = append(jars, classes)
1165 }
1166 }
1167 if len(srcJars) > 0 {
1168 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1169 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1170 jars = append(jars, classes)
1171 }
1172 } else {
1173 classes := android.PathForModuleOut(ctx, "javac", jarName)
1174 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1175 jars = append(jars, classes)
1176 }
Colin Crossd6891432017-09-27 17:39:56 -07001177 if ctx.Failed() {
1178 return
1179 }
Colin Cross2fe66872015-03-30 17:20:39 -07001180 }
1181
Colin Crosscedd4762018-09-13 11:26:19 -07001182 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1183 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001184 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1185
1186 var resArgs []string
1187 var resDeps android.Paths
1188
1189 resArgs = append(resArgs, dirArgs...)
1190 resDeps = append(resDeps, dirDeps...)
1191
1192 resArgs = append(resArgs, fileArgs...)
1193 resDeps = append(resDeps, fileDeps...)
1194
Colin Crossff3ae9d2018-04-10 16:15:18 -07001195 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001196 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001197 resArgs = append(resArgs, srcArgs...)
1198 resDeps = append(resDeps, srcDeps...)
1199 }
Colin Cross40a36712017-09-27 17:41:35 -07001200
1201 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001202 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001203 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001204 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001205 if ctx.Failed() {
1206 return
1207 }
1208 }
1209
Colin Cross331a1212018-08-15 20:40:52 -07001210 if len(deps.staticResourceJars) > 0 {
1211 var jars android.Paths
1212 if j.resourceJar != nil {
1213 jars = append(jars, j.resourceJar)
1214 }
1215 jars = append(jars, deps.staticResourceJars...)
1216
1217 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1218 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1219 false, nil, nil)
1220 j.resourceJar = combinedJar
1221 }
1222
Colin Cross32f676a2017-09-06 13:41:06 -07001223 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001224 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001225
Colin Cross094054a2018-10-17 15:10:48 -07001226 manifest := j.overrideManifest
1227 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001228 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1229 }
Colin Cross635acc92017-09-12 22:50:46 -07001230
Colin Cross0a6e0072017-08-30 14:24:55 -07001231 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001232 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001233 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001234
1235 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001236 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1237 // Optimization: skip the combine step if there is nothing to do
1238 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1239 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1240 // any if len(jars) == 1.
1241 outputFile = moduleOutPath
1242 } else {
1243 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1244 ctx.Build(pctx, android.BuildParams{
1245 Rule: android.Cp,
1246 Input: jars[0],
1247 Output: combinedJar,
1248 })
1249 outputFile = combinedJar
1250 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001251 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001252 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001253 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001254 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001255 outputFile = combinedJar
1256 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001257
Colin Cross331a1212018-08-15 20:40:52 -07001258 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001259 if j.properties.Jarjar_rules != nil {
1260 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001261 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001262 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001263 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1264 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001265
1266 // jarjar resource jar if necessary
1267 if j.resourceJar != nil {
1268 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1269 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1270 j.resourceJar = resourceJarJarFile
1271 }
1272
Colin Cross0a6e0072017-08-30 14:24:55 -07001273 if ctx.Failed() {
1274 return
1275 }
1276 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001277 j.implementationJarFile = outputFile
1278 if j.headerJarFile == nil {
1279 j.headerJarFile = j.implementationJarFile
1280 }
Colin Cross2fe66872015-03-30 17:20:39 -07001281
Colin Cross6510f912017-11-29 00:27:14 -08001282 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001283 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1284 j.properties.Instrument = true
1285 }
1286 }
1287
Colin Cross3144dfc2018-01-03 15:06:47 -08001288 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001289 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1290 }
1291
Colin Cross331a1212018-08-15 20:40:52 -07001292 // merge implementation jar with resources if necessary
1293 implementationAndResourcesJar := outputFile
1294 if j.resourceJar != nil {
1295 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1296 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1297 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1298 false, nil, nil)
1299 implementationAndResourcesJar = combinedJar
1300 }
1301
1302 j.implementationAndResourcesJar = implementationAndResourcesJar
1303
Colin Cross9ae1b922018-06-26 17:59:05 -07001304 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001305 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001306 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001307 if ctx.Failed() {
1308 return
1309 }
Colin Cross331a1212018-08-15 20:40:52 -07001310
1311 // merge dex jar with resources if necessary
1312 if j.resourceJar != nil {
1313 jars := android.Paths{dexOutputFile, j.resourceJar}
1314 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1315 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1316 false, nil, nil)
1317 dexOutputFile = combinedJar
1318 }
1319
1320 j.dexJarFile = dexOutputFile
1321
Colin Cross43f08db2018-11-12 10:13:39 -08001322 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1323
1324 j.maybeStrippedDexJarFile = dexOutputFile
1325
Colin Cross3063b782018-08-15 11:19:12 -07001326 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001327
1328 if ctx.Failed() {
1329 return
1330 }
Colin Cross331a1212018-08-15 20:40:52 -07001331 } else {
1332 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001333 }
Colin Cross331a1212018-08-15 20:40:52 -07001334
Colin Crossb7a63242015-04-16 14:09:14 -07001335 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001336
1337 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1338 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001339}
1340
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001341// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1342// since some of these flags may be used internally.
1343func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1344 for _, flag := range flags {
1345 flag = strings.TrimSpace(flag)
1346
1347 if !strings.HasPrefix(flag, "-") {
1348 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1349 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1350 ctx.PropertyErrorf("kotlincflags",
1351 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1352 } else if inList(flag, config.KotlincIllegalFlags) {
1353 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1354 } else if flag == "-include-runtime" {
1355 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1356 } else {
1357 args := strings.Split(flag, " ")
1358 if args[0] == "-kotlin-home" {
1359 ctx.PropertyErrorf("kotlincflags",
1360 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1361 }
1362 }
1363 }
1364}
1365
Colin Cross8eadbf02017-10-24 17:46:00 -07001366func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001367 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001368
1369 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001370 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001371 // Compile java sources into turbine.jar.
1372 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1373 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1374 if ctx.Failed() {
1375 return nil
1376 }
1377 jars = append(jars, turbineJar)
1378 }
1379
Colin Cross55f63ea2018-08-27 12:37:09 -07001380 jars = append(jars, extraJars...)
1381
Nan Zhanged19fc32017-10-19 13:06:22 -07001382 // Combine any static header libraries into classes-header.jar. If there is only
1383 // one input jar this step will be skipped.
1384 var headerJar android.Path
1385 jars = append(jars, deps.staticHeaderJars...)
1386
Colin Cross5c6ecc12017-10-23 18:12:27 -07001387 // we cannot skip the combine step for now if there is only one jar
1388 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1389 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001390 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1391 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001392 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001393
1394 if j.properties.Jarjar_rules != nil {
1395 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1396 // Transform classes.jar into classes-jarjar.jar
1397 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1398 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1399 headerJar = jarjarFile
1400 if ctx.Failed() {
1401 return nil
1402 }
1403 }
1404
1405 return headerJar
1406}
1407
Colin Crosscb933592017-11-22 13:49:43 -08001408func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001409 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001410
Colin Cross7a3139e2017-12-19 13:57:50 -08001411 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001412
Colin Cross84c38822018-01-03 15:59:46 -08001413 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001414 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1415
1416 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1417
1418 j.jacocoReportClassesFile = jacocoReportClassesFile
1419
1420 return instrumentedJar
1421}
1422
Colin Crossf506d872017-07-19 15:53:04 -07001423var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001424
Nan Zhanged19fc32017-10-19 13:06:22 -07001425func (j *Module) HeaderJars() android.Paths {
1426 return android.Paths{j.headerJarFile}
1427}
1428
1429func (j *Module) ImplementationJars() android.Paths {
1430 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001431}
1432
Colin Cross331a1212018-08-15 20:40:52 -07001433func (j *Module) ResourceJars() android.Paths {
1434 if j.resourceJar == nil {
1435 return nil
1436 }
1437 return android.Paths{j.resourceJar}
1438}
1439
1440func (j *Module) ImplementationAndResourcesJars() android.Paths {
1441 return android.Paths{j.implementationAndResourcesJar}
1442}
1443
Colin Cross46c9b8b2017-06-22 16:51:17 -07001444func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001445 return j.exportAidlIncludeDirs
1446}
1447
Jiyong Park1be96912018-05-28 18:02:19 +09001448func (j *Module) ExportedSdkLibs() []string {
1449 return j.exportedSdkLibs
1450}
1451
Colin Cross46c9b8b2017-06-22 16:51:17 -07001452var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001453
Colin Cross46c9b8b2017-06-22 16:51:17 -07001454func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001455 return j.logtagsSrcs
1456}
1457
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001458// Collect information for opening IDE project files in java/jdeps.go.
1459func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1460 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1461 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1462 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1463 if j.properties.Jarjar_rules != nil {
1464 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1465 }
1466}
1467
1468func (j *Module) CompilerDeps() []string {
1469 jdeps := []string{}
1470 jdeps = append(jdeps, j.properties.Libs...)
1471 jdeps = append(jdeps, j.properties.Static_libs...)
1472 return jdeps
1473}
1474
Colin Cross2fe66872015-03-30 17:20:39 -07001475//
1476// Java libraries (.jar file)
1477//
1478
Colin Crossf506d872017-07-19 15:53:04 -07001479type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001480 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001481}
1482
Colin Crossf506d872017-07-19 15:53:04 -07001483func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001484 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1485 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross46c9b8b2017-06-22 16:51:17 -07001486 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001487
Colin Cross9ae1b922018-06-26 17:59:05 -07001488 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross43f08db2018-11-12 10:13:39 -08001489 if j.deviceProperties.UncompressDex {
1490 alignedOutputFile := android.PathForModuleOut(ctx, "aligned", ctx.ModuleName()+".jar")
1491 TransformZipAlign(ctx, alignedOutputFile, j.outputFile)
1492 j.outputFile = alignedOutputFile
1493 }
1494
Colin Cross2c429dc2017-08-31 16:45:16 -07001495 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1496 ctx.ModuleName()+".jar", j.outputFile)
1497 }
Colin Crossb7a63242015-04-16 14:09:14 -07001498}
1499
Colin Crossf506d872017-07-19 15:53:04 -07001500func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001501 j.deps(ctx)
1502}
1503
Colin Cross9ae1b922018-06-26 17:59:05 -07001504func LibraryFactory() android.Module {
1505 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001506
Colin Cross9ae1b922018-06-26 17:59:05 -07001507 module.AddProperties(
1508 &module.Module.properties,
1509 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001510 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001511 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001512
Colin Cross9ae1b922018-06-26 17:59:05 -07001513 InitJavaModule(module, android.HostAndDeviceSupported)
1514 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001515}
1516
Colin Crossf506d872017-07-19 15:53:04 -07001517func LibraryHostFactory() android.Module {
1518 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001519
Colin Cross6af17aa2017-09-20 12:59:05 -07001520 module.AddProperties(
1521 &module.Module.properties,
1522 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001523
Colin Cross9ae1b922018-06-26 17:59:05 -07001524 module.Module.properties.Installable = proptools.BoolPtr(true)
1525
Colin Cross89536d42017-07-07 14:35:50 -07001526 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001527 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001528}
1529
1530//
Colin Crossb628ea52018-08-14 16:42:33 -07001531// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001532//
1533
1534type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001535 // list of compatibility suites (for example "cts", "vts") that the module should be
1536 // installed into.
1537 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001538
1539 // the name of the test configuration (for example "AndroidTest.xml") that should be
1540 // installed with the module.
1541 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001542
Jack He33338892018-09-19 02:21:28 -07001543 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1544 // should be installed with the module.
1545 Test_config_template *string `android:"arch_variant"`
1546
Colin Crossd96ca352018-08-10 16:06:24 -07001547 // list of files or filegroup modules that provide data that should be installed alongside
1548 // the test
1549 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001550}
1551
1552type Test struct {
1553 Library
1554
1555 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001556
1557 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001558 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001559}
1560
1561func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001562 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001563 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001564
1565 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001566}
1567
1568func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1569 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001570 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001571 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001572 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001573}
1574
1575func TestFactory() android.Module {
1576 module := &Test{}
1577
1578 module.AddProperties(
1579 &module.Module.properties,
1580 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001581 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001582 &module.Module.protoProperties,
1583 &module.testProperties)
1584
Colin Cross9ae1b922018-06-26 17:59:05 -07001585 module.Module.properties.Installable = proptools.BoolPtr(true)
1586
Colin Cross05638fc2018-04-09 18:40:24 -07001587 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001588 return module
1589}
1590
1591func TestHostFactory() android.Module {
1592 module := &Test{}
1593
1594 module.AddProperties(
1595 &module.Module.properties,
1596 &module.Module.protoProperties,
1597 &module.testProperties)
1598
Colin Cross9ae1b922018-06-26 17:59:05 -07001599 module.Module.properties.Installable = proptools.BoolPtr(true)
1600
Colin Cross05638fc2018-04-09 18:40:24 -07001601 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001602 return module
1603}
1604
1605//
Colin Cross2fe66872015-03-30 17:20:39 -07001606// Java Binaries (.jar file plus wrapper script)
1607//
1608
Colin Crossf506d872017-07-19 15:53:04 -07001609type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001610 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001611 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001612
1613 // Name of the class containing main to be inserted into the manifest as Main-Class.
1614 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001615}
1616
Colin Crossf506d872017-07-19 15:53:04 -07001617type Binary struct {
1618 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001619
Colin Crossf506d872017-07-19 15:53:04 -07001620 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001621
Colin Cross6b4a32d2017-12-05 13:42:45 -08001622 isWrapperVariant bool
1623
Colin Crossc3315992017-12-08 19:12:36 -08001624 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001625 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001626}
1627
Alex Light24237172017-10-26 09:46:21 -07001628func (j *Binary) HostToolPath() android.OptionalPath {
1629 return android.OptionalPathForPath(j.binaryFile)
1630}
1631
Colin Crossf506d872017-07-19 15:53:04 -07001632func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001633 if ctx.Arch().ArchType == android.Common {
1634 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001635 if j.binaryProperties.Main_class != nil {
1636 if j.properties.Manifest != nil {
1637 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1638 }
1639 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1640 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1641 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1642 }
1643
Colin Cross6b4a32d2017-12-05 13:42:45 -08001644 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001645 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001646 // Handle the binary wrapper
1647 j.isWrapperVariant = true
1648
Colin Cross366938f2017-12-11 16:29:02 -08001649 if j.binaryProperties.Wrapper != nil {
1650 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001651 } else {
1652 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1653 }
1654
1655 // Depend on the installed jar so that the wrapper doesn't get executed by
1656 // another build rule before the jar has been installed.
1657 jarFile := ctx.PrimaryModule().(*Binary).installFile
1658
1659 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1660 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001661 }
Colin Cross2fe66872015-03-30 17:20:39 -07001662}
1663
Colin Crossf506d872017-07-19 15:53:04 -07001664func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001665 if ctx.Arch().ArchType == android.Common {
1666 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001667 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001668 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001669 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001670}
1671
Colin Crossf506d872017-07-19 15:53:04 -07001672func BinaryFactory() android.Module {
1673 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001674
Colin Cross36242852017-06-23 15:06:31 -07001675 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001676 &module.Module.properties,
1677 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001678 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001679 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001680 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001681
Colin Cross9ae1b922018-06-26 17:59:05 -07001682 module.Module.properties.Installable = proptools.BoolPtr(true)
1683
Colin Cross6b4a32d2017-12-05 13:42:45 -08001684 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1685 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001686 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001687}
1688
Colin Crossf506d872017-07-19 15:53:04 -07001689func BinaryHostFactory() android.Module {
1690 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001691
Colin Cross36242852017-06-23 15:06:31 -07001692 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001693 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001694 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001695 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001696
Colin Cross9ae1b922018-06-26 17:59:05 -07001697 module.Module.properties.Installable = proptools.BoolPtr(true)
1698
Colin Cross6b4a32d2017-12-05 13:42:45 -08001699 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1700 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001701 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001702}
1703
1704//
1705// Java prebuilts
1706//
1707
Colin Cross74d73e22017-08-02 11:05:49 -07001708type ImportProperties struct {
1709 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001710
Nan Zhangea568a42017-11-08 21:20:04 -08001711 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001712
1713 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001714
1715 // List of shared java libs that this module has dependencies to
1716 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001717
1718 // List of files to remove from the jar file(s)
1719 Exclude_files []string
1720
1721 // List of directories to remove from the jar file(s)
1722 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001723
1724 // if set to true, run Jetifier against .jar file. Defaults to false.
1725 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001726}
1727
1728type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001729 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001730 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001731 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001732
Colin Cross74d73e22017-08-02 11:05:49 -07001733 properties ImportProperties
1734
Colin Cross0a6e0072017-08-30 14:24:55 -07001735 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001736 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001737}
1738
Colin Cross83bb3162018-06-25 15:48:06 -07001739func (j *Import) sdkVersion() string {
1740 return String(j.properties.Sdk_version)
1741}
1742
1743func (j *Import) minSdkVersion() string {
1744 return j.sdkVersion()
1745}
1746
Colin Cross74d73e22017-08-02 11:05:49 -07001747func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001748 return &j.prebuilt
1749}
1750
Colin Cross74d73e22017-08-02 11:05:49 -07001751func (j *Import) PrebuiltSrcs() []string {
1752 return j.properties.Jars
1753}
1754
1755func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001756 return j.prebuilt.Name(j.ModuleBase.Name())
1757}
1758
Colin Cross74d73e22017-08-02 11:05:49 -07001759func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001760 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001761 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001762}
1763
Colin Cross74d73e22017-08-02 11:05:49 -07001764func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001765 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001766
Nan Zhang4c819fb2018-08-27 18:31:46 -07001767 jarName := ctx.ModuleName() + ".jar"
1768 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001769 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1770 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001771 if Bool(j.properties.Jetifier_enabled) {
1772 inputFile := outputFile
1773 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1774 TransformJetifier(ctx, outputFile, inputFile)
1775 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001776 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001777
1778 ctx.VisitDirectDeps(func(module android.Module) {
1779 otherName := ctx.OtherModuleName(module)
1780 tag := ctx.OtherModuleDependencyTag(module)
1781
1782 switch dep := module.(type) {
1783 case Dependency:
1784 switch tag {
1785 case libTag, staticLibTag:
1786 // sdk lib names from dependencies are re-exported
1787 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1788 }
1789 case SdkLibraryDependency:
1790 switch tag {
1791 case libTag:
1792 // names of sdk libs that are directly depended are exported
1793 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1794 }
1795 }
1796 })
1797
1798 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001799 if Bool(j.properties.Installable) {
1800 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1801 ctx.ModuleName()+".jar", outputFile)
1802 }
Colin Cross2fe66872015-03-30 17:20:39 -07001803}
1804
Colin Cross74d73e22017-08-02 11:05:49 -07001805var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001806
Nan Zhanged19fc32017-10-19 13:06:22 -07001807func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001808 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001809}
1810
1811func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001812 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001813}
1814
Colin Cross331a1212018-08-15 20:40:52 -07001815func (j *Import) ResourceJars() android.Paths {
1816 return nil
1817}
1818
1819func (j *Import) ImplementationAndResourcesJars() android.Paths {
1820 return android.Paths{j.combinedClasspathFile}
1821}
1822
Colin Cross74d73e22017-08-02 11:05:49 -07001823func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001824 return nil
1825}
1826
Jiyong Park1be96912018-05-28 18:02:19 +09001827func (j *Import) ExportedSdkLibs() []string {
1828 return j.exportedSdkLibs
1829}
1830
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001831// Collect information for opening IDE project files in java/jdeps.go.
1832const (
1833 removedPrefix = "prebuilt_"
1834)
1835
1836func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1837 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1838}
1839
1840func (j *Import) IDECustomizedModuleName() string {
1841 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1842 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1843 // solution to get the Import name.
1844 name := j.Name()
1845 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001846 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001847 }
1848 return name
1849}
1850
Colin Cross74d73e22017-08-02 11:05:49 -07001851var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001852
Colin Cross74d73e22017-08-02 11:05:49 -07001853func ImportFactory() android.Module {
1854 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001855
Colin Cross74d73e22017-08-02 11:05:49 -07001856 module.AddProperties(&module.properties)
1857
1858 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001859 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001860 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001861}
1862
Colin Cross74d73e22017-08-02 11:05:49 -07001863func ImportFactoryHost() android.Module {
1864 module := &Import{}
1865
1866 module.AddProperties(&module.properties)
1867
1868 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001869 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001870 return module
1871}
1872
Colin Cross89536d42017-07-07 14:35:50 -07001873//
1874// Defaults
1875//
1876type Defaults struct {
1877 android.ModuleBase
1878 android.DefaultsModuleBase
1879}
1880
1881func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1882}
1883
1884func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1885}
1886
1887func defaultsFactory() android.Module {
1888 return DefaultsFactory()
1889}
1890
1891func DefaultsFactory(props ...interface{}) android.Module {
1892 module := &Defaults{}
1893
1894 module.AddProperties(props...)
1895 module.AddProperties(
1896 &CompilerProperties{},
1897 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001898 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001899 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001900 &aaptProperties{},
1901 &androidLibraryProperties{},
1902 &appProperties{},
1903 &appTestProperties{},
1904 &ImportProperties{},
1905 &AARImportProperties{},
1906 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001907 )
1908
1909 android.InitDefaultsModule(module)
1910
1911 return module
1912}
Nan Zhangea568a42017-11-08 21:20:04 -08001913
1914var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001915var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001916var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001917var inList = android.InList