blob: 7fd5344461a023dd3e50d8024b8005e58b8f76e5 [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
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
Colin Cross05518bc2018-09-27 15:06:19 -070098 // list of native libraries that will be provided in or alongside the resulting jar
99 Jni_libs []string `android:"arch_variant"`
100
Colin Cross7d5136f2015-05-11 13:39:40 -0700101 // 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
Colin Cross6af2e492018-05-22 11:12:33 -0700189 // if true, compile against the platform APIs instead of an SDK.
190 Platform_apis *bool
191
Colin Crossebe1a512017-11-14 13:12:14 -0800192 Aidl struct {
193 // Top level directories to pass to aidl tool
194 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700195
Colin Crossebe1a512017-11-14 13:12:14 -0800196 // Directories rooted at the Android.bp file to pass to aidl tool
197 Local_include_dirs []string
198
199 // directories that should be added as include directories for any aidl sources of modules
200 // that depend on this module, as well as to aidl for this module.
201 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100202
203 // whether to generate traces (for systrace) for this interface
204 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100205
206 // whether to generate Binder#GetTransaction name method.
207 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800208 }
Colin Cross92430102017-10-09 14:59:32 -0700209
210 // If true, export a copy of the module as a -hostdex module for host testing.
211 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700212
David Brazdil17ef5632018-06-27 10:27:45 +0100213 // If set to true, compile dex regardless of installable. Defaults to false.
214 Compile_dex *bool
215
Colin Cross1bd87802017-12-05 15:31:19 -0800216 Dex_preopt struct {
217 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
218 // true.
219 Enabled *bool
220
221 // If true, generate an app image (.art file) for this module.
222 App_image *bool
223
224 // If true, use a checked-in profile to guide optimization. Defaults to false unless
225 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
226 // that matches the name of this module, in which case it is defaulted to true.
227 Profile_guided *bool
228
229 // If set, provides the path to profile relative to the Android.bp file. If not set,
230 // defaults to searching for a file that matches the name of this module in the default
231 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
232 Profile *string
233 }
Colin Crossa22116e2017-10-19 14:18:58 -0700234
Colin Cross66dbc0b2017-12-28 12:23:20 -0800235 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700236 // If false, disable all optimization. Defaults to true for android_app and android_test
237 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800238 Enabled *bool
239
240 // If true, optimize for size by removing unused code. Defaults to true for apps,
241 // false for libraries and tests.
242 Shrink *bool
243
244 // If true, optimize bytecode. Defaults to false.
245 Optimize *bool
246
247 // If true, obfuscate bytecode. Defaults to false.
248 Obfuscate *bool
249
250 // If true, do not use the flag files generated by aapt that automatically keep
251 // classes referenced by the app manifest. Defaults to false.
252 No_aapt_flags *bool
253
254 // Flags to pass to proguard.
255 Proguard_flags []string
256
257 // Specifies the locations of files containing proguard flags.
258 Proguard_flags_files []string
259 }
260
Colin Cross1369cdb2017-09-29 17:58:17 -0700261 // When targeting 1.9, override the modules to use with --system
262 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700263}
264
Colin Cross46c9b8b2017-06-22 16:51:17 -0700265// Module contains the properties and members used by all java module types
266type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700267 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700268 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700269
Colin Cross89536d42017-07-07 14:35:50 -0700270 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700271 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700272 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700273
Colin Cross331a1212018-08-15 20:40:52 -0700274 // jar file containing header classes including static library dependencies, suitable for
275 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700276 headerJarFile android.Path
277
Colin Cross331a1212018-08-15 20:40:52 -0700278 // jar file containing implementation classes including static library dependencies but no
279 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700280 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700281
Colin Cross331a1212018-08-15 20:40:52 -0700282 // jar file containing only resources including from static library dependencies
283 resourceJar android.Path
284
285 // jar file containing implementation classes and resources including static library
286 // dependencies
287 implementationAndResourcesJar android.Path
288
289 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700290 dexJarFile android.Path
291
Colin Crosscb933592017-11-22 13:49:43 -0800292 // output file containing uninstrumented classes that will be instrumented by jacoco
293 jacocoReportClassesFile android.Path
294
Colin Cross66dbc0b2017-12-28 12:23:20 -0800295 // output file containing mapping of obfuscated names
296 proguardDictionary android.Path
297
Colin Cross331a1212018-08-15 20:40:52 -0700298 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700299 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700300
Colin Cross635c3b02016-05-18 15:37:25 -0700301 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700302
Colin Cross635c3b02016-05-18 15:37:25 -0700303 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700304
Colin Cross2fe66872015-03-30 17:20:39 -0700305 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700306 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800307
308 // list of .java files and srcjars that was passed to javac
309 compiledJavaSrcs android.Paths
310 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800311
312 // list of extra progurad flag files
313 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900314
315 // list of SDK lib names that this java moudule is exporting
316 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700317
318 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
319 // filter out Exclude_srcs, will be used by android.IDEInfo struct
320 expandIDEInfoCompiledSrcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700321}
322
Colin Cross54250902017-12-05 09:28:08 -0800323func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700324 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800325}
326
327var _ 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 Crossbe1da472017-07-07 15:59:46 -0700368var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700369 staticLibTag = dependencyTag{name: "staticlib"}
370 libTag = dependencyTag{name: "javalib"}
Colin Cross6a77c982018-06-19 22:43:34 -0700371 annoTag = dependencyTag{name: "annotation processor"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700372 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700373 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700374 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800375 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700376 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800377 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700378)
Colin Cross2fe66872015-03-30 17:20:39 -0700379
Colin Crossfc3674a2017-09-18 17:41:52 -0700380type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700381 useModule, useFiles, useDefaultLibs, invalidVersion bool
382
Colin Cross86a60ae2018-05-29 14:44:55 -0700383 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700384 systemModules string
385
Colin Crossa97c5d32018-03-28 14:58:31 -0700386 frameworkResModule string
387
Colin Cross86a60ae2018-05-29 14:44:55 -0700388 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700389 aidl android.Path
390}
391
Colin Cross3144dfc2018-01-03 15:06:47 -0800392func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
393 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
394}
395
396func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
397 return j.shouldInstrument(ctx) &&
398 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
399 ctx.Config().UnbundledBuild())
400}
401
Colin Cross83bb3162018-06-25 15:48:06 -0700402func (j *Module) sdkVersion() string {
403 return String(j.deviceProperties.Sdk_version)
404}
405
406func (j *Module) minSdkVersion() string {
407 if j.deviceProperties.Min_sdk_version != nil {
408 return *j.deviceProperties.Min_sdk_version
409 }
410 return j.sdkVersion()
411}
412
413type sdkContext interface {
414 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
415 sdkVersion() string
416 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
417 minSdkVersion() string
418}
419
420func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
421 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100422 case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700423 return ctx.Config().DefaultAppTargetSdk()
424 default:
425 return v
426 }
427}
428
429// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
430// it returns android.FutureApiLevel (10000).
431func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
432 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100433 case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700434 return ctx.Config().DefaultAppTargetSdkInt(), nil
435 default:
436 n := android.GetNumericSdkVersion(v)
437 if i, err := strconv.Atoi(n); err != nil {
438 return -1, fmt.Errorf("invalid sdk version %q", n)
439 } else {
440 return i, nil
441 }
442 }
443}
444
445func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
446 n, err := sdkVersionToNumber(ctx, v)
447 if err != nil {
448 return "", err
449 }
450 return strconv.Itoa(n), nil
451}
452
453func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
454 v := sdkContext.sdkVersion()
455 i, err := sdkVersionToNumber(ctx, v)
456 if err != nil {
457 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700458 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700459 }
460
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900461 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
462 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
463 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
464 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
465 if ctx.DeviceSpecific() || ctx.SocSpecific() {
466 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
467 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
468 }
469 }
470 version := strings.TrimPrefix(v, "system_")
471 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
472 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
473 v, allowed_versions)
474 }
475 }
476
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100477 toPrebuilt := func(sdk string) sdkDep {
478 var api, v string
479 if strings.Contains(sdk, "_") {
480 t := strings.Split(sdk, "_")
481 api = t[0]
482 v = t[1]
483 } else {
484 api = "public"
485 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900486 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100487 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700488 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100489 // There's no aidl for other SDKs yet.
490 // TODO(77525052): Add aidl files for other SDKs too.
491 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
492 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800493 jarPath := android.ExistentPathForSource(ctx, jar)
494 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700495 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700496
Colin Cross6510f912017-11-29 00:27:14 -0800497 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700498 return sdkDep{
499 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700500 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700501 }
502 }
503
Colin Crossfc3674a2017-09-18 17:41:52 -0700504 if !jarPath.Valid() {
505 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
506 return sdkDep{}
507 }
Colin Cross47ff2522017-10-02 14:22:08 -0700508
Colin Crossfc3674a2017-09-18 17:41:52 -0700509 if !aidlPath.Valid() {
510 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
511 return sdkDep{}
512 }
Colin Cross47ff2522017-10-02 14:22:08 -0700513
Colin Crossfc3674a2017-09-18 17:41:52 -0700514 return sdkDep{
515 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700516 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700517 aidl: aidlPath.Path(),
518 }
519 }
520
Colin Crossa97c5d32018-03-28 14:58:31 -0700521 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700522 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700523 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700524 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700525 systemModules: m + "_system_modules",
526 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700527 }
528 if m == "core.current.stubs" {
529 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100530 } else if m == "core.platform.api.stubs" {
531 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700532 }
533 return ret
534 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700535
Colin Cross6510f912017-11-29 00:27:14 -0800536 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100537 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700538 }
539
540 switch v {
541 case "":
542 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700543 useDefaultLibs: true,
544 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700545 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700546 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700547 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700548 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700549 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700550 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700551 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700552 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700553 return toModule("core.current.stubs", "")
Neil Fuller3c979c32018-09-11 09:29:31 +0100554 case "core_platform_current":
555 return toModule("core.platform.api.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700556 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100557 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700558 }
559}
560
Colin Crossbe1da472017-07-07 15:59:46 -0700561func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700562 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700563 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700564 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700565 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700566 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100567 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700568 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700569 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700570 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700571 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100572 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700573 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800574 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700575 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
576 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800577 }
Colin Crossbe1da472017-07-07 15:59:46 -0700578 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700579 } else if j.deviceProperties.System_modules == nil {
580 ctx.PropertyErrorf("no_standard_libs",
581 "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 +0100582 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700583 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700584 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100585 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700586 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800587 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800588 if ctx.ModuleName() == "android_stubs_current" ||
589 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700590 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700591 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800592 }
Colin Cross2fe66872015-03-30 17:20:39 -0700593 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700594
Colin Cross42d48b72018-08-29 14:10:52 -0700595 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
596 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100597 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700598 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100599 }, annoTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700600 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000601 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700602 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800603 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700604
605 if j.hasSrcExt(".proto") {
606 protoDeps(ctx, &j.protoProperties)
607 }
Colin Cross93e85952017-08-15 13:34:18 -0700608
609 if j.hasSrcExt(".kt") {
610 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
611 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700612 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700613 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800614
615 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700616 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800617 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700618}
619
620func hasSrcExt(srcs []string, ext string) bool {
621 for _, src := range srcs {
622 if filepath.Ext(src) == ext {
623 return true
624 }
625 }
626
627 return false
628}
629
Nan Zhang61eaedb2017-11-02 13:28:15 -0700630func shardPaths(paths android.Paths, shardSize int) []android.Paths {
631 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
632 for len(paths) > shardSize {
633 ret = append(ret, paths[0:shardSize])
634 paths = paths[shardSize:]
635 }
636 if len(paths) > 0 {
637 ret = append(ret, paths)
638 }
639 return ret
640}
641
Colin Cross6af17aa2017-09-20 12:59:05 -0700642func (j *Module) hasSrcExt(ext string) bool {
643 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700644}
645
Colin Cross46c9b8b2017-06-22 16:51:17 -0700646func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700647 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700648
Colin Crossebe1a512017-11-14 13:12:14 -0800649 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
650 aidlIncludes = append(aidlIncludes,
651 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
652 aidlIncludes = append(aidlIncludes,
653 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700654
Steven Moreland667f6882018-07-26 12:55:08 -0700655 flags := []string{"-b"}
656
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700657 if aidlPreprocess.Valid() {
658 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700659 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700660 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700661 }
662
Colin Cross635c3b02016-05-18 15:37:25 -0700663 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800664 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700665 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800666 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700667 flags = append(flags, "-I"+src.String())
668 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700669
Martijn Coeneneab15642018-03-09 09:29:59 +0100670 if Bool(j.deviceProperties.Aidl.Generate_traces) {
671 flags = append(flags, "-t")
672 }
673
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100674 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
675 flags = append(flags, "--transaction_names")
676 }
677
Colin Crossf03c82b2015-04-13 13:53:40 -0700678 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700679}
680
Colin Cross32f676a2017-09-06 13:41:06 -0700681type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800682 classpath classpath
683 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700684 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700685 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700686 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700687 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700688 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800689 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700690 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700691 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700692 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700693 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700694}
Colin Cross2fe66872015-03-30 17:20:39 -0700695
Colin Cross54250902017-12-05 09:28:08 -0800696func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
697 for _, f := range dep.Srcs() {
698 if f.Ext() != ".jar" {
699 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
700 ctx.OtherModuleName(dep.(blueprint.Module)))
701 }
702 }
703}
704
Jiyong Park2d492942018-03-05 17:44:10 +0900705type linkType int
706
707const (
708 javaCore linkType = iota
709 javaSdk
710 javaSystem
711 javaPlatform
712)
713
Colin Crossf19b9bb2018-03-26 14:42:44 -0700714func getLinkType(m *Module, name string) linkType {
Colin Cross83bb3162018-06-25 15:48:06 -0700715 ver := m.sdkVersion()
Colin Cross86a60ae2018-05-29 14:44:55 -0700716 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700717 switch {
Neil Fuller3c979c32018-09-11 09:29:31 +0100718 case name == "core.current.stubs" || ver == "core_current" ||
719 name == "core.platform.api.stubs" || ver == "core_platform_current" ||
720 noStdLibs || name == "stub-annotations" || name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900721 return javaCore
Nan Zhang863f05b2018-08-07 13:41:10 -0700722 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900723 return javaSystem
Nan Zhang863f05b2018-08-07 13:41:10 -0700724 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900725 return javaPlatform
Nan Zhang863f05b2018-08-07 13:41:10 -0700726 case name == "android_stubs_current" || ver == "current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700727 return javaSdk
728 case ver == "":
729 return javaPlatform
730 default:
731 if _, err := strconv.Atoi(ver); err != nil {
732 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
733 }
734 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900735 }
736}
737
Jiyong Park750e5572018-01-31 00:20:13 +0900738func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700739 if ctx.Host() {
740 return
741 }
742
743 myLinkType := getLinkType(from, ctx.ModuleName())
744 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900745 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."
746
747 switch myLinkType {
748 case javaCore:
749 if otherLinkType != javaCore {
750 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 +0900751 ctx.OtherModuleName(to))
752 }
Jiyong Park2d492942018-03-05 17:44:10 +0900753 break
754 case javaSdk:
755 if otherLinkType != javaCore && otherLinkType != javaSdk {
756 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
757 ctx.OtherModuleName(to))
758 }
759 break
760 case javaSystem:
761 if otherLinkType == javaPlatform {
762 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
763 ctx.OtherModuleName(to))
764 }
765 break
766 case javaPlatform:
767 // no restriction on link-type
768 break
Jiyong Park750e5572018-01-31 00:20:13 +0900769 }
770}
771
Colin Cross32f676a2017-09-06 13:41:06 -0700772func (j *Module) collectDeps(ctx android.ModuleContext) deps {
773 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700774
Colin Cross300f0382018-03-06 13:11:51 -0800775 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700776 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800777 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700778 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800779 } else if sdkDep.useFiles {
780 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700781 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800782 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
783 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700784 }
785
Colin Crossd11fcda2017-10-23 17:59:01 -0700786 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700787 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700788 tag := ctx.OtherModuleDependencyTag(module)
789
Jiyong Park750e5572018-01-31 00:20:13 +0900790 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700791 switch tag {
792 case bootClasspathTag, libTag, staticLibTag:
793 checkLinkType(ctx, j, to, tag.(dependencyTag))
794 }
Jiyong Park750e5572018-01-31 00:20:13 +0900795 }
Colin Cross54250902017-12-05 09:28:08 -0800796 switch dep := module.(type) {
797 case Dependency:
798 switch tag {
799 case bootClasspathTag:
800 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
801 case libTag:
802 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900803 // sdk lib names from dependencies are re-exported
804 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800805 case staticLibTag:
806 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
807 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
808 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700809 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900810 // sdk lib names from dependencies are re-exported
811 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700812 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700813 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800814 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100815 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800816 // framework.jar has a one-off dependency on the R.java and Manifest.java files
817 // generated by framework-res.apk
818 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
819 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800820 case frameworkApkTag:
821 if ctx.ModuleName() == "android_stubs_current" ||
822 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700823 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800824 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
825 // resource files out of there for aapt.
826 //
827 // Normally the package rule runs aapt, which includes the resource,
828 // but we're not running that in our package rule so just copy in the
829 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700830 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800831 }
Colin Cross54250902017-12-05 09:28:08 -0800832 case kotlinStdlibTag:
833 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800834 }
835
836 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900837 case SdkLibraryDependency:
838 switch tag {
839 case libTag:
840 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900841 // names of sdk libs that are directly depended are exported
842 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900843 default:
844 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
845 }
Colin Cross54250902017-12-05 09:28:08 -0800846 case android.SourceFileProducer:
847 switch tag {
848 case libTag:
849 checkProducesJars(ctx, dep)
850 deps.classpath = append(deps.classpath, dep.Srcs()...)
851 case staticLibTag:
852 checkProducesJars(ctx, dep)
853 deps.classpath = append(deps.classpath, dep.Srcs()...)
854 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
855 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
856 case android.DefaultsDepTag, android.SourceDepTag:
857 // Nothing to do
858 default:
859 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
860 }
861 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700862 switch tag {
863 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700864 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700865 case systemModulesTag:
866 if deps.systemModules != nil {
867 panic("Found two system module dependencies")
868 }
869 sm := module.(*SystemModules)
870 if sm.outputFile == nil {
871 panic("Missing directory for system module dependency")
872 }
873 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700874 default:
875 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700876 }
Colin Crossec7a0422017-07-07 14:47:12 -0700877 }
Colin Cross2fe66872015-03-30 17:20:39 -0700878 })
879
Jiyong Park1be96912018-05-28 18:02:19 +0900880 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
881
Colin Cross32f676a2017-09-06 13:41:06 -0700882 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700883}
884
Colin Cross83bb3162018-06-25 15:48:06 -0700885func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700886 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700887 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
888 if err != nil {
889 ctx.PropertyErrorf("sdk_version", "%s", err)
890 }
Nan Zhang357466b2018-04-17 17:38:36 -0700891 if javaVersion != "" {
892 ret = javaVersion
893 } else if ctx.Device() && sdk <= 23 {
894 ret = "1.7"
895 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
896 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700897 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700898 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
899 ret = "1.8"
900 } else {
901 ret = "1.9"
902 }
903
904 return ret
905}
906
Nan Zhanged19fc32017-10-19 13:06:22 -0700907func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700908
Colin Crossf03c82b2015-04-13 13:53:40 -0700909 var flags javaBuilderFlags
910
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100911 // javaVersion flag.
912 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
913
Nan Zhanged19fc32017-10-19 13:06:22 -0700914 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700915 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100916 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700917 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 }
Colin Cross6510f912017-11-29 00:27:14 -0800919 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700920 // Override the -g flag passed globally to remove local variable debug info to reduce
921 // disk and memory usage.
922 javacFlags = append(javacFlags, "-g:source,lines")
923 }
Colin Cross64162712017-08-08 13:17:59 -0700924
Colin Cross66548102018-06-19 22:47:35 -0700925 if ctx.Config().RunErrorProne() {
926 if config.ErrorProneClasspath == nil {
927 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
928 }
929
930 errorProneFlags := []string{
931 "-Xplugin:ErrorProne",
932 "${config.ErrorProneChecks}",
933 }
934 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
935
936 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
937 "'" + strings.Join(errorProneFlags, " ") + "'"
938 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800939 }
940
Nan Zhanged19fc32017-10-19 13:06:22 -0700941 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800942 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
943 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700944 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800945
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100946 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800947 !Bool(j.properties.No_standard_libs) &&
948 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
949 // Give host-side tools a version of OpenJDK's standard libraries
950 // close to what they're targeting. As of Dec 2017, AOSP is only
951 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
952 //
953 // When building with OpenJDK 8, the following should have no
954 // effect since those jars would be available by default.
955 //
956 // When building with OpenJDK 9 but targeting a version < 1.8,
957 // putting them on the bootclasspath means that:
958 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
959 // b) references to existing APIs are not reinterpreted in an
960 // OpenJDK 9-specific way, eg. calls to subclasses of
961 // java.nio.Buffer as in http://b/70862583
962 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
963 flags.bootClasspath = append(flags.bootClasspath,
964 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
965 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800966 if Bool(j.properties.Use_tools_jar) {
967 flags.bootClasspath = append(flags.bootClasspath,
968 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
969 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800970 }
971
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100972 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -0700973 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
974 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
975 }
976
Nan Zhanged19fc32017-10-19 13:06:22 -0700977 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700978 if deps.systemModules != nil {
979 flags.systemModules = append(flags.systemModules, deps.systemModules)
980 }
981
Nan Zhanged19fc32017-10-19 13:06:22 -0700982 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700983 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700984 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700985 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700986 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
987 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700988 }
989
Colin Cross81440082018-08-15 20:21:55 -0700990 if len(javacFlags) > 0 {
991 // optimization.
992 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
993 flags.javacFlags = "$javacFlags"
994 }
995
Nan Zhanged19fc32017-10-19 13:06:22 -0700996 return flags
997}
Colin Crossc0b06f12015-04-08 13:03:43 -0700998
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800999func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001000
Colin Crossebe1a512017-11-14 13:12:14 -08001001 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001002
1003 deps := j.collectDeps(ctx)
1004 flags := j.collectBuilderFlags(ctx, deps)
1005
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001006 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001007 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1008 }
1009 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001010 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001011 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001012 }
1013
Colin Crossaf050172017-11-15 23:01:59 -08001014 srcFiles = j.genSources(ctx, srcFiles, flags)
1015
1016 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001017 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001018 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001019
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001020 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1021 // that IDEInfo struct will use
1022 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1023
Colin Cross1ee23172017-10-18 14:44:18 -07001024 jarName := ctx.ModuleName() + ".jar"
1025
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001026 javaSrcFiles := srcFiles.FilterByExt(".java")
1027 var uniqueSrcFiles android.Paths
1028 set := make(map[string]bool)
1029 for _, v := range javaSrcFiles {
1030 if _, found := set[v.String()]; !found {
1031 set[v.String()] = true
1032 uniqueSrcFiles = append(uniqueSrcFiles, v)
1033 }
1034 }
1035
Colin Cross55f63ea2018-08-27 12:37:09 -07001036 var kotlinJars android.Paths
1037
Colin Cross93e85952017-08-15 13:34:18 -07001038 if srcFiles.HasExt(".kt") {
1039 // If there are kotlin files, compile them first but pass all the kotlin and java files
1040 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1041 // won't emit any classes for them.
1042
1043 flags.kotlincFlags = "-no-stdlib"
1044 if ctx.Device() {
1045 flags.kotlincFlags += " -no-jdk"
1046 }
1047
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001048 var kotlinSrcFiles android.Paths
1049 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1050 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1051
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001052 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001053 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1054 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1055
Colin Cross1ee23172017-10-18 14:44:18 -07001056 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001057 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001058 if ctx.Failed() {
1059 return
1060 }
1061
1062 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001063 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001064 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001065
Colin Cross93e85952017-08-15 13:34:18 -07001066 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001067 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001068 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001069 }
1070
Colin Cross55f63ea2018-08-27 12:37:09 -07001071 jars := append(android.Paths(nil), kotlinJars...)
1072
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001073 // Store the list of .java files that was passed to javac
1074 j.compiledJavaSrcs = uniqueSrcFiles
1075 j.compiledSrcJars = srcJars
1076
Nan Zhang61eaedb2017-11-02 13:28:15 -07001077 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001078 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001079 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1080 enable_sharding = true
1081 if len(j.properties.Annotation_processors) != 0 ||
1082 len(j.properties.Annotation_processor_classes) != 0 {
1083 ctx.PropertyErrorf("javac_shard_size",
1084 "%q cannot be set when annotation processors are enabled.",
1085 j.properties.Javac_shard_size)
1086 }
1087 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001088 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001089 if ctx.Failed() {
1090 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001091 }
1092 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001093 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001094 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001095 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001096 // If error-prone is enabled, add an additional rule to compile the java files into
1097 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001098 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001099 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1100 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001101 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001102 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001103 extraJarDeps = append(extraJarDeps, errorprone)
1104 }
1105
Nan Zhang61eaedb2017-11-02 13:28:15 -07001106 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001107 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001108 shardSize := int(*(j.properties.Javac_shard_size))
1109 var shardSrcs []android.Paths
1110 if len(uniqueSrcFiles) > 0 {
1111 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1112 for idx, shardSrc := range shardSrcs {
1113 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1114 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1115 jars = append(jars, classes)
1116 }
1117 }
1118 if len(srcJars) > 0 {
1119 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1120 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1121 jars = append(jars, classes)
1122 }
1123 } else {
1124 classes := android.PathForModuleOut(ctx, "javac", jarName)
1125 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1126 jars = append(jars, classes)
1127 }
Colin Crossd6891432017-09-27 17:39:56 -07001128 if ctx.Failed() {
1129 return
1130 }
Colin Cross2fe66872015-03-30 17:20:39 -07001131 }
1132
Colin Crosscedd4762018-09-13 11:26:19 -07001133 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1134 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001135 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1136
1137 var resArgs []string
1138 var resDeps android.Paths
1139
1140 resArgs = append(resArgs, dirArgs...)
1141 resDeps = append(resDeps, dirDeps...)
1142
1143 resArgs = append(resArgs, fileArgs...)
1144 resDeps = append(resDeps, fileDeps...)
1145
Colin Crossff3ae9d2018-04-10 16:15:18 -07001146 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001147 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001148 resArgs = append(resArgs, srcArgs...)
1149 resDeps = append(resDeps, srcDeps...)
1150 }
Colin Cross40a36712017-09-27 17:41:35 -07001151
1152 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001153 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001154 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001155 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001156 if ctx.Failed() {
1157 return
1158 }
1159 }
1160
Colin Cross331a1212018-08-15 20:40:52 -07001161 if len(deps.staticResourceJars) > 0 {
1162 var jars android.Paths
1163 if j.resourceJar != nil {
1164 jars = append(jars, j.resourceJar)
1165 }
1166 jars = append(jars, deps.staticResourceJars...)
1167
1168 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1169 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1170 false, nil, nil)
1171 j.resourceJar = combinedJar
1172 }
1173
Colin Cross32f676a2017-09-06 13:41:06 -07001174 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001175 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001176
Colin Cross366938f2017-12-11 16:29:02 -08001177 var manifest android.OptionalPath
1178 if j.properties.Manifest != nil {
1179 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1180 }
Colin Cross635acc92017-09-12 22:50:46 -07001181
Colin Cross0a6e0072017-08-30 14:24:55 -07001182 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001183 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001184 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001185
1186 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001187 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1188 // Optimization: skip the combine step if there is nothing to do
1189 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1190 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1191 // any if len(jars) == 1.
1192 outputFile = moduleOutPath
1193 } else {
1194 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1195 ctx.Build(pctx, android.BuildParams{
1196 Rule: android.Cp,
1197 Input: jars[0],
1198 Output: combinedJar,
1199 })
1200 outputFile = combinedJar
1201 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001202 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001203 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001204 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001205 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001206 outputFile = combinedJar
1207 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001208
Colin Cross331a1212018-08-15 20:40:52 -07001209 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001210 if j.properties.Jarjar_rules != nil {
1211 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001212 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001213 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001214 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1215 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001216
1217 // jarjar resource jar if necessary
1218 if j.resourceJar != nil {
1219 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1220 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1221 j.resourceJar = resourceJarJarFile
1222 }
1223
Colin Cross0a6e0072017-08-30 14:24:55 -07001224 if ctx.Failed() {
1225 return
1226 }
1227 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001228 j.implementationJarFile = outputFile
1229 if j.headerJarFile == nil {
1230 j.headerJarFile = j.implementationJarFile
1231 }
Colin Cross2fe66872015-03-30 17:20:39 -07001232
Colin Cross6510f912017-11-29 00:27:14 -08001233 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001234 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1235 j.properties.Instrument = true
1236 }
1237 }
1238
Colin Cross3144dfc2018-01-03 15:06:47 -08001239 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001240 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1241 }
1242
Colin Cross331a1212018-08-15 20:40:52 -07001243 // merge implementation jar with resources if necessary
1244 implementationAndResourcesJar := outputFile
1245 if j.resourceJar != nil {
1246 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1247 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1248 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1249 false, nil, nil)
1250 implementationAndResourcesJar = combinedJar
1251 }
1252
1253 j.implementationAndResourcesJar = implementationAndResourcesJar
1254
Colin Cross9ae1b922018-06-26 17:59:05 -07001255 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001256 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001257 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001258 if ctx.Failed() {
1259 return
1260 }
Colin Cross331a1212018-08-15 20:40:52 -07001261
1262 // merge dex jar with resources if necessary
1263 if j.resourceJar != nil {
1264 jars := android.Paths{dexOutputFile, j.resourceJar}
1265 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1266 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1267 false, nil, nil)
1268 dexOutputFile = combinedJar
1269 }
1270
1271 j.dexJarFile = dexOutputFile
1272
Colin Cross3063b782018-08-15 11:19:12 -07001273 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001274 } else {
1275 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001276 }
Colin Cross331a1212018-08-15 20:40:52 -07001277
Colin Crossb7a63242015-04-16 14:09:14 -07001278 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001279
1280 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1281 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001282}
1283
Colin Cross8eadbf02017-10-24 17:46:00 -07001284func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001285 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001286
1287 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001288 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001289 // Compile java sources into turbine.jar.
1290 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1291 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1292 if ctx.Failed() {
1293 return nil
1294 }
1295 jars = append(jars, turbineJar)
1296 }
1297
Colin Cross55f63ea2018-08-27 12:37:09 -07001298 jars = append(jars, extraJars...)
1299
Nan Zhanged19fc32017-10-19 13:06:22 -07001300 // Combine any static header libraries into classes-header.jar. If there is only
1301 // one input jar this step will be skipped.
1302 var headerJar android.Path
1303 jars = append(jars, deps.staticHeaderJars...)
1304
Colin Cross5c6ecc12017-10-23 18:12:27 -07001305 // we cannot skip the combine step for now if there is only one jar
1306 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1307 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001308 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1309 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001310 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001311
1312 if j.properties.Jarjar_rules != nil {
1313 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1314 // Transform classes.jar into classes-jarjar.jar
1315 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1316 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1317 headerJar = jarjarFile
1318 if ctx.Failed() {
1319 return nil
1320 }
1321 }
1322
1323 return headerJar
1324}
1325
Colin Crosscb933592017-11-22 13:49:43 -08001326func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001327 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001328
Colin Cross7a3139e2017-12-19 13:57:50 -08001329 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001330
Colin Cross84c38822018-01-03 15:59:46 -08001331 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001332 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1333
1334 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1335
1336 j.jacocoReportClassesFile = jacocoReportClassesFile
1337
1338 return instrumentedJar
1339}
1340
Colin Crossf506d872017-07-19 15:53:04 -07001341var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001342
Nan Zhanged19fc32017-10-19 13:06:22 -07001343func (j *Module) HeaderJars() android.Paths {
1344 return android.Paths{j.headerJarFile}
1345}
1346
1347func (j *Module) ImplementationJars() android.Paths {
1348 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001349}
1350
Colin Cross331a1212018-08-15 20:40:52 -07001351func (j *Module) ResourceJars() android.Paths {
1352 if j.resourceJar == nil {
1353 return nil
1354 }
1355 return android.Paths{j.resourceJar}
1356}
1357
1358func (j *Module) ImplementationAndResourcesJars() android.Paths {
1359 return android.Paths{j.implementationAndResourcesJar}
1360}
1361
Colin Cross46c9b8b2017-06-22 16:51:17 -07001362func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001363 return j.exportAidlIncludeDirs
1364}
1365
Jiyong Park1be96912018-05-28 18:02:19 +09001366func (j *Module) ExportedSdkLibs() []string {
1367 return j.exportedSdkLibs
1368}
1369
Colin Cross46c9b8b2017-06-22 16:51:17 -07001370var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001371
Colin Cross46c9b8b2017-06-22 16:51:17 -07001372func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001373 return j.logtagsSrcs
1374}
1375
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001376// Collect information for opening IDE project files in java/jdeps.go.
1377func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1378 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1379 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1380 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1381 if j.properties.Jarjar_rules != nil {
1382 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1383 }
1384}
1385
1386func (j *Module) CompilerDeps() []string {
1387 jdeps := []string{}
1388 jdeps = append(jdeps, j.properties.Libs...)
1389 jdeps = append(jdeps, j.properties.Static_libs...)
1390 return jdeps
1391}
1392
Colin Cross2fe66872015-03-30 17:20:39 -07001393//
1394// Java libraries (.jar file)
1395//
1396
Colin Crossf506d872017-07-19 15:53:04 -07001397type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001398 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001399}
1400
Colin Crossf506d872017-07-19 15:53:04 -07001401func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001402 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001403
Colin Cross9ae1b922018-06-26 17:59:05 -07001404 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001405 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1406 ctx.ModuleName()+".jar", j.outputFile)
1407 }
Colin Crossb7a63242015-04-16 14:09:14 -07001408}
1409
Colin Crossf506d872017-07-19 15:53:04 -07001410func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001411 j.deps(ctx)
1412}
1413
Colin Cross9ae1b922018-06-26 17:59:05 -07001414func LibraryFactory() android.Module {
1415 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001416
Colin Cross9ae1b922018-06-26 17:59:05 -07001417 module.AddProperties(
1418 &module.Module.properties,
1419 &module.Module.deviceProperties,
1420 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001421
Colin Cross9ae1b922018-06-26 17:59:05 -07001422 InitJavaModule(module, android.HostAndDeviceSupported)
1423 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001424}
1425
Colin Crossf506d872017-07-19 15:53:04 -07001426func LibraryHostFactory() android.Module {
1427 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001428
Colin Cross6af17aa2017-09-20 12:59:05 -07001429 module.AddProperties(
1430 &module.Module.properties,
1431 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001432
Colin Cross9ae1b922018-06-26 17:59:05 -07001433 module.Module.properties.Installable = proptools.BoolPtr(true)
1434
Colin Cross89536d42017-07-07 14:35:50 -07001435 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001436 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001437}
1438
1439//
Colin Crossb628ea52018-08-14 16:42:33 -07001440// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001441//
1442
1443type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001444 // list of compatibility suites (for example "cts", "vts") that the module should be
1445 // installed into.
1446 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001447
1448 // the name of the test configuration (for example "AndroidTest.xml") that should be
1449 // installed with the module.
1450 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001451
Jack He33338892018-09-19 02:21:28 -07001452 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1453 // should be installed with the module.
1454 Test_config_template *string `android:"arch_variant"`
1455
Colin Crossd96ca352018-08-10 16:06:24 -07001456 // list of files or filegroup modules that provide data that should be installed alongside
1457 // the test
1458 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001459}
1460
1461type Test struct {
1462 Library
1463
1464 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001465
1466 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001467 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001468}
1469
1470func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001471 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001472 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001473
1474 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001475}
1476
1477func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1478 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001479 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001480 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001481 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001482}
1483
1484func TestFactory() android.Module {
1485 module := &Test{}
1486
1487 module.AddProperties(
1488 &module.Module.properties,
1489 &module.Module.deviceProperties,
1490 &module.Module.protoProperties,
1491 &module.testProperties)
1492
Colin Cross9ae1b922018-06-26 17:59:05 -07001493 module.Module.properties.Installable = proptools.BoolPtr(true)
1494
Colin Cross05638fc2018-04-09 18:40:24 -07001495 InitJavaModule(module, android.HostAndDeviceSupported)
1496 android.InitDefaultableModule(module)
1497 return module
1498}
1499
1500func TestHostFactory() android.Module {
1501 module := &Test{}
1502
1503 module.AddProperties(
1504 &module.Module.properties,
1505 &module.Module.protoProperties,
1506 &module.testProperties)
1507
Colin Cross9ae1b922018-06-26 17:59:05 -07001508 module.Module.properties.Installable = proptools.BoolPtr(true)
1509
Colin Cross05638fc2018-04-09 18:40:24 -07001510 InitJavaModule(module, android.HostSupported)
1511 android.InitDefaultableModule(module)
1512 return module
1513}
1514
1515//
Colin Cross2fe66872015-03-30 17:20:39 -07001516// Java Binaries (.jar file plus wrapper script)
1517//
1518
Colin Crossf506d872017-07-19 15:53:04 -07001519type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001520 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001521 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001522}
1523
Colin Crossf506d872017-07-19 15:53:04 -07001524type Binary struct {
1525 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001526
Colin Crossf506d872017-07-19 15:53:04 -07001527 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001528
Colin Cross6b4a32d2017-12-05 13:42:45 -08001529 isWrapperVariant bool
1530
Colin Crossc3315992017-12-08 19:12:36 -08001531 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001532 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001533}
1534
Alex Light24237172017-10-26 09:46:21 -07001535func (j *Binary) HostToolPath() android.OptionalPath {
1536 return android.OptionalPathForPath(j.binaryFile)
1537}
1538
Colin Crossf506d872017-07-19 15:53:04 -07001539func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001540 if ctx.Arch().ArchType == android.Common {
1541 // Compile the jar
1542 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001543 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001544 // Handle the binary wrapper
1545 j.isWrapperVariant = true
1546
Colin Cross366938f2017-12-11 16:29:02 -08001547 if j.binaryProperties.Wrapper != nil {
1548 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001549 } else {
1550 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1551 }
1552
1553 // Depend on the installed jar so that the wrapper doesn't get executed by
1554 // another build rule before the jar has been installed.
1555 jarFile := ctx.PrimaryModule().(*Binary).installFile
1556
1557 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1558 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001559 }
Colin Cross2fe66872015-03-30 17:20:39 -07001560}
1561
Colin Crossf506d872017-07-19 15:53:04 -07001562func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001563 if ctx.Arch().ArchType == android.Common {
1564 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001565 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001566 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001567 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001568}
1569
Colin Crossf506d872017-07-19 15:53:04 -07001570func BinaryFactory() android.Module {
1571 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001572
Colin Cross36242852017-06-23 15:06:31 -07001573 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001574 &module.Module.properties,
1575 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001576 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001577 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001578
Colin Cross9ae1b922018-06-26 17:59:05 -07001579 module.Module.properties.Installable = proptools.BoolPtr(true)
1580
Colin Cross6b4a32d2017-12-05 13:42:45 -08001581 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1582 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001583 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001584}
1585
Colin Crossf506d872017-07-19 15:53:04 -07001586func BinaryHostFactory() android.Module {
1587 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001588
Colin Cross36242852017-06-23 15:06:31 -07001589 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001590 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001591 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001592 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001593
Colin Cross9ae1b922018-06-26 17:59:05 -07001594 module.Module.properties.Installable = proptools.BoolPtr(true)
1595
Colin Cross6b4a32d2017-12-05 13:42:45 -08001596 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1597 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001598 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001599}
1600
1601//
1602// Java prebuilts
1603//
1604
Colin Cross74d73e22017-08-02 11:05:49 -07001605type ImportProperties struct {
1606 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001607
Nan Zhangea568a42017-11-08 21:20:04 -08001608 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001609
1610 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001611
1612 // List of shared java libs that this module has dependencies to
1613 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001614
1615 // List of files to remove from the jar file(s)
1616 Exclude_files []string
1617
1618 // List of directories to remove from the jar file(s)
1619 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001620
1621 // if set to true, run Jetifier against .jar file. Defaults to false.
1622 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001623}
1624
1625type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001626 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001627 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001628
Colin Cross74d73e22017-08-02 11:05:49 -07001629 properties ImportProperties
1630
Colin Cross0a6e0072017-08-30 14:24:55 -07001631 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001632 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001633}
1634
Colin Cross83bb3162018-06-25 15:48:06 -07001635func (j *Import) sdkVersion() string {
1636 return String(j.properties.Sdk_version)
1637}
1638
1639func (j *Import) minSdkVersion() string {
1640 return j.sdkVersion()
1641}
1642
Colin Cross74d73e22017-08-02 11:05:49 -07001643func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001644 return &j.prebuilt
1645}
1646
Colin Cross74d73e22017-08-02 11:05:49 -07001647func (j *Import) PrebuiltSrcs() []string {
1648 return j.properties.Jars
1649}
1650
1651func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001652 return j.prebuilt.Name(j.ModuleBase.Name())
1653}
1654
Colin Cross74d73e22017-08-02 11:05:49 -07001655func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001656 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001657 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001658}
1659
Colin Cross74d73e22017-08-02 11:05:49 -07001660func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001661 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001662
Nan Zhang4c819fb2018-08-27 18:31:46 -07001663 jarName := ctx.ModuleName() + ".jar"
1664 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001665 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1666 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001667 if Bool(j.properties.Jetifier_enabled) {
1668 inputFile := outputFile
1669 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1670 TransformJetifier(ctx, outputFile, inputFile)
1671 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001672 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001673
1674 ctx.VisitDirectDeps(func(module android.Module) {
1675 otherName := ctx.OtherModuleName(module)
1676 tag := ctx.OtherModuleDependencyTag(module)
1677
1678 switch dep := module.(type) {
1679 case Dependency:
1680 switch tag {
1681 case libTag, staticLibTag:
1682 // sdk lib names from dependencies are re-exported
1683 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1684 }
1685 case SdkLibraryDependency:
1686 switch tag {
1687 case libTag:
1688 // names of sdk libs that are directly depended are exported
1689 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1690 }
1691 }
1692 })
1693
1694 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001695 if Bool(j.properties.Installable) {
1696 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1697 ctx.ModuleName()+".jar", outputFile)
1698 }
Colin Cross2fe66872015-03-30 17:20:39 -07001699}
1700
Colin Cross74d73e22017-08-02 11:05:49 -07001701var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001702
Nan Zhanged19fc32017-10-19 13:06:22 -07001703func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001704 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001705}
1706
1707func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001708 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001709}
1710
Colin Cross331a1212018-08-15 20:40:52 -07001711func (j *Import) ResourceJars() android.Paths {
1712 return nil
1713}
1714
1715func (j *Import) ImplementationAndResourcesJars() android.Paths {
1716 return android.Paths{j.combinedClasspathFile}
1717}
1718
Colin Cross74d73e22017-08-02 11:05:49 -07001719func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001720 return nil
1721}
1722
Jiyong Park1be96912018-05-28 18:02:19 +09001723func (j *Import) ExportedSdkLibs() []string {
1724 return j.exportedSdkLibs
1725}
1726
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001727// Collect information for opening IDE project files in java/jdeps.go.
1728const (
1729 removedPrefix = "prebuilt_"
1730)
1731
1732func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1733 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1734}
1735
1736func (j *Import) IDECustomizedModuleName() string {
1737 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1738 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1739 // solution to get the Import name.
1740 name := j.Name()
1741 if strings.HasPrefix(name, removedPrefix) {
1742 name = strings.Trim(name, removedPrefix)
1743 }
1744 return name
1745}
1746
Colin Cross74d73e22017-08-02 11:05:49 -07001747var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001748
Colin Cross74d73e22017-08-02 11:05:49 -07001749func ImportFactory() android.Module {
1750 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001751
Colin Cross74d73e22017-08-02 11:05:49 -07001752 module.AddProperties(&module.properties)
1753
1754 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001755 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1756 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001757}
1758
Colin Cross74d73e22017-08-02 11:05:49 -07001759func ImportFactoryHost() android.Module {
1760 module := &Import{}
1761
1762 module.AddProperties(&module.properties)
1763
1764 android.InitPrebuiltModule(module, &module.properties.Jars)
1765 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1766 return module
1767}
1768
Colin Cross89536d42017-07-07 14:35:50 -07001769//
1770// Defaults
1771//
1772type Defaults struct {
1773 android.ModuleBase
1774 android.DefaultsModuleBase
1775}
1776
1777func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1778}
1779
1780func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1781}
1782
1783func defaultsFactory() android.Module {
1784 return DefaultsFactory()
1785}
1786
1787func DefaultsFactory(props ...interface{}) android.Module {
1788 module := &Defaults{}
1789
1790 module.AddProperties(props...)
1791 module.AddProperties(
1792 &CompilerProperties{},
1793 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001794 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001795 )
1796
1797 android.InitDefaultsModule(module)
1798
1799 return module
1800}
Nan Zhangea568a42017-11-08 21:20:04 -08001801
1802var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001803var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001804var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001805var inList = android.InList