blob: fa4aee43d374b497006ca218cf515142ff56a449 [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 Crossbe1da472017-07-07 15:59:46 -0700433func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700434 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700435 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700436 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700437 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700438 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100439 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700440 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700441 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700442 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700443 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100444 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700445 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800446 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700447 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
448 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800449 }
Colin Crossbe1da472017-07-07 15:59:46 -0700450 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700451 } else if j.deviceProperties.System_modules == nil {
452 ctx.PropertyErrorf("no_standard_libs",
453 "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 +0100454 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700455 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700456 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100457 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700458 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800459 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800460 if ctx.ModuleName() == "android_stubs_current" ||
461 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700462 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700463 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800464 }
Colin Cross2fe66872015-03-30 17:20:39 -0700465 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700466
Colin Cross42d48b72018-08-29 14:10:52 -0700467 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
468 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100469 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700470 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100471 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700472
Colin Cross7f9036c2017-08-30 13:27:57 -0700473 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000474 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700475 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800476 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700477
478 if j.hasSrcExt(".proto") {
479 protoDeps(ctx, &j.protoProperties)
480 }
Colin Cross93e85952017-08-15 13:34:18 -0700481
482 if j.hasSrcExt(".kt") {
483 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
484 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700485 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700486 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800487
488 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700489 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800490 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700491}
492
493func hasSrcExt(srcs []string, ext string) bool {
494 for _, src := range srcs {
495 if filepath.Ext(src) == ext {
496 return true
497 }
498 }
499
500 return false
501}
502
Nan Zhang61eaedb2017-11-02 13:28:15 -0700503func shardPaths(paths android.Paths, shardSize int) []android.Paths {
504 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
505 for len(paths) > shardSize {
506 ret = append(ret, paths[0:shardSize])
507 paths = paths[shardSize:]
508 }
509 if len(paths) > 0 {
510 ret = append(ret, paths)
511 }
512 return ret
513}
514
Colin Cross6af17aa2017-09-20 12:59:05 -0700515func (j *Module) hasSrcExt(ext string) bool {
516 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700517}
518
Colin Cross46c9b8b2017-06-22 16:51:17 -0700519func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700520 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700521
Colin Crossebe1a512017-11-14 13:12:14 -0800522 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
523 aidlIncludes = append(aidlIncludes,
524 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
525 aidlIncludes = append(aidlIncludes,
526 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700527
Steven Moreland667f6882018-07-26 12:55:08 -0700528 flags := []string{"-b"}
529
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700530 if aidlPreprocess.Valid() {
531 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700532 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700533 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700534 }
535
Colin Cross635c3b02016-05-18 15:37:25 -0700536 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800537 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700538 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800539 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700540 flags = append(flags, "-I"+src.String())
541 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700542
Martijn Coeneneab15642018-03-09 09:29:59 +0100543 if Bool(j.deviceProperties.Aidl.Generate_traces) {
544 flags = append(flags, "-t")
545 }
546
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100547 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
548 flags = append(flags, "--transaction_names")
549 }
550
Colin Crossf03c82b2015-04-13 13:53:40 -0700551 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700552}
553
Colin Cross32f676a2017-09-06 13:41:06 -0700554type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800555 classpath classpath
556 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700557 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700558 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700559 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700560 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700561 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800562 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700563 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700564 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700565 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700566 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700567}
Colin Cross2fe66872015-03-30 17:20:39 -0700568
Colin Cross54250902017-12-05 09:28:08 -0800569func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
570 for _, f := range dep.Srcs() {
571 if f.Ext() != ".jar" {
572 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
573 ctx.OtherModuleName(dep.(blueprint.Module)))
574 }
575 }
576}
577
Jiyong Park2d492942018-03-05 17:44:10 +0900578type linkType int
579
580const (
581 javaCore linkType = iota
582 javaSdk
583 javaSystem
584 javaPlatform
585)
586
Jiyong Park46f78fb2018-10-20 16:33:17 +0900587func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700588 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700589 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900590 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
591 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
592 name == "core-lambda-stubs":
593 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100594 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900595 return javaCore, false
596 case name == "android_system_stubs_current":
597 return javaSystem, true
598 case strings.HasPrefix(ver, "system_"):
599 return javaSystem, false
600 case name == "android_test_stubs_current":
601 return javaSystem, true
602 case strings.HasPrefix(ver, "test_"):
603 return javaPlatform, false
604 case name == "android_stubs_current":
605 return javaSdk, true
606 case ver == "current":
607 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700608 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900609 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700610 default:
611 if _, err := strconv.Atoi(ver); err != nil {
612 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
613 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900614 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900615 }
616}
617
Jiyong Park750e5572018-01-31 00:20:13 +0900618func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700619 if ctx.Host() {
620 return
621 }
622
Jiyong Park46f78fb2018-10-20 16:33:17 +0900623 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
624 if stubs {
625 return
626 }
627 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900628 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."
629
630 switch myLinkType {
631 case javaCore:
632 if otherLinkType != javaCore {
633 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 +0900634 ctx.OtherModuleName(to))
635 }
Jiyong Park2d492942018-03-05 17:44:10 +0900636 break
637 case javaSdk:
638 if otherLinkType != javaCore && otherLinkType != javaSdk {
639 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
640 ctx.OtherModuleName(to))
641 }
642 break
643 case javaSystem:
644 if otherLinkType == javaPlatform {
645 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
646 ctx.OtherModuleName(to))
647 }
648 break
649 case javaPlatform:
650 // no restriction on link-type
651 break
Jiyong Park750e5572018-01-31 00:20:13 +0900652 }
653}
654
Colin Cross32f676a2017-09-06 13:41:06 -0700655func (j *Module) collectDeps(ctx android.ModuleContext) deps {
656 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700657
Colin Cross300f0382018-03-06 13:11:51 -0800658 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700659 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800660 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700661 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800662 } else if sdkDep.useFiles {
663 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700664 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800665 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
666 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700667 }
668
Colin Crossd11fcda2017-10-23 17:59:01 -0700669 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700670 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700671 tag := ctx.OtherModuleDependencyTag(module)
672
Colin Crossa4f08812018-10-02 22:03:40 -0700673 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700674 // Handled by AndroidApp.collectAppDeps
675 return
676 }
677 if tag == certificateTag {
678 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700679 return
680 }
681
Jiyong Park750e5572018-01-31 00:20:13 +0900682 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700683 switch tag {
684 case bootClasspathTag, libTag, staticLibTag:
685 checkLinkType(ctx, j, to, tag.(dependencyTag))
686 }
Jiyong Park750e5572018-01-31 00:20:13 +0900687 }
Colin Cross54250902017-12-05 09:28:08 -0800688 switch dep := module.(type) {
689 case Dependency:
690 switch tag {
691 case bootClasspathTag:
692 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700693 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800694 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900695 // sdk lib names from dependencies are re-exported
696 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800697 case staticLibTag:
698 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
699 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
700 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700701 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900702 // sdk lib names from dependencies are re-exported
703 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700704 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700705 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800706 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100707 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800708 // framework.jar has a one-off dependency on the R.java and Manifest.java files
709 // generated by framework-res.apk
710 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
711 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800712 case frameworkApkTag:
713 if ctx.ModuleName() == "android_stubs_current" ||
714 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700715 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800716 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
717 // resource files out of there for aapt.
718 //
719 // Normally the package rule runs aapt, which includes the resource,
720 // but we're not running that in our package rule so just copy in the
721 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700722 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800723 }
Colin Cross54250902017-12-05 09:28:08 -0800724 case kotlinStdlibTag:
725 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800726 }
727
728 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900729 case SdkLibraryDependency:
730 switch tag {
731 case libTag:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900732 linkType, _ := getLinkType(j, ctx.ModuleName())
733 deps.classpath = append(deps.classpath, dep.HeaderJars(linkType)...)
Jiyong Park1be96912018-05-28 18:02:19 +0900734 // names of sdk libs that are directly depended are exported
735 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900736 default:
737 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
738 }
Colin Cross54250902017-12-05 09:28:08 -0800739 case android.SourceFileProducer:
740 switch tag {
741 case libTag:
742 checkProducesJars(ctx, dep)
743 deps.classpath = append(deps.classpath, dep.Srcs()...)
744 case staticLibTag:
745 checkProducesJars(ctx, dep)
746 deps.classpath = append(deps.classpath, dep.Srcs()...)
747 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
748 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
749 case android.DefaultsDepTag, android.SourceDepTag:
750 // Nothing to do
751 default:
752 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
753 }
754 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700755 switch tag {
756 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700757 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700758 case systemModulesTag:
759 if deps.systemModules != nil {
760 panic("Found two system module dependencies")
761 }
762 sm := module.(*SystemModules)
763 if sm.outputFile == nil {
764 panic("Missing directory for system module dependency")
765 }
766 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700767 default:
768 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700769 }
Colin Crossec7a0422017-07-07 14:47:12 -0700770 }
Colin Cross2fe66872015-03-30 17:20:39 -0700771 })
772
Jiyong Park1be96912018-05-28 18:02:19 +0900773 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
774
Colin Cross32f676a2017-09-06 13:41:06 -0700775 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700776}
777
Colin Cross83bb3162018-06-25 15:48:06 -0700778func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700779 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800780 v := sdkContext.sdkVersion()
781 // For PDK builds, use the latest SDK version instead of "current"
782 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
783 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
784 latestSdkVersion := 0
785 if len(sdkVersions) > 0 {
786 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
787 }
788 v = strconv.Itoa(latestSdkVersion)
789 }
790
791 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700792 if err != nil {
793 ctx.PropertyErrorf("sdk_version", "%s", err)
794 }
Nan Zhang357466b2018-04-17 17:38:36 -0700795 if javaVersion != "" {
796 ret = javaVersion
797 } else if ctx.Device() && sdk <= 23 {
798 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900799 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700800 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700801 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700802 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
803 ret = "1.8"
804 } else {
805 ret = "1.9"
806 }
807
808 return ret
809}
810
Nan Zhanged19fc32017-10-19 13:06:22 -0700811func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700812
Colin Crossf03c82b2015-04-13 13:53:40 -0700813 var flags javaBuilderFlags
814
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100815 // javaVersion flag.
816 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
817
Nan Zhanged19fc32017-10-19 13:06:22 -0700818 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700819 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100820 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700821 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700822 }
Colin Cross6510f912017-11-29 00:27:14 -0800823 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700824 // Override the -g flag passed globally to remove local variable debug info to reduce
825 // disk and memory usage.
826 javacFlags = append(javacFlags, "-g:source,lines")
827 }
Colin Cross64162712017-08-08 13:17:59 -0700828
Colin Cross66548102018-06-19 22:47:35 -0700829 if ctx.Config().RunErrorProne() {
830 if config.ErrorProneClasspath == nil {
831 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
832 }
833
834 errorProneFlags := []string{
835 "-Xplugin:ErrorProne",
836 "${config.ErrorProneChecks}",
837 }
838 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
839
840 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
841 "'" + strings.Join(errorProneFlags, " ") + "'"
842 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800843 }
844
Nan Zhanged19fc32017-10-19 13:06:22 -0700845 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800846 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
847 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700848 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800849
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100850 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800851 !Bool(j.properties.No_standard_libs) &&
852 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
853 // Give host-side tools a version of OpenJDK's standard libraries
854 // close to what they're targeting. As of Dec 2017, AOSP is only
855 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
856 //
857 // When building with OpenJDK 8, the following should have no
858 // effect since those jars would be available by default.
859 //
860 // When building with OpenJDK 9 but targeting a version < 1.8,
861 // putting them on the bootclasspath means that:
862 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
863 // b) references to existing APIs are not reinterpreted in an
864 // OpenJDK 9-specific way, eg. calls to subclasses of
865 // java.nio.Buffer as in http://b/70862583
866 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
867 flags.bootClasspath = append(flags.bootClasspath,
868 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
869 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800870 if Bool(j.properties.Use_tools_jar) {
871 flags.bootClasspath = append(flags.bootClasspath,
872 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
873 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800874 }
875
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100876 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800877 // Manually specify build directory in case it is not under the repo root.
878 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
879 // just adding a symlink under the root doesn't help.)
880 patchPaths := ".:" + ctx.Config().BuildDir()
881 classPath := flags.classpath.FormJavaClassPath("")
882 if classPath != "" {
883 patchPaths += ":" + classPath
884 }
885 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700886 }
887
Nan Zhanged19fc32017-10-19 13:06:22 -0700888 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700889 if deps.systemModules != nil {
890 flags.systemModules = append(flags.systemModules, deps.systemModules)
891 }
892
Nan Zhanged19fc32017-10-19 13:06:22 -0700893 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700894 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700895 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700896 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700897 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
898 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700899 }
900
Colin Cross81440082018-08-15 20:21:55 -0700901 if len(javacFlags) > 0 {
902 // optimization.
903 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
904 flags.javacFlags = "$javacFlags"
905 }
906
Nan Zhanged19fc32017-10-19 13:06:22 -0700907 return flags
908}
Colin Crossc0b06f12015-04-08 13:03:43 -0700909
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800910func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700911
Colin Crossebe1a512017-11-14 13:12:14 -0800912 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700913
914 deps := j.collectDeps(ctx)
915 flags := j.collectBuilderFlags(ctx, deps)
916
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100917 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
919 }
920 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700921 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800922 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700923 }
924
Colin Crossaf050172017-11-15 23:01:59 -0800925 srcFiles = j.genSources(ctx, srcFiles, flags)
926
927 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700928 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800929 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700930
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700931 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
932 // that IDEInfo struct will use
933 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
934
Colin Cross1ee23172017-10-18 14:44:18 -0700935 jarName := ctx.ModuleName() + ".jar"
936
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000937 javaSrcFiles := srcFiles.FilterByExt(".java")
938 var uniqueSrcFiles android.Paths
939 set := make(map[string]bool)
940 for _, v := range javaSrcFiles {
941 if _, found := set[v.String()]; !found {
942 set[v.String()] = true
943 uniqueSrcFiles = append(uniqueSrcFiles, v)
944 }
945 }
946
Colin Cross55f63ea2018-08-27 12:37:09 -0700947 var kotlinJars android.Paths
948
Colin Cross93e85952017-08-15 13:34:18 -0700949 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200950 // user defined kotlin flags.
951 kotlincFlags := j.properties.Kotlincflags
952 CheckKotlincFlags(ctx, kotlincFlags)
953
Colin Cross93e85952017-08-15 13:34:18 -0700954 // If there are kotlin files, compile them first but pass all the kotlin and java files
955 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
956 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200957 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700958 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200959 kotlincFlags = append(kotlincFlags, "-no-jdk")
960 }
961 if len(kotlincFlags) > 0 {
962 // optimization.
963 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
964 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700965 }
966
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000967 var kotlinSrcFiles android.Paths
968 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
969 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
970
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000971 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700972 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
973 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
974
Colin Cross1ee23172017-10-18 14:44:18 -0700975 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000976 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700977 if ctx.Failed() {
978 return
979 }
980
981 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -0700982 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -0700983 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000984
Colin Cross93e85952017-08-15 13:34:18 -0700985 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -0700986 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -0700987 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -0700988 }
989
Colin Cross55f63ea2018-08-27 12:37:09 -0700990 jars := append(android.Paths(nil), kotlinJars...)
991
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800992 // Store the list of .java files that was passed to javac
993 j.compiledJavaSrcs = uniqueSrcFiles
994 j.compiledSrcJars = srcJars
995
Nan Zhang61eaedb2017-11-02 13:28:15 -0700996 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800997 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700998 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
999 enable_sharding = true
1000 if len(j.properties.Annotation_processors) != 0 ||
1001 len(j.properties.Annotation_processor_classes) != 0 {
1002 ctx.PropertyErrorf("javac_shard_size",
1003 "%q cannot be set when annotation processors are enabled.",
1004 j.properties.Javac_shard_size)
1005 }
1006 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001007 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001008 if ctx.Failed() {
1009 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001010 }
1011 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001012 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001013 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001014 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001015 // If error-prone is enabled, add an additional rule to compile the java files into
1016 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001017 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001018 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1019 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001020 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001021 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001022 extraJarDeps = append(extraJarDeps, errorprone)
1023 }
1024
Nan Zhang61eaedb2017-11-02 13:28:15 -07001025 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001026 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001027 shardSize := int(*(j.properties.Javac_shard_size))
1028 var shardSrcs []android.Paths
1029 if len(uniqueSrcFiles) > 0 {
1030 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1031 for idx, shardSrc := range shardSrcs {
1032 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1033 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1034 jars = append(jars, classes)
1035 }
1036 }
1037 if len(srcJars) > 0 {
1038 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1039 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1040 jars = append(jars, classes)
1041 }
1042 } else {
1043 classes := android.PathForModuleOut(ctx, "javac", jarName)
1044 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1045 jars = append(jars, classes)
1046 }
Colin Crossd6891432017-09-27 17:39:56 -07001047 if ctx.Failed() {
1048 return
1049 }
Colin Cross2fe66872015-03-30 17:20:39 -07001050 }
1051
Colin Crosscedd4762018-09-13 11:26:19 -07001052 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1053 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001054 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1055
1056 var resArgs []string
1057 var resDeps android.Paths
1058
1059 resArgs = append(resArgs, dirArgs...)
1060 resDeps = append(resDeps, dirDeps...)
1061
1062 resArgs = append(resArgs, fileArgs...)
1063 resDeps = append(resDeps, fileDeps...)
1064
Colin Crossff3ae9d2018-04-10 16:15:18 -07001065 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001066 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001067 resArgs = append(resArgs, srcArgs...)
1068 resDeps = append(resDeps, srcDeps...)
1069 }
Colin Cross40a36712017-09-27 17:41:35 -07001070
1071 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001072 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001073 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001074 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001075 if ctx.Failed() {
1076 return
1077 }
1078 }
1079
Colin Cross331a1212018-08-15 20:40:52 -07001080 if len(deps.staticResourceJars) > 0 {
1081 var jars android.Paths
1082 if j.resourceJar != nil {
1083 jars = append(jars, j.resourceJar)
1084 }
1085 jars = append(jars, deps.staticResourceJars...)
1086
1087 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1088 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1089 false, nil, nil)
1090 j.resourceJar = combinedJar
1091 }
1092
Colin Cross32f676a2017-09-06 13:41:06 -07001093 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001094 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001095
Colin Cross094054a2018-10-17 15:10:48 -07001096 manifest := j.overrideManifest
1097 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001098 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1099 }
Colin Cross635acc92017-09-12 22:50:46 -07001100
Colin Cross0a6e0072017-08-30 14:24:55 -07001101 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001102 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001103 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001104
1105 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001106 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1107 // Optimization: skip the combine step if there is nothing to do
1108 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1109 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1110 // any if len(jars) == 1.
1111 outputFile = moduleOutPath
1112 } else {
1113 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1114 ctx.Build(pctx, android.BuildParams{
1115 Rule: android.Cp,
1116 Input: jars[0],
1117 Output: combinedJar,
1118 })
1119 outputFile = combinedJar
1120 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001121 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001122 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001123 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001124 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001125 outputFile = combinedJar
1126 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001127
Colin Cross331a1212018-08-15 20:40:52 -07001128 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001129 if j.properties.Jarjar_rules != nil {
1130 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001131 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001132 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001133 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1134 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001135
1136 // jarjar resource jar if necessary
1137 if j.resourceJar != nil {
1138 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1139 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1140 j.resourceJar = resourceJarJarFile
1141 }
1142
Colin Cross0a6e0072017-08-30 14:24:55 -07001143 if ctx.Failed() {
1144 return
1145 }
1146 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001147 j.implementationJarFile = outputFile
1148 if j.headerJarFile == nil {
1149 j.headerJarFile = j.implementationJarFile
1150 }
Colin Cross2fe66872015-03-30 17:20:39 -07001151
Colin Cross6510f912017-11-29 00:27:14 -08001152 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001153 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1154 j.properties.Instrument = true
1155 }
1156 }
1157
Colin Cross3144dfc2018-01-03 15:06:47 -08001158 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001159 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1160 }
1161
Colin Cross331a1212018-08-15 20:40:52 -07001162 // merge implementation jar with resources if necessary
1163 implementationAndResourcesJar := outputFile
1164 if j.resourceJar != nil {
1165 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1166 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1167 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1168 false, nil, nil)
1169 implementationAndResourcesJar = combinedJar
1170 }
1171
1172 j.implementationAndResourcesJar = implementationAndResourcesJar
1173
Colin Cross9ae1b922018-06-26 17:59:05 -07001174 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001175 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001176 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001177 if ctx.Failed() {
1178 return
1179 }
Colin Cross331a1212018-08-15 20:40:52 -07001180
1181 // merge dex jar with resources if necessary
1182 if j.resourceJar != nil {
1183 jars := android.Paths{dexOutputFile, j.resourceJar}
1184 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1185 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1186 false, nil, nil)
1187 dexOutputFile = combinedJar
1188 }
1189
1190 j.dexJarFile = dexOutputFile
1191
Colin Crossdc2da912019-01-05 22:13:05 -08001192 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross2fc72f62018-12-21 12:59:54 -08001193 j.dexpreopter.uncompressedDex = j.deviceProperties.UncompressDex
Colin Cross43f08db2018-11-12 10:13:39 -08001194 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1195
1196 j.maybeStrippedDexJarFile = dexOutputFile
1197
Colin Cross3063b782018-08-15 11:19:12 -07001198 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001199
1200 if ctx.Failed() {
1201 return
1202 }
Colin Cross331a1212018-08-15 20:40:52 -07001203 } else {
1204 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001205 }
Colin Cross331a1212018-08-15 20:40:52 -07001206
Colin Crossb7a63242015-04-16 14:09:14 -07001207 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001208
1209 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1210 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001211}
1212
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001213// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1214// since some of these flags may be used internally.
1215func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1216 for _, flag := range flags {
1217 flag = strings.TrimSpace(flag)
1218
1219 if !strings.HasPrefix(flag, "-") {
1220 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1221 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1222 ctx.PropertyErrorf("kotlincflags",
1223 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1224 } else if inList(flag, config.KotlincIllegalFlags) {
1225 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1226 } else if flag == "-include-runtime" {
1227 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1228 } else {
1229 args := strings.Split(flag, " ")
1230 if args[0] == "-kotlin-home" {
1231 ctx.PropertyErrorf("kotlincflags",
1232 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1233 }
1234 }
1235 }
1236}
1237
Colin Cross8eadbf02017-10-24 17:46:00 -07001238func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001239 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001240
1241 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001242 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001243 // Compile java sources into turbine.jar.
1244 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1245 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1246 if ctx.Failed() {
1247 return nil
1248 }
1249 jars = append(jars, turbineJar)
1250 }
1251
Colin Cross55f63ea2018-08-27 12:37:09 -07001252 jars = append(jars, extraJars...)
1253
Nan Zhanged19fc32017-10-19 13:06:22 -07001254 // Combine any static header libraries into classes-header.jar. If there is only
1255 // one input jar this step will be skipped.
1256 var headerJar android.Path
1257 jars = append(jars, deps.staticHeaderJars...)
1258
Colin Cross5c6ecc12017-10-23 18:12:27 -07001259 // we cannot skip the combine step for now if there is only one jar
1260 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1261 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001262 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1263 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001264 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001265
1266 if j.properties.Jarjar_rules != nil {
1267 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1268 // Transform classes.jar into classes-jarjar.jar
1269 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1270 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1271 headerJar = jarjarFile
1272 if ctx.Failed() {
1273 return nil
1274 }
1275 }
1276
1277 return headerJar
1278}
1279
Colin Crosscb933592017-11-22 13:49:43 -08001280func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001281 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001282
Colin Cross7a3139e2017-12-19 13:57:50 -08001283 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001284
Colin Cross84c38822018-01-03 15:59:46 -08001285 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001286 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1287
1288 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1289
1290 j.jacocoReportClassesFile = jacocoReportClassesFile
1291
1292 return instrumentedJar
1293}
1294
albaltai36ff7dc2018-12-25 14:35:23 +08001295var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001296
Nan Zhanged19fc32017-10-19 13:06:22 -07001297func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001298 if j.headerJarFile == nil {
1299 return nil
1300 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001301 return android.Paths{j.headerJarFile}
1302}
1303
1304func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001305 if j.implementationJarFile == nil {
1306 return nil
1307 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001308 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001309}
1310
Colin Cross331a1212018-08-15 20:40:52 -07001311func (j *Module) ResourceJars() android.Paths {
1312 if j.resourceJar == nil {
1313 return nil
1314 }
1315 return android.Paths{j.resourceJar}
1316}
1317
1318func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001319 if j.implementationAndResourcesJar == nil {
1320 return nil
1321 }
Colin Cross331a1212018-08-15 20:40:52 -07001322 return android.Paths{j.implementationAndResourcesJar}
1323}
1324
Colin Cross46c9b8b2017-06-22 16:51:17 -07001325func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001326 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001327 return j.exportAidlIncludeDirs
1328}
1329
Jiyong Park1be96912018-05-28 18:02:19 +09001330func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001331 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001332 return j.exportedSdkLibs
1333}
1334
Colin Cross46c9b8b2017-06-22 16:51:17 -07001335var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001336
Colin Cross46c9b8b2017-06-22 16:51:17 -07001337func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001338 return j.logtagsSrcs
1339}
1340
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001341// Collect information for opening IDE project files in java/jdeps.go.
1342func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1343 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1344 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1345 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1346 if j.properties.Jarjar_rules != nil {
1347 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1348 }
1349}
1350
1351func (j *Module) CompilerDeps() []string {
1352 jdeps := []string{}
1353 jdeps = append(jdeps, j.properties.Libs...)
1354 jdeps = append(jdeps, j.properties.Static_libs...)
1355 return jdeps
1356}
1357
Colin Cross2fe66872015-03-30 17:20:39 -07001358//
1359// Java libraries (.jar file)
1360//
1361
Colin Crossf506d872017-07-19 15:53:04 -07001362type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001363 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001364}
1365
Colin Cross2fc72f62018-12-21 12:59:54 -08001366func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
1367 return false
1368}
1369
Colin Crossf506d872017-07-19 15:53:04 -07001370func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001371 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1372 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross2fc72f62018-12-21 12:59:54 -08001373 j.deviceProperties.UncompressDex = j.shouldUncompressDex(ctx)
Colin Cross46c9b8b2017-06-22 16:51:17 -07001374 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001375
Colin Cross9ae1b922018-06-26 17:59:05 -07001376 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross43f08db2018-11-12 10:13:39 -08001377 if j.deviceProperties.UncompressDex {
1378 alignedOutputFile := android.PathForModuleOut(ctx, "aligned", ctx.ModuleName()+".jar")
1379 TransformZipAlign(ctx, alignedOutputFile, j.outputFile)
1380 j.outputFile = alignedOutputFile
1381 }
1382
Colin Cross2c429dc2017-08-31 16:45:16 -07001383 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1384 ctx.ModuleName()+".jar", j.outputFile)
1385 }
Colin Crossb7a63242015-04-16 14:09:14 -07001386}
1387
Colin Crossf506d872017-07-19 15:53:04 -07001388func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001389 j.deps(ctx)
1390}
1391
Colin Cross9ae1b922018-06-26 17:59:05 -07001392func LibraryFactory() android.Module {
1393 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001394
Colin Cross9ae1b922018-06-26 17:59:05 -07001395 module.AddProperties(
1396 &module.Module.properties,
1397 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001398 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001399 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001400
Colin Cross9ae1b922018-06-26 17:59:05 -07001401 InitJavaModule(module, android.HostAndDeviceSupported)
1402 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001403}
1404
Colin Crossf506d872017-07-19 15:53:04 -07001405func LibraryHostFactory() android.Module {
1406 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001407
Colin Cross6af17aa2017-09-20 12:59:05 -07001408 module.AddProperties(
1409 &module.Module.properties,
1410 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001411
Colin Cross9ae1b922018-06-26 17:59:05 -07001412 module.Module.properties.Installable = proptools.BoolPtr(true)
1413
Colin Cross89536d42017-07-07 14:35:50 -07001414 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001415 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001416}
1417
1418//
Colin Crossb628ea52018-08-14 16:42:33 -07001419// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001420//
1421
1422type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001423 // list of compatibility suites (for example "cts", "vts") that the module should be
1424 // installed into.
1425 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001426
1427 // the name of the test configuration (for example "AndroidTest.xml") that should be
1428 // installed with the module.
1429 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001430
Jack He33338892018-09-19 02:21:28 -07001431 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1432 // should be installed with the module.
1433 Test_config_template *string `android:"arch_variant"`
1434
Colin Crossd96ca352018-08-10 16:06:24 -07001435 // list of files or filegroup modules that provide data that should be installed alongside
1436 // the test
1437 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001438}
1439
1440type Test struct {
1441 Library
1442
1443 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001444
1445 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001446 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001447}
1448
1449func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001450 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001451 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001452
1453 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001454}
1455
1456func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1457 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001458 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001459 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001460 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001461}
1462
1463func TestFactory() android.Module {
1464 module := &Test{}
1465
1466 module.AddProperties(
1467 &module.Module.properties,
1468 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001469 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001470 &module.Module.protoProperties,
1471 &module.testProperties)
1472
Colin Cross9ae1b922018-06-26 17:59:05 -07001473 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001474 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001475
Colin Cross05638fc2018-04-09 18:40:24 -07001476 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001477 return module
1478}
1479
1480func TestHostFactory() android.Module {
1481 module := &Test{}
1482
1483 module.AddProperties(
1484 &module.Module.properties,
1485 &module.Module.protoProperties,
1486 &module.testProperties)
1487
Colin Cross9ae1b922018-06-26 17:59:05 -07001488 module.Module.properties.Installable = proptools.BoolPtr(true)
1489
Colin Cross05638fc2018-04-09 18:40:24 -07001490 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001491 return module
1492}
1493
1494//
Colin Cross2fe66872015-03-30 17:20:39 -07001495// Java Binaries (.jar file plus wrapper script)
1496//
1497
Colin Crossf506d872017-07-19 15:53:04 -07001498type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001499 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001500 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001501
1502 // Name of the class containing main to be inserted into the manifest as Main-Class.
1503 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001504}
1505
Colin Crossf506d872017-07-19 15:53:04 -07001506type Binary struct {
1507 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001508
Colin Crossf506d872017-07-19 15:53:04 -07001509 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001510
Colin Cross6b4a32d2017-12-05 13:42:45 -08001511 isWrapperVariant bool
1512
Colin Crossc3315992017-12-08 19:12:36 -08001513 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001514 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001515}
1516
Alex Light24237172017-10-26 09:46:21 -07001517func (j *Binary) HostToolPath() android.OptionalPath {
1518 return android.OptionalPathForPath(j.binaryFile)
1519}
1520
Colin Crossf506d872017-07-19 15:53:04 -07001521func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001522 if ctx.Arch().ArchType == android.Common {
1523 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001524 if j.binaryProperties.Main_class != nil {
1525 if j.properties.Manifest != nil {
1526 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1527 }
1528 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1529 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1530 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1531 }
1532
Colin Cross6b4a32d2017-12-05 13:42:45 -08001533 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001534 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001535 // Handle the binary wrapper
1536 j.isWrapperVariant = true
1537
Colin Cross366938f2017-12-11 16:29:02 -08001538 if j.binaryProperties.Wrapper != nil {
1539 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001540 } else {
1541 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1542 }
1543
1544 // Depend on the installed jar so that the wrapper doesn't get executed by
1545 // another build rule before the jar has been installed.
1546 jarFile := ctx.PrimaryModule().(*Binary).installFile
1547
1548 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1549 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001550 }
Colin Cross2fe66872015-03-30 17:20:39 -07001551}
1552
Colin Crossf506d872017-07-19 15:53:04 -07001553func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001554 if ctx.Arch().ArchType == android.Common {
1555 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001556 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001557 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001558 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001559}
1560
Colin Crossf506d872017-07-19 15:53:04 -07001561func BinaryFactory() android.Module {
1562 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001563
Colin Cross36242852017-06-23 15:06:31 -07001564 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001565 &module.Module.properties,
1566 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001567 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001568 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001569 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001570
Colin Cross9ae1b922018-06-26 17:59:05 -07001571 module.Module.properties.Installable = proptools.BoolPtr(true)
1572
Colin Cross6b4a32d2017-12-05 13:42:45 -08001573 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1574 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001575 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001576}
1577
Colin Crossf506d872017-07-19 15:53:04 -07001578func BinaryHostFactory() android.Module {
1579 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001580
Colin Cross36242852017-06-23 15:06:31 -07001581 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001582 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001583 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001584 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001585
Colin Cross9ae1b922018-06-26 17:59:05 -07001586 module.Module.properties.Installable = proptools.BoolPtr(true)
1587
Colin Cross6b4a32d2017-12-05 13:42:45 -08001588 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1589 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001590 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001591}
1592
1593//
1594// Java prebuilts
1595//
1596
Colin Cross74d73e22017-08-02 11:05:49 -07001597type ImportProperties struct {
1598 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001599
Nan Zhangea568a42017-11-08 21:20:04 -08001600 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001601
1602 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001603
1604 // List of shared java libs that this module has dependencies to
1605 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001606
1607 // List of files to remove from the jar file(s)
1608 Exclude_files []string
1609
1610 // List of directories to remove from the jar file(s)
1611 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001612
1613 // if set to true, run Jetifier against .jar file. Defaults to false.
1614 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001615}
1616
1617type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001618 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001619 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001620 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001621
Colin Cross74d73e22017-08-02 11:05:49 -07001622 properties ImportProperties
1623
Colin Cross0a6e0072017-08-30 14:24:55 -07001624 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001625 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001626}
1627
Colin Cross83bb3162018-06-25 15:48:06 -07001628func (j *Import) sdkVersion() string {
1629 return String(j.properties.Sdk_version)
1630}
1631
1632func (j *Import) minSdkVersion() string {
1633 return j.sdkVersion()
1634}
1635
Colin Cross74d73e22017-08-02 11:05:49 -07001636func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001637 return &j.prebuilt
1638}
1639
Colin Cross74d73e22017-08-02 11:05:49 -07001640func (j *Import) PrebuiltSrcs() []string {
1641 return j.properties.Jars
1642}
1643
1644func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001645 return j.prebuilt.Name(j.ModuleBase.Name())
1646}
1647
Colin Cross74d73e22017-08-02 11:05:49 -07001648func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001649 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001650 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001651}
1652
Colin Cross74d73e22017-08-02 11:05:49 -07001653func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001654 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001655
Nan Zhang4c819fb2018-08-27 18:31:46 -07001656 jarName := ctx.ModuleName() + ".jar"
1657 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001658 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1659 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001660 if Bool(j.properties.Jetifier_enabled) {
1661 inputFile := outputFile
1662 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1663 TransformJetifier(ctx, outputFile, inputFile)
1664 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001665 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001666
1667 ctx.VisitDirectDeps(func(module android.Module) {
1668 otherName := ctx.OtherModuleName(module)
1669 tag := ctx.OtherModuleDependencyTag(module)
1670
1671 switch dep := module.(type) {
1672 case Dependency:
1673 switch tag {
1674 case libTag, staticLibTag:
1675 // sdk lib names from dependencies are re-exported
1676 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1677 }
1678 case SdkLibraryDependency:
1679 switch tag {
1680 case libTag:
1681 // names of sdk libs that are directly depended are exported
1682 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1683 }
1684 }
1685 })
1686
1687 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001688 if Bool(j.properties.Installable) {
1689 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1690 ctx.ModuleName()+".jar", outputFile)
1691 }
Colin Cross2fe66872015-03-30 17:20:39 -07001692}
1693
Colin Cross74d73e22017-08-02 11:05:49 -07001694var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001695
Nan Zhanged19fc32017-10-19 13:06:22 -07001696func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001697 if j.combinedClasspathFile == nil {
1698 return nil
1699 }
Colin Cross37f6d792018-07-12 12:28:41 -07001700 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001701}
1702
1703func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001704 if j.combinedClasspathFile == nil {
1705 return nil
1706 }
Colin Cross37f6d792018-07-12 12:28:41 -07001707 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001708}
1709
Colin Cross331a1212018-08-15 20:40:52 -07001710func (j *Import) ResourceJars() android.Paths {
1711 return nil
1712}
1713
1714func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001715 if j.combinedClasspathFile == nil {
1716 return nil
1717 }
Colin Cross331a1212018-08-15 20:40:52 -07001718 return android.Paths{j.combinedClasspathFile}
1719}
1720
Colin Cross74d73e22017-08-02 11:05:49 -07001721func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001722 return nil
1723}
1724
Jiyong Park1be96912018-05-28 18:02:19 +09001725func (j *Import) ExportedSdkLibs() []string {
1726 return j.exportedSdkLibs
1727}
1728
albaltai36ff7dc2018-12-25 14:35:23 +08001729// Add compile time check for interface implementation
1730var _ android.IDEInfo = (*Import)(nil)
1731var _ android.IDECustomizedModuleName = (*Import)(nil)
1732
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001733// Collect information for opening IDE project files in java/jdeps.go.
1734const (
1735 removedPrefix = "prebuilt_"
1736)
1737
1738func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1739 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1740}
1741
1742func (j *Import) IDECustomizedModuleName() string {
1743 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1744 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1745 // solution to get the Import name.
1746 name := j.Name()
1747 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001748 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001749 }
1750 return name
1751}
1752
Colin Cross74d73e22017-08-02 11:05:49 -07001753var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001754
Colin Cross74d73e22017-08-02 11:05:49 -07001755func ImportFactory() android.Module {
1756 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001757
Colin Cross74d73e22017-08-02 11:05:49 -07001758 module.AddProperties(&module.properties)
1759
1760 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001761 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001762 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001763}
1764
Colin Cross74d73e22017-08-02 11:05:49 -07001765func ImportFactoryHost() android.Module {
1766 module := &Import{}
1767
1768 module.AddProperties(&module.properties)
1769
1770 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001771 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001772 return module
1773}
1774
Colin Cross89536d42017-07-07 14:35:50 -07001775//
1776// Defaults
1777//
1778type Defaults struct {
1779 android.ModuleBase
1780 android.DefaultsModuleBase
1781}
1782
1783func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1784}
1785
1786func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1787}
1788
1789func defaultsFactory() android.Module {
1790 return DefaultsFactory()
1791}
1792
1793func DefaultsFactory(props ...interface{}) android.Module {
1794 module := &Defaults{}
1795
1796 module.AddProperties(props...)
1797 module.AddProperties(
1798 &CompilerProperties{},
1799 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001800 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001801 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001802 &aaptProperties{},
1803 &androidLibraryProperties{},
1804 &appProperties{},
1805 &appTestProperties{},
1806 &ImportProperties{},
1807 &AARImportProperties{},
1808 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001809 )
1810
1811 android.InitDefaultsModule(module)
1812
1813 return module
1814}
Nan Zhangea568a42017-11-08 21:20:04 -08001815
1816var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001817var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001818var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001819var inList = android.InList