blob: b60f9c73941d99c40964820df013e935b2f6c00f [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
78 // list of files that should be excluded from java_resources
79 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
98 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
Colin Cross540eff82017-06-22 17:01:52 -0700101 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700102 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700103
104 // If not blank, set the java version passed to javac as -source and -target
105 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700106
Colin Cross9ae1b922018-06-26 17:59:05 -0700107 // If set to true, allow this module to be dexed and installed on devices. Has no
108 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700109 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700110
Colin Cross0f37af02017-09-27 17:42:05 -0700111 // If set to true, include sources used to compile the module in to the final jar
112 Include_srcs *bool
113
Colin Cross32f676a2017-09-06 13:41:06 -0700114 // List of modules to use as annotation processors
115 Annotation_processors []string
116
117 // List of classes to pass to javac to use as annotation processors
118 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119
Nan Zhang61eaedb2017-11-02 13:28:15 -0700120 // The number of Java source entries each Javac instance can process
121 Javac_shard_size *int64
122
Nan Zhang5f8cb422018-02-06 10:34:32 -0800123 // Add host jdk tools.jar to bootclasspath
124 Use_tools_jar *bool
125
Colin Cross1369cdb2017-09-29 17:58:17 -0700126 Openjdk9 struct {
127 // List of source files that should only be used when passing -source 1.9
128 Srcs []string
129
130 // List of javac flags that should only be used when passing -source 1.9
131 Javacflags []string
132 }
Colin Crosscb933592017-11-22 13:49:43 -0800133
Colin Cross81440082018-08-15 20:21:55 -0700134 // When compiling language level 9+ .java code in packages that are part of
135 // a system module, patch_module names the module that your sources and
136 // dependencies should be patched into. The Android runtime currently
137 // doesn't implement the JEP 261 module system so this option is only
138 // supported at compile time. It should only be needed to compile tests in
139 // packages that exist in libcore and which are inconvenient to move
140 // elsewhere.
141 Patch_module *string
142
Colin Crosscb933592017-11-22 13:49:43 -0800143 Jacoco struct {
144 // List of classes to include for instrumentation with jacoco to collect coverage
145 // information at runtime when building with coverage enabled. If unset defaults to all
146 // classes.
147 // Supports '*' as the last character of an entry in the list as a wildcard match.
148 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
149 // it matches classes in the package that have the class name as a prefix.
150 Include_filter []string
151
152 // List of classes to exclude from instrumentation with jacoco to collect coverage
153 // information at runtime when building with coverage enabled. Overrides classes selected
154 // by the include_filter property.
155 // Supports '*' as the last character of an entry in the list as a wildcard match.
156 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
157 // it matches classes in the package that have the class name as a prefix.
158 Exclude_filter []string
159 }
160
Andreas Gampef3e5b552018-01-22 21:27:21 -0800161 Errorprone struct {
162 // List of javac flags that should only be used when running errorprone.
163 Javacflags []string
164 }
165
Colin Cross0f2ee152017-12-14 15:22:43 -0800166 Proto struct {
167 // List of extra options that will be passed to the proto generator.
168 Output_params []string
169 }
170
Colin Crosscb933592017-11-22 13:49:43 -0800171 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700172}
173
Colin Cross89536d42017-07-07 14:35:50 -0700174type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700175 // list of module-specific flags that will be used for dex compiles
176 Dxflags []string `android:"arch_variant"`
177
Colin Cross83bb3162018-06-25 15:48:06 -0700178 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
179 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800180 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
183 // Defaults to sdk_version if not set.
184 Min_sdk_version *string
185
Colin Cross6af2e492018-05-22 11:12:33 -0700186 // if true, compile against the platform APIs instead of an SDK.
187 Platform_apis *bool
188
Colin Crossebe1a512017-11-14 13:12:14 -0800189 Aidl struct {
190 // Top level directories to pass to aidl tool
191 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700192
Colin Crossebe1a512017-11-14 13:12:14 -0800193 // Directories rooted at the Android.bp file to pass to aidl tool
194 Local_include_dirs []string
195
196 // directories that should be added as include directories for any aidl sources of modules
197 // that depend on this module, as well as to aidl for this module.
198 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100199
200 // whether to generate traces (for systrace) for this interface
201 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100202
203 // whether to generate Binder#GetTransaction name method.
204 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800205 }
Colin Cross92430102017-10-09 14:59:32 -0700206
207 // If true, export a copy of the module as a -hostdex module for host testing.
208 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700209
David Brazdil17ef5632018-06-27 10:27:45 +0100210 // If set to true, compile dex regardless of installable. Defaults to false.
211 Compile_dex *bool
212
Colin Cross1bd87802017-12-05 15:31:19 -0800213 Dex_preopt struct {
214 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
215 // true.
216 Enabled *bool
217
218 // If true, generate an app image (.art file) for this module.
219 App_image *bool
220
221 // If true, use a checked-in profile to guide optimization. Defaults to false unless
222 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
223 // that matches the name of this module, in which case it is defaulted to true.
224 Profile_guided *bool
225
226 // If set, provides the path to profile relative to the Android.bp file. If not set,
227 // defaults to searching for a file that matches the name of this module in the default
228 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
229 Profile *string
230 }
Colin Crossa22116e2017-10-19 14:18:58 -0700231
Colin Cross66dbc0b2017-12-28 12:23:20 -0800232 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700233 // If false, disable all optimization. Defaults to true for android_app and android_test
234 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800235 Enabled *bool
236
237 // If true, optimize for size by removing unused code. Defaults to true for apps,
238 // false for libraries and tests.
239 Shrink *bool
240
241 // If true, optimize bytecode. Defaults to false.
242 Optimize *bool
243
244 // If true, obfuscate bytecode. Defaults to false.
245 Obfuscate *bool
246
247 // If true, do not use the flag files generated by aapt that automatically keep
248 // classes referenced by the app manifest. Defaults to false.
249 No_aapt_flags *bool
250
251 // Flags to pass to proguard.
252 Proguard_flags []string
253
254 // Specifies the locations of files containing proguard flags.
255 Proguard_flags_files []string
256 }
257
Colin Cross1369cdb2017-09-29 17:58:17 -0700258 // When targeting 1.9, override the modules to use with --system
259 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700260}
261
Colin Cross46c9b8b2017-06-22 16:51:17 -0700262// Module contains the properties and members used by all java module types
263type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700264 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700265 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700266
Colin Cross89536d42017-07-07 14:35:50 -0700267 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700268 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700269 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700270
Colin Cross331a1212018-08-15 20:40:52 -0700271 // jar file containing header classes including static library dependencies, suitable for
272 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700273 headerJarFile android.Path
274
Colin Cross331a1212018-08-15 20:40:52 -0700275 // jar file containing implementation classes including static library dependencies but no
276 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700278
Colin Cross331a1212018-08-15 20:40:52 -0700279 // jar file containing only resources including from static library dependencies
280 resourceJar android.Path
281
282 // jar file containing implementation classes and resources including static library
283 // dependencies
284 implementationAndResourcesJar android.Path
285
286 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700287 dexJarFile android.Path
288
Colin Crosscb933592017-11-22 13:49:43 -0800289 // output file containing uninstrumented classes that will be instrumented by jacoco
290 jacocoReportClassesFile android.Path
291
Colin Cross66dbc0b2017-12-28 12:23:20 -0800292 // output file containing mapping of obfuscated names
293 proguardDictionary android.Path
294
Colin Cross331a1212018-08-15 20:40:52 -0700295 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700296 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700297
Colin Cross635c3b02016-05-18 15:37:25 -0700298 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700299
Colin Cross635c3b02016-05-18 15:37:25 -0700300 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700301
Colin Cross2fe66872015-03-30 17:20:39 -0700302 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700303 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800304
305 // list of .java files and srcjars that was passed to javac
306 compiledJavaSrcs android.Paths
307 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800308
309 // list of extra progurad flag files
310 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900311
312 // list of SDK lib names that this java moudule is exporting
313 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700314}
315
Colin Cross54250902017-12-05 09:28:08 -0800316func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700317 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800318}
319
320var _ android.SourceFileProducer = (*Module)(nil)
321
Colin Crossf506d872017-07-19 15:53:04 -0700322type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700323 HeaderJars() android.Paths
324 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700325 ResourceJars() android.Paths
326 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700327 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900328 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700329}
330
Jiyong Parkc678ad32018-04-10 13:07:10 +0900331type SdkLibraryDependency interface {
332 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900333 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900334}
335
Nan Zhangb2b33de2018-02-23 11:18:47 -0800336type SrcDependency interface {
337 CompiledSrcs() android.Paths
338 CompiledSrcJars() android.Paths
339}
340
341func (j *Module) CompiledSrcs() android.Paths {
342 return j.compiledJavaSrcs
343}
344
345func (j *Module) CompiledSrcJars() android.Paths {
346 return j.compiledSrcJars
347}
348
349var _ SrcDependency = (*Module)(nil)
350
Colin Cross89536d42017-07-07 14:35:50 -0700351func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
352 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
353 android.InitDefaultableModule(module)
354}
355
Colin Crossbe1da472017-07-07 15:59:46 -0700356type dependencyTag struct {
357 blueprint.BaseDependencyTag
358 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700359}
360
Colin Crossbe1da472017-07-07 15:59:46 -0700361var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700362 staticLibTag = dependencyTag{name: "staticlib"}
363 libTag = dependencyTag{name: "javalib"}
Colin Cross6a77c982018-06-19 22:43:34 -0700364 annoTag = dependencyTag{name: "annotation processor"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700365 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700366 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700367 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800368 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700369 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800370 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700371)
Colin Cross2fe66872015-03-30 17:20:39 -0700372
Colin Crossfc3674a2017-09-18 17:41:52 -0700373type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700374 useModule, useFiles, useDefaultLibs, invalidVersion bool
375
Colin Cross86a60ae2018-05-29 14:44:55 -0700376 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700377 systemModules string
378
Colin Crossa97c5d32018-03-28 14:58:31 -0700379 frameworkResModule string
380
Colin Cross86a60ae2018-05-29 14:44:55 -0700381 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700382 aidl android.Path
383}
384
Colin Cross3144dfc2018-01-03 15:06:47 -0800385func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
386 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
387}
388
389func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
390 return j.shouldInstrument(ctx) &&
391 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
392 ctx.Config().UnbundledBuild())
393}
394
Colin Cross83bb3162018-06-25 15:48:06 -0700395func (j *Module) sdkVersion() string {
396 return String(j.deviceProperties.Sdk_version)
397}
398
399func (j *Module) minSdkVersion() string {
400 if j.deviceProperties.Min_sdk_version != nil {
401 return *j.deviceProperties.Min_sdk_version
402 }
403 return j.sdkVersion()
404}
405
406type sdkContext interface {
407 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
408 sdkVersion() string
409 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
410 minSdkVersion() string
411}
412
413func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
414 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100415 case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700416 return ctx.Config().DefaultAppTargetSdk()
417 default:
418 return v
419 }
420}
421
422// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
423// it returns android.FutureApiLevel (10000).
424func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
425 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100426 case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700427 return ctx.Config().DefaultAppTargetSdkInt(), nil
428 default:
429 n := android.GetNumericSdkVersion(v)
430 if i, err := strconv.Atoi(n); err != nil {
431 return -1, fmt.Errorf("invalid sdk version %q", n)
432 } else {
433 return i, nil
434 }
435 }
436}
437
438func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
439 n, err := sdkVersionToNumber(ctx, v)
440 if err != nil {
441 return "", err
442 }
443 return strconv.Itoa(n), nil
444}
445
446func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
447 v := sdkContext.sdkVersion()
448 i, err := sdkVersionToNumber(ctx, v)
449 if err != nil {
450 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700451 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700452 }
453
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900454 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
455 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
456 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
457 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
458 if ctx.DeviceSpecific() || ctx.SocSpecific() {
459 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
460 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
461 }
462 }
463 version := strings.TrimPrefix(v, "system_")
464 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
465 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
466 v, allowed_versions)
467 }
468 }
469
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100470 toPrebuilt := func(sdk string) sdkDep {
471 var api, v string
472 if strings.Contains(sdk, "_") {
473 t := strings.Split(sdk, "_")
474 api = t[0]
475 v = t[1]
476 } else {
477 api = "public"
478 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900479 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100480 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700481 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100482 // There's no aidl for other SDKs yet.
483 // TODO(77525052): Add aidl files for other SDKs too.
484 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
485 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800486 jarPath := android.ExistentPathForSource(ctx, jar)
487 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700488 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700489
Colin Cross6510f912017-11-29 00:27:14 -0800490 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700491 return sdkDep{
492 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700493 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700494 }
495 }
496
Colin Crossfc3674a2017-09-18 17:41:52 -0700497 if !jarPath.Valid() {
498 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
499 return sdkDep{}
500 }
Colin Cross47ff2522017-10-02 14:22:08 -0700501
Colin Crossfc3674a2017-09-18 17:41:52 -0700502 if !aidlPath.Valid() {
503 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
504 return sdkDep{}
505 }
Colin Cross47ff2522017-10-02 14:22:08 -0700506
Colin Crossfc3674a2017-09-18 17:41:52 -0700507 return sdkDep{
508 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700509 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700510 aidl: aidlPath.Path(),
511 }
512 }
513
Colin Crossa97c5d32018-03-28 14:58:31 -0700514 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700515 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700516 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700517 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700518 systemModules: m + "_system_modules",
519 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700520 }
521 if m == "core.current.stubs" {
522 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100523 } else if m == "core.platform.api.stubs" {
524 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700525 }
526 return ret
527 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700528
Colin Cross6510f912017-11-29 00:27:14 -0800529 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100530 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700531 }
532
533 switch v {
534 case "":
535 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700536 useDefaultLibs: true,
537 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700538 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700539 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700540 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700541 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700542 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700543 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700544 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700545 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700546 return toModule("core.current.stubs", "")
Neil Fuller3c979c32018-09-11 09:29:31 +0100547 case "core_platform_current":
548 return toModule("core.platform.api.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700549 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100550 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700551 }
552}
553
Colin Crossbe1da472017-07-07 15:59:46 -0700554func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700555 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700556 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700557 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700558 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700559 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100560 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700561 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700562 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700563 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700564 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100565 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700566 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800567 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700568 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
569 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800570 }
Colin Crossbe1da472017-07-07 15:59:46 -0700571 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700572 } else if j.deviceProperties.System_modules == nil {
573 ctx.PropertyErrorf("no_standard_libs",
574 "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 +0100575 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700576 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700577 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100578 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700579 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800580 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800581 if ctx.ModuleName() == "android_stubs_current" ||
582 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700583 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700584 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800585 }
Colin Cross2fe66872015-03-30 17:20:39 -0700586 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700587
Colin Cross42d48b72018-08-29 14:10:52 -0700588 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
589 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100590 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700591 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100592 }, annoTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700593 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000594 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700595 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800596 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700597
598 if j.hasSrcExt(".proto") {
599 protoDeps(ctx, &j.protoProperties)
600 }
Colin Cross93e85952017-08-15 13:34:18 -0700601
602 if j.hasSrcExt(".kt") {
603 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
604 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700605 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700606 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800607
608 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700609 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800610 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700611}
612
613func hasSrcExt(srcs []string, ext string) bool {
614 for _, src := range srcs {
615 if filepath.Ext(src) == ext {
616 return true
617 }
618 }
619
620 return false
621}
622
Nan Zhang61eaedb2017-11-02 13:28:15 -0700623func shardPaths(paths android.Paths, shardSize int) []android.Paths {
624 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
625 for len(paths) > shardSize {
626 ret = append(ret, paths[0:shardSize])
627 paths = paths[shardSize:]
628 }
629 if len(paths) > 0 {
630 ret = append(ret, paths)
631 }
632 return ret
633}
634
Colin Cross6af17aa2017-09-20 12:59:05 -0700635func (j *Module) hasSrcExt(ext string) bool {
636 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700637}
638
Colin Cross46c9b8b2017-06-22 16:51:17 -0700639func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700640 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700641
Colin Crossebe1a512017-11-14 13:12:14 -0800642 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
643 aidlIncludes = append(aidlIncludes,
644 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
645 aidlIncludes = append(aidlIncludes,
646 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700647
Steven Moreland667f6882018-07-26 12:55:08 -0700648 flags := []string{"-b"}
649
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700650 if aidlPreprocess.Valid() {
651 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700652 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700653 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700654 }
655
Colin Cross635c3b02016-05-18 15:37:25 -0700656 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800657 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700658 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800659 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700660 flags = append(flags, "-I"+src.String())
661 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700662
Martijn Coeneneab15642018-03-09 09:29:59 +0100663 if Bool(j.deviceProperties.Aidl.Generate_traces) {
664 flags = append(flags, "-t")
665 }
666
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100667 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
668 flags = append(flags, "--transaction_names")
669 }
670
Colin Crossf03c82b2015-04-13 13:53:40 -0700671 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700672}
673
Colin Cross32f676a2017-09-06 13:41:06 -0700674type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800675 classpath classpath
676 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700677 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700678 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700679 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700680 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700681 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800682 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700683 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700684 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700685 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700686 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700687}
Colin Cross2fe66872015-03-30 17:20:39 -0700688
Colin Cross54250902017-12-05 09:28:08 -0800689func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
690 for _, f := range dep.Srcs() {
691 if f.Ext() != ".jar" {
692 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
693 ctx.OtherModuleName(dep.(blueprint.Module)))
694 }
695 }
696}
697
Jiyong Park2d492942018-03-05 17:44:10 +0900698type linkType int
699
700const (
701 javaCore linkType = iota
702 javaSdk
703 javaSystem
704 javaPlatform
705)
706
Colin Crossf19b9bb2018-03-26 14:42:44 -0700707func getLinkType(m *Module, name string) linkType {
Colin Cross83bb3162018-06-25 15:48:06 -0700708 ver := m.sdkVersion()
Colin Cross86a60ae2018-05-29 14:44:55 -0700709 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700710 switch {
Neil Fuller3c979c32018-09-11 09:29:31 +0100711 case name == "core.current.stubs" || ver == "core_current" ||
712 name == "core.platform.api.stubs" || ver == "core_platform_current" ||
713 noStdLibs || name == "stub-annotations" || name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900714 return javaCore
Nan Zhang863f05b2018-08-07 13:41:10 -0700715 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900716 return javaSystem
Nan Zhang863f05b2018-08-07 13:41:10 -0700717 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900718 return javaPlatform
Nan Zhang863f05b2018-08-07 13:41:10 -0700719 case name == "android_stubs_current" || ver == "current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700720 return javaSdk
721 case ver == "":
722 return javaPlatform
723 default:
724 if _, err := strconv.Atoi(ver); err != nil {
725 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
726 }
727 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900728 }
729}
730
Jiyong Park750e5572018-01-31 00:20:13 +0900731func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700732 if ctx.Host() {
733 return
734 }
735
736 myLinkType := getLinkType(from, ctx.ModuleName())
737 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900738 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."
739
740 switch myLinkType {
741 case javaCore:
742 if otherLinkType != javaCore {
743 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 +0900744 ctx.OtherModuleName(to))
745 }
Jiyong Park2d492942018-03-05 17:44:10 +0900746 break
747 case javaSdk:
748 if otherLinkType != javaCore && otherLinkType != javaSdk {
749 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
750 ctx.OtherModuleName(to))
751 }
752 break
753 case javaSystem:
754 if otherLinkType == javaPlatform {
755 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
756 ctx.OtherModuleName(to))
757 }
758 break
759 case javaPlatform:
760 // no restriction on link-type
761 break
Jiyong Park750e5572018-01-31 00:20:13 +0900762 }
763}
764
Colin Cross32f676a2017-09-06 13:41:06 -0700765func (j *Module) collectDeps(ctx android.ModuleContext) deps {
766 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700767
Colin Cross300f0382018-03-06 13:11:51 -0800768 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700769 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800770 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700771 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800772 } else if sdkDep.useFiles {
773 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700774 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800775 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
776 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700777 }
778
Colin Crossd11fcda2017-10-23 17:59:01 -0700779 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700780 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700781 tag := ctx.OtherModuleDependencyTag(module)
782
Jiyong Park750e5572018-01-31 00:20:13 +0900783 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700784 switch tag {
785 case bootClasspathTag, libTag, staticLibTag:
786 checkLinkType(ctx, j, to, tag.(dependencyTag))
787 }
Jiyong Park750e5572018-01-31 00:20:13 +0900788 }
Colin Cross54250902017-12-05 09:28:08 -0800789 switch dep := module.(type) {
790 case Dependency:
791 switch tag {
792 case bootClasspathTag:
793 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
794 case libTag:
795 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900796 // sdk lib names from dependencies are re-exported
797 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800798 case staticLibTag:
799 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
800 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
801 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700802 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
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 Cross6a77c982018-06-19 22:43:34 -0700805 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700806 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800807 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100808 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800809 // framework.jar has a one-off dependency on the R.java and Manifest.java files
810 // generated by framework-res.apk
811 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
812 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800813 case frameworkApkTag:
814 if ctx.ModuleName() == "android_stubs_current" ||
815 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700816 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800817 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
818 // resource files out of there for aapt.
819 //
820 // Normally the package rule runs aapt, which includes the resource,
821 // but we're not running that in our package rule so just copy in the
822 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700823 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800824 }
Colin Cross54250902017-12-05 09:28:08 -0800825 case kotlinStdlibTag:
826 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800827 }
828
829 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900830 case SdkLibraryDependency:
831 switch tag {
832 case libTag:
833 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900834 // names of sdk libs that are directly depended are exported
835 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900836 default:
837 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
838 }
Colin Cross54250902017-12-05 09:28:08 -0800839 case android.SourceFileProducer:
840 switch tag {
841 case libTag:
842 checkProducesJars(ctx, dep)
843 deps.classpath = append(deps.classpath, dep.Srcs()...)
844 case staticLibTag:
845 checkProducesJars(ctx, dep)
846 deps.classpath = append(deps.classpath, dep.Srcs()...)
847 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
848 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
849 case android.DefaultsDepTag, android.SourceDepTag:
850 // Nothing to do
851 default:
852 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
853 }
854 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700855 switch tag {
856 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700857 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700858 case systemModulesTag:
859 if deps.systemModules != nil {
860 panic("Found two system module dependencies")
861 }
862 sm := module.(*SystemModules)
863 if sm.outputFile == nil {
864 panic("Missing directory for system module dependency")
865 }
866 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700867 default:
868 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700869 }
Colin Crossec7a0422017-07-07 14:47:12 -0700870 }
Colin Cross2fe66872015-03-30 17:20:39 -0700871 })
872
Jiyong Park1be96912018-05-28 18:02:19 +0900873 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
874
Colin Cross32f676a2017-09-06 13:41:06 -0700875 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700876}
877
Colin Cross83bb3162018-06-25 15:48:06 -0700878func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700879 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700880 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
881 if err != nil {
882 ctx.PropertyErrorf("sdk_version", "%s", err)
883 }
Nan Zhang357466b2018-04-17 17:38:36 -0700884 if javaVersion != "" {
885 ret = javaVersion
886 } else if ctx.Device() && sdk <= 23 {
887 ret = "1.7"
888 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
889 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700890 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700891 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
892 ret = "1.8"
893 } else {
894 ret = "1.9"
895 }
896
897 return ret
898}
899
Nan Zhanged19fc32017-10-19 13:06:22 -0700900func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700901
Colin Crossf03c82b2015-04-13 13:53:40 -0700902 var flags javaBuilderFlags
903
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100904 // javaVersion flag.
905 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
906
Nan Zhanged19fc32017-10-19 13:06:22 -0700907 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700908 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100909 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700910 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700911 }
Colin Cross6510f912017-11-29 00:27:14 -0800912 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700913 // Override the -g flag passed globally to remove local variable debug info to reduce
914 // disk and memory usage.
915 javacFlags = append(javacFlags, "-g:source,lines")
916 }
Colin Cross64162712017-08-08 13:17:59 -0700917
Colin Cross66548102018-06-19 22:47:35 -0700918 if ctx.Config().RunErrorProne() {
919 if config.ErrorProneClasspath == nil {
920 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
921 }
922
923 errorProneFlags := []string{
924 "-Xplugin:ErrorProne",
925 "${config.ErrorProneChecks}",
926 }
927 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
928
929 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
930 "'" + strings.Join(errorProneFlags, " ") + "'"
931 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800932 }
933
Nan Zhanged19fc32017-10-19 13:06:22 -0700934 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800935 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
936 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700937 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800938
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100939 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800940 !Bool(j.properties.No_standard_libs) &&
941 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
942 // Give host-side tools a version of OpenJDK's standard libraries
943 // close to what they're targeting. As of Dec 2017, AOSP is only
944 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
945 //
946 // When building with OpenJDK 8, the following should have no
947 // effect since those jars would be available by default.
948 //
949 // When building with OpenJDK 9 but targeting a version < 1.8,
950 // putting them on the bootclasspath means that:
951 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
952 // b) references to existing APIs are not reinterpreted in an
953 // OpenJDK 9-specific way, eg. calls to subclasses of
954 // java.nio.Buffer as in http://b/70862583
955 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
956 flags.bootClasspath = append(flags.bootClasspath,
957 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
958 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800959 if Bool(j.properties.Use_tools_jar) {
960 flags.bootClasspath = append(flags.bootClasspath,
961 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
962 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800963 }
964
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100965 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -0700966 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
967 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
968 }
969
Nan Zhanged19fc32017-10-19 13:06:22 -0700970 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700971 if deps.systemModules != nil {
972 flags.systemModules = append(flags.systemModules, deps.systemModules)
973 }
974
Nan Zhanged19fc32017-10-19 13:06:22 -0700975 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700976 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700977 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700978 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700979 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
980 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700981 }
982
Colin Cross81440082018-08-15 20:21:55 -0700983 if len(javacFlags) > 0 {
984 // optimization.
985 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
986 flags.javacFlags = "$javacFlags"
987 }
988
Nan Zhanged19fc32017-10-19 13:06:22 -0700989 return flags
990}
Colin Crossc0b06f12015-04-08 13:03:43 -0700991
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800992func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700993
Colin Crossebe1a512017-11-14 13:12:14 -0800994 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700995
996 deps := j.collectDeps(ctx)
997 flags := j.collectBuilderFlags(ctx, deps)
998
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100999 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001000 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1001 }
1002 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001003 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001004 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001005 }
1006
Colin Crossaf050172017-11-15 23:01:59 -08001007 srcFiles = j.genSources(ctx, srcFiles, flags)
1008
1009 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001010 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001011 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001012
Colin Cross1ee23172017-10-18 14:44:18 -07001013 jarName := ctx.ModuleName() + ".jar"
1014
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001015 javaSrcFiles := srcFiles.FilterByExt(".java")
1016 var uniqueSrcFiles android.Paths
1017 set := make(map[string]bool)
1018 for _, v := range javaSrcFiles {
1019 if _, found := set[v.String()]; !found {
1020 set[v.String()] = true
1021 uniqueSrcFiles = append(uniqueSrcFiles, v)
1022 }
1023 }
1024
Colin Cross55f63ea2018-08-27 12:37:09 -07001025 var kotlinJars android.Paths
1026
Colin Cross93e85952017-08-15 13:34:18 -07001027 if srcFiles.HasExt(".kt") {
1028 // If there are kotlin files, compile them first but pass all the kotlin and java files
1029 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1030 // won't emit any classes for them.
1031
1032 flags.kotlincFlags = "-no-stdlib"
1033 if ctx.Device() {
1034 flags.kotlincFlags += " -no-jdk"
1035 }
1036
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001037 var kotlinSrcFiles android.Paths
1038 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1039 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1040
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001041 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001042 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1043 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1044
Colin Cross1ee23172017-10-18 14:44:18 -07001045 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001046 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001047 if ctx.Failed() {
1048 return
1049 }
1050
1051 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001052 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001053 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001054
Colin Cross93e85952017-08-15 13:34:18 -07001055 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001056 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001057 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001058 }
1059
Colin Cross55f63ea2018-08-27 12:37:09 -07001060 jars := append(android.Paths(nil), kotlinJars...)
1061
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001062 // Store the list of .java files that was passed to javac
1063 j.compiledJavaSrcs = uniqueSrcFiles
1064 j.compiledSrcJars = srcJars
1065
Nan Zhang61eaedb2017-11-02 13:28:15 -07001066 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001067 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001068 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1069 enable_sharding = true
1070 if len(j.properties.Annotation_processors) != 0 ||
1071 len(j.properties.Annotation_processor_classes) != 0 {
1072 ctx.PropertyErrorf("javac_shard_size",
1073 "%q cannot be set when annotation processors are enabled.",
1074 j.properties.Javac_shard_size)
1075 }
1076 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001077 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001078 if ctx.Failed() {
1079 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001080 }
1081 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001082 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001083 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001084 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001085 // If error-prone is enabled, add an additional rule to compile the java files into
1086 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001087 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001088 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1089 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001090 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001091 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001092 extraJarDeps = append(extraJarDeps, errorprone)
1093 }
1094
Nan Zhang61eaedb2017-11-02 13:28:15 -07001095 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001096 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001097 shardSize := int(*(j.properties.Javac_shard_size))
1098 var shardSrcs []android.Paths
1099 if len(uniqueSrcFiles) > 0 {
1100 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1101 for idx, shardSrc := range shardSrcs {
1102 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1103 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1104 jars = append(jars, classes)
1105 }
1106 }
1107 if len(srcJars) > 0 {
1108 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1109 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1110 jars = append(jars, classes)
1111 }
1112 } else {
1113 classes := android.PathForModuleOut(ctx, "javac", jarName)
1114 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1115 jars = append(jars, classes)
1116 }
Colin Crossd6891432017-09-27 17:39:56 -07001117 if ctx.Failed() {
1118 return
1119 }
Colin Cross2fe66872015-03-30 17:20:39 -07001120 }
1121
Colin Cross0f37af02017-09-27 17:42:05 -07001122 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
1123 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1124
1125 var resArgs []string
1126 var resDeps android.Paths
1127
1128 resArgs = append(resArgs, dirArgs...)
1129 resDeps = append(resDeps, dirDeps...)
1130
1131 resArgs = append(resArgs, fileArgs...)
1132 resDeps = append(resDeps, fileDeps...)
1133
Colin Crossff3ae9d2018-04-10 16:15:18 -07001134 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001135 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001136 resArgs = append(resArgs, srcArgs...)
1137 resDeps = append(resDeps, srcDeps...)
1138 }
Colin Cross40a36712017-09-27 17:41:35 -07001139
1140 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001141 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001142 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001143 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001144 if ctx.Failed() {
1145 return
1146 }
1147 }
1148
Colin Cross331a1212018-08-15 20:40:52 -07001149 if len(deps.staticResourceJars) > 0 {
1150 var jars android.Paths
1151 if j.resourceJar != nil {
1152 jars = append(jars, j.resourceJar)
1153 }
1154 jars = append(jars, deps.staticResourceJars...)
1155
1156 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1157 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1158 false, nil, nil)
1159 j.resourceJar = combinedJar
1160 }
1161
Colin Cross32f676a2017-09-06 13:41:06 -07001162 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001163 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001164
Colin Cross366938f2017-12-11 16:29:02 -08001165 var manifest android.OptionalPath
1166 if j.properties.Manifest != nil {
1167 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1168 }
Colin Cross635acc92017-09-12 22:50:46 -07001169
Colin Cross0a6e0072017-08-30 14:24:55 -07001170 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001171 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001172 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001173
1174 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001175 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1176 // Optimization: skip the combine step if there is nothing to do
1177 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1178 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1179 // any if len(jars) == 1.
1180 outputFile = moduleOutPath
1181 } else {
1182 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1183 ctx.Build(pctx, android.BuildParams{
1184 Rule: android.Cp,
1185 Input: jars[0],
1186 Output: combinedJar,
1187 })
1188 outputFile = combinedJar
1189 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001190 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001191 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001192 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001193 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001194 outputFile = combinedJar
1195 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001196
Colin Cross331a1212018-08-15 20:40:52 -07001197 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001198 if j.properties.Jarjar_rules != nil {
1199 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001200 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001201 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001202 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1203 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001204
1205 // jarjar resource jar if necessary
1206 if j.resourceJar != nil {
1207 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1208 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1209 j.resourceJar = resourceJarJarFile
1210 }
1211
Colin Cross0a6e0072017-08-30 14:24:55 -07001212 if ctx.Failed() {
1213 return
1214 }
1215 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001216 j.implementationJarFile = outputFile
1217 if j.headerJarFile == nil {
1218 j.headerJarFile = j.implementationJarFile
1219 }
Colin Cross2fe66872015-03-30 17:20:39 -07001220
Colin Cross6510f912017-11-29 00:27:14 -08001221 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001222 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1223 j.properties.Instrument = true
1224 }
1225 }
1226
Colin Cross3144dfc2018-01-03 15:06:47 -08001227 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001228 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1229 }
1230
Colin Cross331a1212018-08-15 20:40:52 -07001231 // merge implementation jar with resources if necessary
1232 implementationAndResourcesJar := outputFile
1233 if j.resourceJar != nil {
1234 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1235 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1236 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1237 false, nil, nil)
1238 implementationAndResourcesJar = combinedJar
1239 }
1240
1241 j.implementationAndResourcesJar = implementationAndResourcesJar
1242
Colin Cross9ae1b922018-06-26 17:59:05 -07001243 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001244 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001245 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001246 if ctx.Failed() {
1247 return
1248 }
Colin Cross331a1212018-08-15 20:40:52 -07001249
1250 // merge dex jar with resources if necessary
1251 if j.resourceJar != nil {
1252 jars := android.Paths{dexOutputFile, j.resourceJar}
1253 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1254 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1255 false, nil, nil)
1256 dexOutputFile = combinedJar
1257 }
1258
1259 j.dexJarFile = dexOutputFile
1260
Colin Cross3063b782018-08-15 11:19:12 -07001261 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001262 } else {
1263 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001264 }
Colin Cross331a1212018-08-15 20:40:52 -07001265
Colin Crossb7a63242015-04-16 14:09:14 -07001266 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001267
1268 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1269 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001270}
1271
Colin Cross8eadbf02017-10-24 17:46:00 -07001272func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001273 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001274
1275 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001276 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001277 // Compile java sources into turbine.jar.
1278 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1279 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1280 if ctx.Failed() {
1281 return nil
1282 }
1283 jars = append(jars, turbineJar)
1284 }
1285
Colin Cross55f63ea2018-08-27 12:37:09 -07001286 jars = append(jars, extraJars...)
1287
Nan Zhanged19fc32017-10-19 13:06:22 -07001288 // Combine any static header libraries into classes-header.jar. If there is only
1289 // one input jar this step will be skipped.
1290 var headerJar android.Path
1291 jars = append(jars, deps.staticHeaderJars...)
1292
Colin Cross5c6ecc12017-10-23 18:12:27 -07001293 // we cannot skip the combine step for now if there is only one jar
1294 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1295 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001296 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1297 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001298 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001299
1300 if j.properties.Jarjar_rules != nil {
1301 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1302 // Transform classes.jar into classes-jarjar.jar
1303 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1304 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1305 headerJar = jarjarFile
1306 if ctx.Failed() {
1307 return nil
1308 }
1309 }
1310
1311 return headerJar
1312}
1313
Colin Crosscb933592017-11-22 13:49:43 -08001314func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001315 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001316
Colin Cross7a3139e2017-12-19 13:57:50 -08001317 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001318
Colin Cross84c38822018-01-03 15:59:46 -08001319 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001320 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1321
1322 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1323
1324 j.jacocoReportClassesFile = jacocoReportClassesFile
1325
1326 return instrumentedJar
1327}
1328
Colin Crossf506d872017-07-19 15:53:04 -07001329var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001330
Nan Zhanged19fc32017-10-19 13:06:22 -07001331func (j *Module) HeaderJars() android.Paths {
1332 return android.Paths{j.headerJarFile}
1333}
1334
1335func (j *Module) ImplementationJars() android.Paths {
1336 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001337}
1338
Colin Cross331a1212018-08-15 20:40:52 -07001339func (j *Module) ResourceJars() android.Paths {
1340 if j.resourceJar == nil {
1341 return nil
1342 }
1343 return android.Paths{j.resourceJar}
1344}
1345
1346func (j *Module) ImplementationAndResourcesJars() android.Paths {
1347 return android.Paths{j.implementationAndResourcesJar}
1348}
1349
Colin Cross46c9b8b2017-06-22 16:51:17 -07001350func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001351 return j.exportAidlIncludeDirs
1352}
1353
Jiyong Park1be96912018-05-28 18:02:19 +09001354func (j *Module) ExportedSdkLibs() []string {
1355 return j.exportedSdkLibs
1356}
1357
Colin Cross46c9b8b2017-06-22 16:51:17 -07001358var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001359
Colin Cross46c9b8b2017-06-22 16:51:17 -07001360func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001361 return j.logtagsSrcs
1362}
1363
Colin Cross2fe66872015-03-30 17:20:39 -07001364//
1365// Java libraries (.jar file)
1366//
1367
Colin Crossf506d872017-07-19 15:53:04 -07001368type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001369 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001370}
1371
Colin Crossf506d872017-07-19 15:53:04 -07001372func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001373 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001374
Colin Cross9ae1b922018-06-26 17:59:05 -07001375 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001376 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1377 ctx.ModuleName()+".jar", j.outputFile)
1378 }
Colin Crossb7a63242015-04-16 14:09:14 -07001379}
1380
Colin Crossf506d872017-07-19 15:53:04 -07001381func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001382 j.deps(ctx)
1383}
1384
Colin Cross9ae1b922018-06-26 17:59:05 -07001385func LibraryFactory() android.Module {
1386 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001387
Colin Cross9ae1b922018-06-26 17:59:05 -07001388 module.AddProperties(
1389 &module.Module.properties,
1390 &module.Module.deviceProperties,
1391 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001392
Colin Cross9ae1b922018-06-26 17:59:05 -07001393 InitJavaModule(module, android.HostAndDeviceSupported)
1394 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001395}
1396
Colin Crossf506d872017-07-19 15:53:04 -07001397func LibraryHostFactory() android.Module {
1398 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001399
Colin Cross6af17aa2017-09-20 12:59:05 -07001400 module.AddProperties(
1401 &module.Module.properties,
1402 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001403
Colin Cross9ae1b922018-06-26 17:59:05 -07001404 module.Module.properties.Installable = proptools.BoolPtr(true)
1405
Colin Cross89536d42017-07-07 14:35:50 -07001406 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001407 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001408}
1409
1410//
Colin Crossb628ea52018-08-14 16:42:33 -07001411// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001412//
1413
1414type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001415 // list of compatibility suites (for example "cts", "vts") that the module should be
1416 // installed into.
1417 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001418
1419 // the name of the test configuration (for example "AndroidTest.xml") that should be
1420 // installed with the module.
1421 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001422
1423 // list of files or filegroup modules that provide data that should be installed alongside
1424 // the test
1425 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001426}
1427
1428type Test struct {
1429 Library
1430
1431 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001432
1433 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001434 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001435}
1436
1437func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1438 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config)
Colin Crossd96ca352018-08-10 16:06:24 -07001439 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001440
1441 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001442}
1443
1444func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1445 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001446 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Colin Crossd96ca352018-08-10 16:06:24 -07001447 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001448}
1449
1450func TestFactory() android.Module {
1451 module := &Test{}
1452
1453 module.AddProperties(
1454 &module.Module.properties,
1455 &module.Module.deviceProperties,
1456 &module.Module.protoProperties,
1457 &module.testProperties)
1458
Colin Cross9ae1b922018-06-26 17:59:05 -07001459 module.Module.properties.Installable = proptools.BoolPtr(true)
1460
Colin Cross05638fc2018-04-09 18:40:24 -07001461 InitJavaModule(module, android.HostAndDeviceSupported)
1462 android.InitDefaultableModule(module)
1463 return module
1464}
1465
1466func TestHostFactory() android.Module {
1467 module := &Test{}
1468
1469 module.AddProperties(
1470 &module.Module.properties,
1471 &module.Module.protoProperties,
1472 &module.testProperties)
1473
Colin Cross9ae1b922018-06-26 17:59:05 -07001474 module.Module.properties.Installable = proptools.BoolPtr(true)
1475
Colin Cross05638fc2018-04-09 18:40:24 -07001476 InitJavaModule(module, android.HostSupported)
1477 android.InitDefaultableModule(module)
1478 return module
1479}
1480
1481//
Colin Cross2fe66872015-03-30 17:20:39 -07001482// Java Binaries (.jar file plus wrapper script)
1483//
1484
Colin Crossf506d872017-07-19 15:53:04 -07001485type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001486 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001487 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001488}
1489
Colin Crossf506d872017-07-19 15:53:04 -07001490type Binary struct {
1491 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001492
Colin Crossf506d872017-07-19 15:53:04 -07001493 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001494
Colin Cross6b4a32d2017-12-05 13:42:45 -08001495 isWrapperVariant bool
1496
Colin Crossc3315992017-12-08 19:12:36 -08001497 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001498 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001499}
1500
Alex Light24237172017-10-26 09:46:21 -07001501func (j *Binary) HostToolPath() android.OptionalPath {
1502 return android.OptionalPathForPath(j.binaryFile)
1503}
1504
Colin Crossf506d872017-07-19 15:53:04 -07001505func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001506 if ctx.Arch().ArchType == android.Common {
1507 // Compile the jar
1508 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001509 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001510 // Handle the binary wrapper
1511 j.isWrapperVariant = true
1512
Colin Cross366938f2017-12-11 16:29:02 -08001513 if j.binaryProperties.Wrapper != nil {
1514 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001515 } else {
1516 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1517 }
1518
1519 // Depend on the installed jar so that the wrapper doesn't get executed by
1520 // another build rule before the jar has been installed.
1521 jarFile := ctx.PrimaryModule().(*Binary).installFile
1522
1523 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1524 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001525 }
Colin Cross2fe66872015-03-30 17:20:39 -07001526}
1527
Colin Crossf506d872017-07-19 15:53:04 -07001528func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001529 if ctx.Arch().ArchType == android.Common {
1530 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001531 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001532 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001533 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001534}
1535
Colin Crossf506d872017-07-19 15:53:04 -07001536func BinaryFactory() android.Module {
1537 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001538
Colin Cross36242852017-06-23 15:06:31 -07001539 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001540 &module.Module.properties,
1541 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001542 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001543 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001544
Colin Cross9ae1b922018-06-26 17:59:05 -07001545 module.Module.properties.Installable = proptools.BoolPtr(true)
1546
Colin Cross6b4a32d2017-12-05 13:42:45 -08001547 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1548 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001549 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001550}
1551
Colin Crossf506d872017-07-19 15:53:04 -07001552func BinaryHostFactory() android.Module {
1553 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001554
Colin Cross36242852017-06-23 15:06:31 -07001555 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001556 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001557 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001558 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001559
Colin Cross9ae1b922018-06-26 17:59:05 -07001560 module.Module.properties.Installable = proptools.BoolPtr(true)
1561
Colin Cross6b4a32d2017-12-05 13:42:45 -08001562 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1563 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001564 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001565}
1566
1567//
1568// Java prebuilts
1569//
1570
Colin Cross74d73e22017-08-02 11:05:49 -07001571type ImportProperties struct {
1572 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001573
Nan Zhangea568a42017-11-08 21:20:04 -08001574 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001575
1576 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001577
1578 // List of shared java libs that this module has dependencies to
1579 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001580
1581 // List of files to remove from the jar file(s)
1582 Exclude_files []string
1583
1584 // List of directories to remove from the jar file(s)
1585 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001586
1587 // if set to true, run Jetifier against .jar file. Defaults to false.
1588 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001589}
1590
1591type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001592 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001593 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001594
Colin Cross74d73e22017-08-02 11:05:49 -07001595 properties ImportProperties
1596
Colin Cross0a6e0072017-08-30 14:24:55 -07001597 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001598 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001599}
1600
Colin Cross83bb3162018-06-25 15:48:06 -07001601func (j *Import) sdkVersion() string {
1602 return String(j.properties.Sdk_version)
1603}
1604
1605func (j *Import) minSdkVersion() string {
1606 return j.sdkVersion()
1607}
1608
Colin Cross74d73e22017-08-02 11:05:49 -07001609func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001610 return &j.prebuilt
1611}
1612
Colin Cross74d73e22017-08-02 11:05:49 -07001613func (j *Import) PrebuiltSrcs() []string {
1614 return j.properties.Jars
1615}
1616
1617func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001618 return j.prebuilt.Name(j.ModuleBase.Name())
1619}
1620
Colin Cross74d73e22017-08-02 11:05:49 -07001621func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001622 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001623 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001624}
1625
Colin Cross74d73e22017-08-02 11:05:49 -07001626func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001627 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001628
Nan Zhang4c819fb2018-08-27 18:31:46 -07001629 jarName := ctx.ModuleName() + ".jar"
1630 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001631 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1632 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001633 if Bool(j.properties.Jetifier_enabled) {
1634 inputFile := outputFile
1635 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1636 TransformJetifier(ctx, outputFile, inputFile)
1637 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001638 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001639
1640 ctx.VisitDirectDeps(func(module android.Module) {
1641 otherName := ctx.OtherModuleName(module)
1642 tag := ctx.OtherModuleDependencyTag(module)
1643
1644 switch dep := module.(type) {
1645 case Dependency:
1646 switch tag {
1647 case libTag, staticLibTag:
1648 // sdk lib names from dependencies are re-exported
1649 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1650 }
1651 case SdkLibraryDependency:
1652 switch tag {
1653 case libTag:
1654 // names of sdk libs that are directly depended are exported
1655 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1656 }
1657 }
1658 })
1659
1660 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001661 if Bool(j.properties.Installable) {
1662 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1663 ctx.ModuleName()+".jar", outputFile)
1664 }
Colin Cross2fe66872015-03-30 17:20:39 -07001665}
1666
Colin Cross74d73e22017-08-02 11:05:49 -07001667var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001668
Nan Zhanged19fc32017-10-19 13:06:22 -07001669func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001670 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001671}
1672
1673func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001674 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001675}
1676
Colin Cross331a1212018-08-15 20:40:52 -07001677func (j *Import) ResourceJars() android.Paths {
1678 return nil
1679}
1680
1681func (j *Import) ImplementationAndResourcesJars() android.Paths {
1682 return android.Paths{j.combinedClasspathFile}
1683}
1684
Colin Cross74d73e22017-08-02 11:05:49 -07001685func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001686 return nil
1687}
1688
Jiyong Park1be96912018-05-28 18:02:19 +09001689func (j *Import) ExportedSdkLibs() []string {
1690 return j.exportedSdkLibs
1691}
1692
Colin Cross74d73e22017-08-02 11:05:49 -07001693var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001694
Colin Cross74d73e22017-08-02 11:05:49 -07001695func ImportFactory() android.Module {
1696 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001697
Colin Cross74d73e22017-08-02 11:05:49 -07001698 module.AddProperties(&module.properties)
1699
1700 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001701 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1702 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001703}
1704
Colin Cross74d73e22017-08-02 11:05:49 -07001705func ImportFactoryHost() android.Module {
1706 module := &Import{}
1707
1708 module.AddProperties(&module.properties)
1709
1710 android.InitPrebuiltModule(module, &module.properties.Jars)
1711 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1712 return module
1713}
1714
Colin Cross89536d42017-07-07 14:35:50 -07001715//
1716// Defaults
1717//
1718type Defaults struct {
1719 android.ModuleBase
1720 android.DefaultsModuleBase
1721}
1722
1723func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1724}
1725
1726func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1727}
1728
1729func defaultsFactory() android.Module {
1730 return DefaultsFactory()
1731}
1732
1733func DefaultsFactory(props ...interface{}) android.Module {
1734 module := &Defaults{}
1735
1736 module.AddProperties(props...)
1737 module.AddProperties(
1738 &CompilerProperties{},
1739 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001740 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001741 )
1742
1743 android.InitDefaultsModule(module)
1744
1745 return module
1746}
Nan Zhangea568a42017-11-08 21:20:04 -08001747
1748var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001749var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001750var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001751var inList = android.InList