blob: 705704646e111faf780ac41ad878a8598c90a8a9 [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
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000089 // Use renamed kotlin stdlib (com.android.kotlin.*). This allows kotlin usage without colliding
90 // with app-provided kotlin stdlib.
91 Renamed_kotlin_stdlib *bool
92
Colin Cross7d5136f2015-05-11 13:39:40 -070093 // list of module-specific flags that will be used for javac compiles
94 Javacflags []string `android:"arch_variant"`
95
Colin Cross7d5136f2015-05-11 13:39:40 -070096 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070097 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070098
99 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700100 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700101
102 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700103 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700104
Colin Cross540eff82017-06-22 17:01:52 -0700105 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700106 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700107
108 // If not blank, set the java version passed to javac as -source and -target
109 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700110
Colin Cross9ae1b922018-06-26 17:59:05 -0700111 // If set to true, allow this module to be dexed and installed on devices. Has no
112 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700113 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700114
Colin Cross0f37af02017-09-27 17:42:05 -0700115 // If set to true, include sources used to compile the module in to the final jar
116 Include_srcs *bool
117
Colin Cross32f676a2017-09-06 13:41:06 -0700118 // List of modules to use as annotation processors
119 Annotation_processors []string
120
121 // List of classes to pass to javac to use as annotation processors
122 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700123
Nan Zhang61eaedb2017-11-02 13:28:15 -0700124 // The number of Java source entries each Javac instance can process
125 Javac_shard_size *int64
126
Nan Zhang5f8cb422018-02-06 10:34:32 -0800127 // Add host jdk tools.jar to bootclasspath
128 Use_tools_jar *bool
129
Colin Cross1369cdb2017-09-29 17:58:17 -0700130 Openjdk9 struct {
131 // List of source files that should only be used when passing -source 1.9
132 Srcs []string
133
134 // List of javac flags that should only be used when passing -source 1.9
135 Javacflags []string
136 }
Colin Crosscb933592017-11-22 13:49:43 -0800137
Colin Cross81440082018-08-15 20:21:55 -0700138 // When compiling language level 9+ .java code in packages that are part of
139 // a system module, patch_module names the module that your sources and
140 // dependencies should be patched into. The Android runtime currently
141 // doesn't implement the JEP 261 module system so this option is only
142 // supported at compile time. It should only be needed to compile tests in
143 // packages that exist in libcore and which are inconvenient to move
144 // elsewhere.
145 Patch_module *string
146
Colin Crosscb933592017-11-22 13:49:43 -0800147 Jacoco struct {
148 // List of classes to include for instrumentation with jacoco to collect coverage
149 // information at runtime when building with coverage enabled. If unset defaults to all
150 // classes.
151 // Supports '*' as the last character of an entry in the list as a wildcard match.
152 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
153 // it matches classes in the package that have the class name as a prefix.
154 Include_filter []string
155
156 // List of classes to exclude from instrumentation with jacoco to collect coverage
157 // information at runtime when building with coverage enabled. Overrides classes selected
158 // by the include_filter property.
159 // Supports '*' as the last character of an entry in the list as a wildcard match.
160 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
161 // it matches classes in the package that have the class name as a prefix.
162 Exclude_filter []string
163 }
164
Andreas Gampef3e5b552018-01-22 21:27:21 -0800165 Errorprone struct {
166 // List of javac flags that should only be used when running errorprone.
167 Javacflags []string
168 }
169
Colin Cross0f2ee152017-12-14 15:22:43 -0800170 Proto struct {
171 // List of extra options that will be passed to the proto generator.
172 Output_params []string
173 }
174
Colin Crosscb933592017-11-22 13:49:43 -0800175 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700176}
177
Colin Cross89536d42017-07-07 14:35:50 -0700178type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700179 // list of module-specific flags that will be used for dex compiles
180 Dxflags []string `android:"arch_variant"`
181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
183 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800184 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700185
Colin Cross83bb3162018-06-25 15:48:06 -0700186 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
187 // Defaults to sdk_version if not set.
188 Min_sdk_version *string
189
Colin Cross6af2e492018-05-22 11:12:33 -0700190 // if true, compile against the platform APIs instead of an SDK.
191 Platform_apis *bool
192
Colin Crossebe1a512017-11-14 13:12:14 -0800193 Aidl struct {
194 // Top level directories to pass to aidl tool
195 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700196
Colin Crossebe1a512017-11-14 13:12:14 -0800197 // Directories rooted at the Android.bp file to pass to aidl tool
198 Local_include_dirs []string
199
200 // directories that should be added as include directories for any aidl sources of modules
201 // that depend on this module, as well as to aidl for this module.
202 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100203
204 // whether to generate traces (for systrace) for this interface
205 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100206
207 // whether to generate Binder#GetTransaction name method.
208 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800209 }
Colin Cross92430102017-10-09 14:59:32 -0700210
211 // If true, export a copy of the module as a -hostdex module for host testing.
212 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700213
David Brazdil17ef5632018-06-27 10:27:45 +0100214 // If set to true, compile dex regardless of installable. Defaults to false.
215 Compile_dex *bool
216
Colin Cross1bd87802017-12-05 15:31:19 -0800217 Dex_preopt struct {
218 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
219 // true.
220 Enabled *bool
221
222 // If true, generate an app image (.art file) for this module.
223 App_image *bool
224
225 // If true, use a checked-in profile to guide optimization. Defaults to false unless
226 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
227 // that matches the name of this module, in which case it is defaulted to true.
228 Profile_guided *bool
229
230 // If set, provides the path to profile relative to the Android.bp file. If not set,
231 // defaults to searching for a file that matches the name of this module in the default
232 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
233 Profile *string
234 }
Colin Crossa22116e2017-10-19 14:18:58 -0700235
Colin Cross66dbc0b2017-12-28 12:23:20 -0800236 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700237 // If false, disable all optimization. Defaults to true for android_app and android_test
238 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800239 Enabled *bool
240
241 // If true, optimize for size by removing unused code. Defaults to true for apps,
242 // false for libraries and tests.
243 Shrink *bool
244
245 // If true, optimize bytecode. Defaults to false.
246 Optimize *bool
247
248 // If true, obfuscate bytecode. Defaults to false.
249 Obfuscate *bool
250
251 // If true, do not use the flag files generated by aapt that automatically keep
252 // classes referenced by the app manifest. Defaults to false.
253 No_aapt_flags *bool
254
255 // Flags to pass to proguard.
256 Proguard_flags []string
257
258 // Specifies the locations of files containing proguard flags.
259 Proguard_flags_files []string
260 }
261
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 // When targeting 1.9, override the modules to use with --system
263 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700264}
265
Colin Cross46c9b8b2017-06-22 16:51:17 -0700266// Module contains the properties and members used by all java module types
267type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700268 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700269 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700270
Colin Cross89536d42017-07-07 14:35:50 -0700271 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700272 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700273 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700274
Colin Cross331a1212018-08-15 20:40:52 -0700275 // jar file containing header classes including static library dependencies, suitable for
276 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 headerJarFile android.Path
278
Colin Cross331a1212018-08-15 20:40:52 -0700279 // jar file containing implementation classes including static library dependencies but no
280 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700281 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700282
Colin Cross331a1212018-08-15 20:40:52 -0700283 // jar file containing only resources including from static library dependencies
284 resourceJar android.Path
285
286 // jar file containing implementation classes and resources including static library
287 // dependencies
288 implementationAndResourcesJar android.Path
289
290 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700291 dexJarFile android.Path
292
Colin Crosscb933592017-11-22 13:49:43 -0800293 // output file containing uninstrumented classes that will be instrumented by jacoco
294 jacocoReportClassesFile android.Path
295
Colin Cross66dbc0b2017-12-28 12:23:20 -0800296 // output file containing mapping of obfuscated names
297 proguardDictionary android.Path
298
Colin Cross331a1212018-08-15 20:40:52 -0700299 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700300 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700301
Colin Cross635c3b02016-05-18 15:37:25 -0700302 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700303
Colin Cross635c3b02016-05-18 15:37:25 -0700304 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700305
Colin Cross2fe66872015-03-30 17:20:39 -0700306 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700307 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800308
309 // list of .java files and srcjars that was passed to javac
310 compiledJavaSrcs android.Paths
311 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800312
313 // list of extra progurad flag files
314 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900315
316 // list of SDK lib names that this java moudule is exporting
317 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700318}
319
Colin Cross54250902017-12-05 09:28:08 -0800320func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700321 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800322}
323
324var _ android.SourceFileProducer = (*Module)(nil)
325
Colin Crossf506d872017-07-19 15:53:04 -0700326type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700327 HeaderJars() android.Paths
328 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700329 ResourceJars() android.Paths
330 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700331 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900332 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700333}
334
Jiyong Parkc678ad32018-04-10 13:07:10 +0900335type SdkLibraryDependency interface {
336 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900337 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338}
339
Nan Zhangb2b33de2018-02-23 11:18:47 -0800340type SrcDependency interface {
341 CompiledSrcs() android.Paths
342 CompiledSrcJars() android.Paths
343}
344
345func (j *Module) CompiledSrcs() android.Paths {
346 return j.compiledJavaSrcs
347}
348
349func (j *Module) CompiledSrcJars() android.Paths {
350 return j.compiledSrcJars
351}
352
353var _ SrcDependency = (*Module)(nil)
354
Colin Cross89536d42017-07-07 14:35:50 -0700355func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
356 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
357 android.InitDefaultableModule(module)
358}
359
Colin Crossbe1da472017-07-07 15:59:46 -0700360type dependencyTag struct {
361 blueprint.BaseDependencyTag
362 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700363}
364
Colin Crossbe1da472017-07-07 15:59:46 -0700365var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700366 staticLibTag = dependencyTag{name: "staticlib"}
367 libTag = dependencyTag{name: "javalib"}
Colin Cross6a77c982018-06-19 22:43:34 -0700368 annoTag = dependencyTag{name: "annotation processor"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700369 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700370 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700371 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800372 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700373 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800374 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700375)
Colin Cross2fe66872015-03-30 17:20:39 -0700376
Colin Crossfc3674a2017-09-18 17:41:52 -0700377type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700378 useModule, useFiles, useDefaultLibs, invalidVersion bool
379
Colin Cross86a60ae2018-05-29 14:44:55 -0700380 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700381 systemModules string
382
Colin Crossa97c5d32018-03-28 14:58:31 -0700383 frameworkResModule string
384
Colin Cross86a60ae2018-05-29 14:44:55 -0700385 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700386 aidl android.Path
387}
388
Colin Cross3144dfc2018-01-03 15:06:47 -0800389func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
390 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
391}
392
393func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
394 return j.shouldInstrument(ctx) &&
395 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
396 ctx.Config().UnbundledBuild())
397}
398
Colin Cross83bb3162018-06-25 15:48:06 -0700399func (j *Module) sdkVersion() string {
400 return String(j.deviceProperties.Sdk_version)
401}
402
403func (j *Module) minSdkVersion() string {
404 if j.deviceProperties.Min_sdk_version != nil {
405 return *j.deviceProperties.Min_sdk_version
406 }
407 return j.sdkVersion()
408}
409
410type sdkContext interface {
411 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
412 sdkVersion() string
413 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
414 minSdkVersion() string
415}
416
417func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
418 switch v {
419 case "", "current", "system_current", "test_current", "core_current":
420 return ctx.Config().DefaultAppTargetSdk()
421 default:
422 return v
423 }
424}
425
426// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
427// it returns android.FutureApiLevel (10000).
428func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
429 switch v {
430 case "", "current", "test_current", "system_current", "core_current":
431 return ctx.Config().DefaultAppTargetSdkInt(), nil
432 default:
433 n := android.GetNumericSdkVersion(v)
434 if i, err := strconv.Atoi(n); err != nil {
435 return -1, fmt.Errorf("invalid sdk version %q", n)
436 } else {
437 return i, nil
438 }
439 }
440}
441
442func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
443 n, err := sdkVersionToNumber(ctx, v)
444 if err != nil {
445 return "", err
446 }
447 return strconv.Itoa(n), nil
448}
449
450func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
451 v := sdkContext.sdkVersion()
452 i, err := sdkVersionToNumber(ctx, v)
453 if err != nil {
454 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700455 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700456 }
457
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900458 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
459 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
460 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
461 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
462 if ctx.DeviceSpecific() || ctx.SocSpecific() {
463 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
464 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
465 }
466 }
467 version := strings.TrimPrefix(v, "system_")
468 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
469 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
470 v, allowed_versions)
471 }
472 }
473
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100474 toPrebuilt := func(sdk string) sdkDep {
475 var api, v string
476 if strings.Contains(sdk, "_") {
477 t := strings.Split(sdk, "_")
478 api = t[0]
479 v = t[1]
480 } else {
481 api = "public"
482 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900483 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100484 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700485 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100486 // There's no aidl for other SDKs yet.
487 // TODO(77525052): Add aidl files for other SDKs too.
488 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
489 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800490 jarPath := android.ExistentPathForSource(ctx, jar)
491 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700492 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700493
Colin Cross6510f912017-11-29 00:27:14 -0800494 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700495 return sdkDep{
496 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700497 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700498 }
499 }
500
Colin Crossfc3674a2017-09-18 17:41:52 -0700501 if !jarPath.Valid() {
502 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
503 return sdkDep{}
504 }
Colin Cross47ff2522017-10-02 14:22:08 -0700505
Colin Crossfc3674a2017-09-18 17:41:52 -0700506 if !aidlPath.Valid() {
507 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
508 return sdkDep{}
509 }
Colin Cross47ff2522017-10-02 14:22:08 -0700510
Colin Crossfc3674a2017-09-18 17:41:52 -0700511 return sdkDep{
512 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700513 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700514 aidl: aidlPath.Path(),
515 }
516 }
517
Colin Crossa97c5d32018-03-28 14:58:31 -0700518 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700519 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700520 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700521 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700522 systemModules: m + "_system_modules",
523 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700524 }
525 if m == "core.current.stubs" {
526 ret.systemModules = "core-system-modules"
527 }
528 return ret
529 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700530
Colin Cross6510f912017-11-29 00:27:14 -0800531 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100532 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700533 }
534
535 switch v {
536 case "":
537 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700538 useDefaultLibs: true,
539 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700540 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700541 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700542 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700543 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700544 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700545 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700546 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700547 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700548 return toModule("core.current.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 Crosscb2c9292017-09-23 19:57:16 -0700559 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800560 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700561 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
562 }
Colin Crossff3ae9d2018-04-10 16:15:18 -0700563 if !Bool(j.properties.No_framework_libs) {
Colin Crossfa5eb232017-10-01 20:33:03 -0700564 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
565 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700566 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800567 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700568 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
569 }
Colin Cross86a60ae2018-05-29 14:44:55 -0700570 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800571 if Bool(j.deviceProperties.Optimize.Enabled) {
572 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
573 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
574 }
Colin Crossbe1da472017-07-07 15:59:46 -0700575 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700576 } else if j.deviceProperties.System_modules == nil {
577 ctx.PropertyErrorf("no_standard_libs",
578 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
Colin Cross6510f912017-11-29 00:27:14 -0800579 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700580 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700581 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800582 if ctx.ModuleName() == "framework" {
583 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
584 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800585 if ctx.ModuleName() == "android_stubs_current" ||
586 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang16c0a312018-06-13 17:42:48 -0700587 ctx.ModuleName() == "android_test_stubs_current" ||
588 ctx.ModuleName() == "metalava_android_stubs_current" ||
589 ctx.ModuleName() == "metalava_android_system_stubs_current" ||
590 ctx.ModuleName() == "metalava_android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800591 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
592 }
Colin Cross2fe66872015-03-30 17:20:39 -0700593 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700594
Colin Crossf506d872017-07-19 15:53:04 -0700595 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
596 ctx.AddDependency(ctx.Module(), 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
612 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
613 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800614
615 if j.shouldInstrumentStatic(ctx) {
616 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
617 }
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 {
Nan Zhangec93cd92018-07-13 16:36:04 -0700718 case name == "core.current.stubs" || ver == "core_current" || noStdLibs || name == "stub-annotations" ||
719 name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900720 return javaCore
Nan Zhang16c0a312018-06-13 17:42:48 -0700721 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_") || name == "metalava_android_system_stubs_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900722 return javaSystem
Nan Zhang16c0a312018-06-13 17:42:48 -0700723 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_") || name == "metalava_android_test_stubs_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900724 return javaPlatform
Nan Zhang16c0a312018-06-13 17:42:48 -0700725 case name == "android_stubs_current" || ver == "current" || name == "metalava_android_stubs_current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700726 return javaSdk
727 case ver == "":
728 return javaPlatform
729 default:
730 if _, err := strconv.Atoi(ver); err != nil {
731 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
732 }
733 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900734 }
735}
736
Jiyong Park750e5572018-01-31 00:20:13 +0900737func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700738 if ctx.Host() {
739 return
740 }
741
742 myLinkType := getLinkType(from, ctx.ModuleName())
743 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900744 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."
745
746 switch myLinkType {
747 case javaCore:
748 if otherLinkType != javaCore {
749 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 +0900750 ctx.OtherModuleName(to))
751 }
Jiyong Park2d492942018-03-05 17:44:10 +0900752 break
753 case javaSdk:
754 if otherLinkType != javaCore && otherLinkType != javaSdk {
755 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
756 ctx.OtherModuleName(to))
757 }
758 break
759 case javaSystem:
760 if otherLinkType == javaPlatform {
761 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
762 ctx.OtherModuleName(to))
763 }
764 break
765 case javaPlatform:
766 // no restriction on link-type
767 break
Jiyong Park750e5572018-01-31 00:20:13 +0900768 }
769}
770
Colin Cross32f676a2017-09-06 13:41:06 -0700771func (j *Module) collectDeps(ctx android.ModuleContext) deps {
772 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700773
Colin Cross300f0382018-03-06 13:11:51 -0800774 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700775 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800776 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700777 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800778 } else if sdkDep.useFiles {
779 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700780 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800781 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
782 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700783 }
784
Colin Crossd11fcda2017-10-23 17:59:01 -0700785 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700786 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700787 tag := ctx.OtherModuleDependencyTag(module)
788
Jiyong Park750e5572018-01-31 00:20:13 +0900789 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700790 switch tag {
791 case bootClasspathTag, libTag, staticLibTag:
792 checkLinkType(ctx, j, to, tag.(dependencyTag))
793 }
Jiyong Park750e5572018-01-31 00:20:13 +0900794 }
Colin Cross54250902017-12-05 09:28:08 -0800795 switch dep := module.(type) {
796 case Dependency:
797 switch tag {
798 case bootClasspathTag:
799 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
800 case libTag:
801 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900802 // sdk lib names from dependencies are re-exported
803 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800804 case staticLibTag:
805 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
806 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
807 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700808 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900809 // sdk lib names from dependencies are re-exported
810 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700811 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700812 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800813 case frameworkResTag:
814 if ctx.ModuleName() == "framework" {
815 // framework.jar has a one-off dependency on the R.java and Manifest.java files
816 // generated by framework-res.apk
817 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
818 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800819 case frameworkApkTag:
820 if ctx.ModuleName() == "android_stubs_current" ||
821 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang16c0a312018-06-13 17:42:48 -0700822 ctx.ModuleName() == "android_test_stubs_current" ||
823 ctx.ModuleName() == "metalava_android_stubs_current" ||
824 ctx.ModuleName() == "metalava_android_system_stubs_current" ||
825 ctx.ModuleName() == "metalava_android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800826 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
827 // resource files out of there for aapt.
828 //
829 // Normally the package rule runs aapt, which includes the resource,
830 // but we're not running that in our package rule so just copy in the
831 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700832 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800833 }
Colin Cross54250902017-12-05 09:28:08 -0800834 case kotlinStdlibTag:
835 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800836 }
837
838 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900839 case SdkLibraryDependency:
840 switch tag {
841 case libTag:
842 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900843 // names of sdk libs that are directly depended are exported
844 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900845 default:
846 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
847 }
Colin Cross54250902017-12-05 09:28:08 -0800848 case android.SourceFileProducer:
849 switch tag {
850 case libTag:
851 checkProducesJars(ctx, dep)
852 deps.classpath = append(deps.classpath, dep.Srcs()...)
853 case staticLibTag:
854 checkProducesJars(ctx, dep)
855 deps.classpath = append(deps.classpath, dep.Srcs()...)
856 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
857 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
858 case android.DefaultsDepTag, android.SourceDepTag:
859 // Nothing to do
860 default:
861 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
862 }
863 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700864 switch tag {
865 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700866 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700867 case systemModulesTag:
868 if deps.systemModules != nil {
869 panic("Found two system module dependencies")
870 }
871 sm := module.(*SystemModules)
872 if sm.outputFile == nil {
873 panic("Missing directory for system module dependency")
874 }
875 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700876 default:
877 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700878 }
Colin Crossec7a0422017-07-07 14:47:12 -0700879 }
Colin Cross2fe66872015-03-30 17:20:39 -0700880 })
881
Jiyong Park1be96912018-05-28 18:02:19 +0900882 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
883
Colin Cross32f676a2017-09-06 13:41:06 -0700884 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700885}
886
Colin Cross83bb3162018-06-25 15:48:06 -0700887func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700888 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700889 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
890 if err != nil {
891 ctx.PropertyErrorf("sdk_version", "%s", err)
892 }
Nan Zhang357466b2018-04-17 17:38:36 -0700893 if javaVersion != "" {
894 ret = javaVersion
895 } else if ctx.Device() && sdk <= 23 {
896 ret = "1.7"
897 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
898 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700899 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700900 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
901 ret = "1.8"
902 } else {
903 ret = "1.9"
904 }
905
906 return ret
907}
908
Nan Zhanged19fc32017-10-19 13:06:22 -0700909func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700910
Colin Crossf03c82b2015-04-13 13:53:40 -0700911 var flags javaBuilderFlags
912
Nan Zhanged19fc32017-10-19 13:06:22 -0700913 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700914 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800915 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700916 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700917 }
Colin Cross6510f912017-11-29 00:27:14 -0800918 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700919 // Override the -g flag passed globally to remove local variable debug info to reduce
920 // disk and memory usage.
921 javacFlags = append(javacFlags, "-g:source,lines")
922 }
Colin Cross64162712017-08-08 13:17:59 -0700923
Colin Cross66548102018-06-19 22:47:35 -0700924 if ctx.Config().RunErrorProne() {
925 if config.ErrorProneClasspath == nil {
926 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
927 }
928
929 errorProneFlags := []string{
930 "-Xplugin:ErrorProne",
931 "${config.ErrorProneChecks}",
932 }
933 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
934
935 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
936 "'" + strings.Join(errorProneFlags, " ") + "'"
937 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800938 }
939
Nan Zhanged19fc32017-10-19 13:06:22 -0700940 // javaVersion flag.
Colin Cross83bb3162018-06-25 15:48:06 -0700941 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Colin Cross64162712017-08-08 13:17:59 -0700942
Nan Zhanged19fc32017-10-19 13:06:22 -0700943 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800944 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
945 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700946 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800947
948 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
949 !Bool(j.properties.No_standard_libs) &&
950 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
951 // Give host-side tools a version of OpenJDK's standard libraries
952 // close to what they're targeting. As of Dec 2017, AOSP is only
953 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
954 //
955 // When building with OpenJDK 8, the following should have no
956 // effect since those jars would be available by default.
957 //
958 // When building with OpenJDK 9 but targeting a version < 1.8,
959 // putting them on the bootclasspath means that:
960 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
961 // b) references to existing APIs are not reinterpreted in an
962 // OpenJDK 9-specific way, eg. calls to subclasses of
963 // java.nio.Buffer as in http://b/70862583
964 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
965 flags.bootClasspath = append(flags.bootClasspath,
966 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
967 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800968 if Bool(j.properties.Use_tools_jar) {
969 flags.bootClasspath = append(flags.bootClasspath,
970 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
971 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800972 }
973
Colin Cross81440082018-08-15 20:21:55 -0700974 if j.properties.Patch_module != nil && ctx.Config().TargetOpenJDK9() {
975 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
976 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
977 }
978
Nan Zhanged19fc32017-10-19 13:06:22 -0700979 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700980 if deps.systemModules != nil {
981 flags.systemModules = append(flags.systemModules, deps.systemModules)
982 }
983
Nan Zhanged19fc32017-10-19 13:06:22 -0700984 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700985 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700986 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700987 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700988 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
989 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700990 }
991
Colin Cross81440082018-08-15 20:21:55 -0700992 if len(javacFlags) > 0 {
993 // optimization.
994 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
995 flags.javacFlags = "$javacFlags"
996 }
997
Nan Zhanged19fc32017-10-19 13:06:22 -0700998 return flags
999}
Colin Crossc0b06f12015-04-08 13:03:43 -07001000
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001001func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001002
Colin Crossebe1a512017-11-14 13:12:14 -08001003 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001004
1005 deps := j.collectDeps(ctx)
1006 flags := j.collectBuilderFlags(ctx, deps)
1007
Colin Cross6510f912017-11-29 00:27:14 -08001008 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001009 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1010 }
1011 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001012 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001013 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001014 }
1015
Colin Crossaf050172017-11-15 23:01:59 -08001016 srcFiles = j.genSources(ctx, srcFiles, flags)
1017
1018 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001019 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001020 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001021
Colin Cross0a6e0072017-08-30 14:24:55 -07001022 var jars android.Paths
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 Cross37f6d792018-07-12 12:28:41 -07001036 var stripFiles []string
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"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001044
Colin Cross93e85952017-08-15 13:34:18 -07001045 if ctx.Device() {
1046 flags.kotlincFlags += " -no-jdk"
1047 }
1048
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001049 var kotlinSrcFiles android.Paths
1050 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1051 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1052
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001053 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001054 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1055 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1056
Colin Cross1ee23172017-10-18 14:44:18 -07001057 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001058 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001059 if ctx.Failed() {
1060 return
1061 }
1062
1063 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001064 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001065 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001066
Colin Cross93e85952017-08-15 13:34:18 -07001067 // Jar kotlin classes into the final jar after javac
1068 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001069
Colin Cross37f6d792018-07-12 12:28:41 -07001070 if Bool(j.properties.Renamed_kotlin_stdlib) {
1071 // Remove any kotlin-reflect related files
1072 // TODO(pszczepaniak): Support kotlin-reflect
Colin Cross4c03f682018-07-15 08:16:31 -07001073 stripFiles = append(stripFiles,
1074 "**/*.kotlin_module",
Colin Cross29788aa2018-07-13 21:23:59 -07001075 "**/*.kotlin_builtins")
Colin Cross37f6d792018-07-12 12:28:41 -07001076 } else {
1077 // Only add kotlin-stdlib if not using (on-device) renamed stdlib
1078 // (it's expected to be on device bootclasspath)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001079 jars = append(jars, deps.kotlinStdlib...)
1080 }
Colin Cross93e85952017-08-15 13:34:18 -07001081 }
1082
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001083 // Store the list of .java files that was passed to javac
1084 j.compiledJavaSrcs = uniqueSrcFiles
1085 j.compiledSrcJars = srcJars
1086
Nan Zhang61eaedb2017-11-02 13:28:15 -07001087 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001088 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001089 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1090 enable_sharding = true
1091 if len(j.properties.Annotation_processors) != 0 ||
1092 len(j.properties.Annotation_processor_classes) != 0 {
1093 ctx.PropertyErrorf("javac_shard_size",
1094 "%q cannot be set when annotation processors are enabled.",
1095 j.properties.Javac_shard_size)
1096 }
1097 }
Colin Crossf19b9bb2018-03-26 14:42:44 -07001098 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
1099 if ctx.Failed() {
1100 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001101 }
1102 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001103 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001104 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001105 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001106 // If error-prone is enabled, add an additional rule to compile the java files into
1107 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001108 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001109 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1110 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001111 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001112 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001113 extraJarDeps = append(extraJarDeps, errorprone)
1114 }
1115
Nan Zhang61eaedb2017-11-02 13:28:15 -07001116 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001117 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001118 shardSize := int(*(j.properties.Javac_shard_size))
1119 var shardSrcs []android.Paths
1120 if len(uniqueSrcFiles) > 0 {
1121 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1122 for idx, shardSrc := range shardSrcs {
1123 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1124 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1125 jars = append(jars, classes)
1126 }
1127 }
1128 if len(srcJars) > 0 {
1129 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1130 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1131 jars = append(jars, classes)
1132 }
1133 } else {
1134 classes := android.PathForModuleOut(ctx, "javac", jarName)
1135 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1136 jars = append(jars, classes)
1137 }
Colin Crossd6891432017-09-27 17:39:56 -07001138 if ctx.Failed() {
1139 return
1140 }
Colin Cross2fe66872015-03-30 17:20:39 -07001141 }
1142
Colin Cross0f37af02017-09-27 17:42:05 -07001143 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
1144 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1145
1146 var resArgs []string
1147 var resDeps android.Paths
1148
1149 resArgs = append(resArgs, dirArgs...)
1150 resDeps = append(resDeps, dirDeps...)
1151
1152 resArgs = append(resArgs, fileArgs...)
1153 resDeps = append(resDeps, fileDeps...)
1154
Colin Crossff3ae9d2018-04-10 16:15:18 -07001155 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001156 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001157 resArgs = append(resArgs, srcArgs...)
1158 resDeps = append(resDeps, srcDeps...)
1159 }
Colin Cross40a36712017-09-27 17:41:35 -07001160
1161 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001162 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001163 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001164 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001165 if ctx.Failed() {
1166 return
1167 }
1168 }
1169
Colin Cross331a1212018-08-15 20:40:52 -07001170 if len(deps.staticResourceJars) > 0 {
1171 var jars android.Paths
1172 if j.resourceJar != nil {
1173 jars = append(jars, j.resourceJar)
1174 }
1175 jars = append(jars, deps.staticResourceJars...)
1176
1177 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1178 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1179 false, nil, nil)
1180 j.resourceJar = combinedJar
1181 }
1182
Colin Cross32f676a2017-09-06 13:41:06 -07001183 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001184 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001185
Colin Cross366938f2017-12-11 16:29:02 -08001186 var manifest android.OptionalPath
1187 if j.properties.Manifest != nil {
1188 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1189 }
Colin Cross635acc92017-09-12 22:50:46 -07001190
Colin Cross0a6e0072017-08-30 14:24:55 -07001191 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001192 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001193 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001194
1195 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001196 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1197 // Optimization: skip the combine step if there is nothing to do
1198 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1199 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1200 // any if len(jars) == 1.
1201 outputFile = moduleOutPath
1202 } else {
1203 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1204 ctx.Build(pctx, android.BuildParams{
1205 Rule: android.Cp,
1206 Input: jars[0],
1207 Output: combinedJar,
1208 })
1209 outputFile = combinedJar
1210 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001211 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001212 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001213 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
1214 false, stripFiles, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001215 outputFile = combinedJar
1216 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001217
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001218 // Use renamed kotlin standard library?
Colin Crossff3ae9d2018-04-10 16:15:18 -07001219 if srcFiles.HasExt(".kt") && Bool(j.properties.Renamed_kotlin_stdlib) {
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001220 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
1221 TransformJarJar(ctx, jarjarFile, outputFile,
1222 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
1223 outputFile = jarjarFile
1224 if ctx.Failed() {
1225 return
1226 }
1227 }
1228
Colin Cross331a1212018-08-15 20:40:52 -07001229 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001230 if j.properties.Jarjar_rules != nil {
1231 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001232 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001233 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001234 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1235 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001236
1237 // jarjar resource jar if necessary
1238 if j.resourceJar != nil {
1239 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1240 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1241 j.resourceJar = resourceJarJarFile
1242 }
1243
Colin Cross0a6e0072017-08-30 14:24:55 -07001244 if ctx.Failed() {
1245 return
1246 }
1247 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001248 j.implementationJarFile = outputFile
1249 if j.headerJarFile == nil {
1250 j.headerJarFile = j.implementationJarFile
1251 }
Colin Cross2fe66872015-03-30 17:20:39 -07001252
Colin Cross6510f912017-11-29 00:27:14 -08001253 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001254 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1255 j.properties.Instrument = true
1256 }
1257 }
1258
Colin Cross3144dfc2018-01-03 15:06:47 -08001259 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001260 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1261 }
1262
Colin Cross331a1212018-08-15 20:40:52 -07001263 // merge implementation jar with resources if necessary
1264 implementationAndResourcesJar := outputFile
1265 if j.resourceJar != nil {
1266 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1267 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1268 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1269 false, nil, nil)
1270 implementationAndResourcesJar = combinedJar
1271 }
1272
1273 j.implementationAndResourcesJar = implementationAndResourcesJar
1274
Colin Cross9ae1b922018-06-26 17:59:05 -07001275 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001276 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001277 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001278 if ctx.Failed() {
1279 return
1280 }
Colin Cross331a1212018-08-15 20:40:52 -07001281
1282 // merge dex jar with resources if necessary
1283 if j.resourceJar != nil {
1284 jars := android.Paths{dexOutputFile, j.resourceJar}
1285 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1286 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1287 false, nil, nil)
1288 dexOutputFile = combinedJar
1289 }
1290
1291 j.dexJarFile = dexOutputFile
1292
Colin Cross3063b782018-08-15 11:19:12 -07001293 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001294 } else {
1295 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001296 }
Colin Cross331a1212018-08-15 20:40:52 -07001297
Colin Crossb7a63242015-04-16 14:09:14 -07001298 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001299
1300 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1301 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001302}
1303
Colin Cross8eadbf02017-10-24 17:46:00 -07001304func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001305 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1306
1307 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001308 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001309 // Compile java sources into turbine.jar.
1310 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1311 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1312 if ctx.Failed() {
1313 return nil
1314 }
1315 jars = append(jars, turbineJar)
1316 }
1317
1318 // Combine any static header libraries into classes-header.jar. If there is only
1319 // one input jar this step will be skipped.
1320 var headerJar android.Path
1321 jars = append(jars, deps.staticHeaderJars...)
1322
Colin Cross5c6ecc12017-10-23 18:12:27 -07001323 // we cannot skip the combine step for now if there is only one jar
1324 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1325 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001326 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1327 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001328 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001329
1330 if j.properties.Jarjar_rules != nil {
1331 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1332 // Transform classes.jar into classes-jarjar.jar
1333 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1334 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1335 headerJar = jarjarFile
1336 if ctx.Failed() {
1337 return nil
1338 }
1339 }
1340
1341 return headerJar
1342}
1343
Colin Crosscb933592017-11-22 13:49:43 -08001344func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001345 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001346
Colin Cross7a3139e2017-12-19 13:57:50 -08001347 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001348
Colin Cross84c38822018-01-03 15:59:46 -08001349 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001350 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1351
1352 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1353
1354 j.jacocoReportClassesFile = jacocoReportClassesFile
1355
1356 return instrumentedJar
1357}
1358
Colin Crossf506d872017-07-19 15:53:04 -07001359var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001360
Nan Zhanged19fc32017-10-19 13:06:22 -07001361func (j *Module) HeaderJars() android.Paths {
1362 return android.Paths{j.headerJarFile}
1363}
1364
1365func (j *Module) ImplementationJars() android.Paths {
1366 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001367}
1368
Colin Cross331a1212018-08-15 20:40:52 -07001369func (j *Module) ResourceJars() android.Paths {
1370 if j.resourceJar == nil {
1371 return nil
1372 }
1373 return android.Paths{j.resourceJar}
1374}
1375
1376func (j *Module) ImplementationAndResourcesJars() android.Paths {
1377 return android.Paths{j.implementationAndResourcesJar}
1378}
1379
Colin Cross46c9b8b2017-06-22 16:51:17 -07001380func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001381 return j.exportAidlIncludeDirs
1382}
1383
Jiyong Park1be96912018-05-28 18:02:19 +09001384func (j *Module) ExportedSdkLibs() []string {
1385 return j.exportedSdkLibs
1386}
1387
Colin Cross46c9b8b2017-06-22 16:51:17 -07001388var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001389
Colin Cross46c9b8b2017-06-22 16:51:17 -07001390func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001391 return j.logtagsSrcs
1392}
1393
Colin Cross2fe66872015-03-30 17:20:39 -07001394//
1395// Java libraries (.jar file)
1396//
1397
Colin Crossf506d872017-07-19 15:53:04 -07001398type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001399 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001400}
1401
Colin Crossf506d872017-07-19 15:53:04 -07001402func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001403 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001404
Colin Cross9ae1b922018-06-26 17:59:05 -07001405 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001406 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1407 ctx.ModuleName()+".jar", j.outputFile)
1408 }
Colin Crossb7a63242015-04-16 14:09:14 -07001409}
1410
Colin Crossf506d872017-07-19 15:53:04 -07001411func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001412 j.deps(ctx)
1413}
1414
Colin Cross9ae1b922018-06-26 17:59:05 -07001415func LibraryFactory() android.Module {
1416 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001417
Colin Cross9ae1b922018-06-26 17:59:05 -07001418 module.AddProperties(
1419 &module.Module.properties,
1420 &module.Module.deviceProperties,
1421 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001422
Colin Cross9ae1b922018-06-26 17:59:05 -07001423 InitJavaModule(module, android.HostAndDeviceSupported)
1424 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001425}
1426
Colin Crossf506d872017-07-19 15:53:04 -07001427func LibraryHostFactory() android.Module {
1428 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001429
Colin Cross6af17aa2017-09-20 12:59:05 -07001430 module.AddProperties(
1431 &module.Module.properties,
1432 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001433
Colin Cross9ae1b922018-06-26 17:59:05 -07001434 module.Module.properties.Installable = proptools.BoolPtr(true)
1435
Colin Cross89536d42017-07-07 14:35:50 -07001436 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001437 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001438}
1439
1440//
Colin Crossb628ea52018-08-14 16:42:33 -07001441// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001442//
1443
1444type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001445 // list of compatibility suites (for example "cts", "vts") that the module should be
1446 // installed into.
1447 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001448
1449 // the name of the test configuration (for example "AndroidTest.xml") that should be
1450 // installed with the module.
1451 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001452
1453 // list of files or filegroup modules that provide data that should be installed alongside
1454 // the test
1455 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001456}
1457
1458type Test struct {
1459 Library
1460
1461 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001462
1463 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001464 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001465}
1466
1467func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1468 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config)
Colin Crossd96ca352018-08-10 16:06:24 -07001469 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001470
1471 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001472}
1473
1474func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1475 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001476 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Colin Crossd96ca352018-08-10 16:06:24 -07001477 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001478}
1479
1480func TestFactory() android.Module {
1481 module := &Test{}
1482
1483 module.AddProperties(
1484 &module.Module.properties,
1485 &module.Module.deviceProperties,
1486 &module.Module.protoProperties,
1487 &module.testProperties)
1488
Colin Cross9ae1b922018-06-26 17:59:05 -07001489 module.Module.properties.Installable = proptools.BoolPtr(true)
1490
Colin Cross05638fc2018-04-09 18:40:24 -07001491 InitJavaModule(module, android.HostAndDeviceSupported)
1492 android.InitDefaultableModule(module)
1493 return module
1494}
1495
1496func TestHostFactory() android.Module {
1497 module := &Test{}
1498
1499 module.AddProperties(
1500 &module.Module.properties,
1501 &module.Module.protoProperties,
1502 &module.testProperties)
1503
Colin Cross9ae1b922018-06-26 17:59:05 -07001504 module.Module.properties.Installable = proptools.BoolPtr(true)
1505
Colin Cross05638fc2018-04-09 18:40:24 -07001506 InitJavaModule(module, android.HostSupported)
1507 android.InitDefaultableModule(module)
1508 return module
1509}
1510
1511//
Colin Cross2fe66872015-03-30 17:20:39 -07001512// Java Binaries (.jar file plus wrapper script)
1513//
1514
Colin Crossf506d872017-07-19 15:53:04 -07001515type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001516 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001517 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001518}
1519
Colin Crossf506d872017-07-19 15:53:04 -07001520type Binary struct {
1521 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001522
Colin Crossf506d872017-07-19 15:53:04 -07001523 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001524
Colin Cross6b4a32d2017-12-05 13:42:45 -08001525 isWrapperVariant bool
1526
Colin Crossc3315992017-12-08 19:12:36 -08001527 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001528 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001529}
1530
Alex Light24237172017-10-26 09:46:21 -07001531func (j *Binary) HostToolPath() android.OptionalPath {
1532 return android.OptionalPathForPath(j.binaryFile)
1533}
1534
Colin Crossf506d872017-07-19 15:53:04 -07001535func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001536 if ctx.Arch().ArchType == android.Common {
1537 // Compile the jar
1538 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001539 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001540 // Handle the binary wrapper
1541 j.isWrapperVariant = true
1542
Colin Cross366938f2017-12-11 16:29:02 -08001543 if j.binaryProperties.Wrapper != nil {
1544 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001545 } else {
1546 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1547 }
1548
1549 // Depend on the installed jar so that the wrapper doesn't get executed by
1550 // another build rule before the jar has been installed.
1551 jarFile := ctx.PrimaryModule().(*Binary).installFile
1552
1553 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1554 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001555 }
Colin Cross2fe66872015-03-30 17:20:39 -07001556}
1557
Colin Crossf506d872017-07-19 15:53:04 -07001558func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001559 if ctx.Arch().ArchType == android.Common {
1560 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001561 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001562 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001563 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001564}
1565
Colin Crossf506d872017-07-19 15:53:04 -07001566func BinaryFactory() android.Module {
1567 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001568
Colin Cross36242852017-06-23 15:06:31 -07001569 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001570 &module.Module.properties,
1571 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001572 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001573 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001574
Colin Cross9ae1b922018-06-26 17:59:05 -07001575 module.Module.properties.Installable = proptools.BoolPtr(true)
1576
Colin Cross6b4a32d2017-12-05 13:42:45 -08001577 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1578 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001579 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001580}
1581
Colin Crossf506d872017-07-19 15:53:04 -07001582func BinaryHostFactory() android.Module {
1583 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001584
Colin Cross36242852017-06-23 15:06:31 -07001585 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001586 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001587 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001588 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001589
Colin Cross9ae1b922018-06-26 17:59:05 -07001590 module.Module.properties.Installable = proptools.BoolPtr(true)
1591
Colin Cross6b4a32d2017-12-05 13:42:45 -08001592 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1593 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001594 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001595}
1596
1597//
1598// Java prebuilts
1599//
1600
Colin Cross74d73e22017-08-02 11:05:49 -07001601type ImportProperties struct {
1602 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001603
Nan Zhangea568a42017-11-08 21:20:04 -08001604 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001605
1606 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001607
1608 // List of shared java libs that this module has dependencies to
1609 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001610
1611 // List of files to remove from the jar file(s)
1612 Exclude_files []string
1613
1614 // List of directories to remove from the jar file(s)
1615 Exclude_dirs []string
Colin Cross74d73e22017-08-02 11:05:49 -07001616}
1617
1618type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001619 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001620 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001621
Colin Cross74d73e22017-08-02 11:05:49 -07001622 properties ImportProperties
1623
Colin Cross0a6e0072017-08-30 14:24:55 -07001624 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001625 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001626}
1627
Colin Cross83bb3162018-06-25 15:48:06 -07001628func (j *Import) sdkVersion() string {
1629 return String(j.properties.Sdk_version)
1630}
1631
1632func (j *Import) minSdkVersion() string {
1633 return j.sdkVersion()
1634}
1635
Colin Cross74d73e22017-08-02 11:05:49 -07001636func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001637 return &j.prebuilt
1638}
1639
Colin Cross74d73e22017-08-02 11:05:49 -07001640func (j *Import) PrebuiltSrcs() []string {
1641 return j.properties.Jars
1642}
1643
1644func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001645 return j.prebuilt.Name(j.ModuleBase.Name())
1646}
1647
Colin Cross74d73e22017-08-02 11:05:49 -07001648func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001649 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Jiyong Park1be96912018-05-28 18:02:19 +09001650 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001651}
1652
Colin Cross74d73e22017-08-02 11:05:49 -07001653func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001654 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001655
Colin Crosse9a275b2017-10-16 17:09:48 -07001656 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Colin Cross37f6d792018-07-12 12:28:41 -07001657 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1658 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Crosse9a275b2017-10-16 17:09:48 -07001659 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001660
1661 ctx.VisitDirectDeps(func(module android.Module) {
1662 otherName := ctx.OtherModuleName(module)
1663 tag := ctx.OtherModuleDependencyTag(module)
1664
1665 switch dep := module.(type) {
1666 case Dependency:
1667 switch tag {
1668 case libTag, staticLibTag:
1669 // sdk lib names from dependencies are re-exported
1670 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1671 }
1672 case SdkLibraryDependency:
1673 switch tag {
1674 case libTag:
1675 // names of sdk libs that are directly depended are exported
1676 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1677 }
1678 }
1679 })
1680
1681 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001682 if Bool(j.properties.Installable) {
1683 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1684 ctx.ModuleName()+".jar", outputFile)
1685 }
Colin Cross2fe66872015-03-30 17:20:39 -07001686}
1687
Colin Cross74d73e22017-08-02 11:05:49 -07001688var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001689
Nan Zhanged19fc32017-10-19 13:06:22 -07001690func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001691 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001692}
1693
1694func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001695 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001696}
1697
Colin Cross331a1212018-08-15 20:40:52 -07001698func (j *Import) ResourceJars() android.Paths {
1699 return nil
1700}
1701
1702func (j *Import) ImplementationAndResourcesJars() android.Paths {
1703 return android.Paths{j.combinedClasspathFile}
1704}
1705
Colin Cross74d73e22017-08-02 11:05:49 -07001706func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001707 return nil
1708}
1709
Jiyong Park1be96912018-05-28 18:02:19 +09001710func (j *Import) ExportedSdkLibs() []string {
1711 return j.exportedSdkLibs
1712}
1713
Colin Cross74d73e22017-08-02 11:05:49 -07001714var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001715
Colin Cross74d73e22017-08-02 11:05:49 -07001716func ImportFactory() android.Module {
1717 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001718
Colin Cross74d73e22017-08-02 11:05:49 -07001719 module.AddProperties(&module.properties)
1720
1721 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001722 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1723 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001724}
1725
Colin Cross74d73e22017-08-02 11:05:49 -07001726func ImportFactoryHost() android.Module {
1727 module := &Import{}
1728
1729 module.AddProperties(&module.properties)
1730
1731 android.InitPrebuiltModule(module, &module.properties.Jars)
1732 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1733 return module
1734}
1735
Colin Cross89536d42017-07-07 14:35:50 -07001736//
1737// Defaults
1738//
1739type Defaults struct {
1740 android.ModuleBase
1741 android.DefaultsModuleBase
1742}
1743
1744func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1745}
1746
1747func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1748}
1749
1750func defaultsFactory() android.Module {
1751 return DefaultsFactory()
1752}
1753
1754func DefaultsFactory(props ...interface{}) android.Module {
1755 module := &Defaults{}
1756
1757 module.AddProperties(props...)
1758 module.AddProperties(
1759 &CompilerProperties{},
1760 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001761 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001762 )
1763
1764 android.InitDefaultsModule(module)
1765
1766 return module
1767}
Nan Zhangea568a42017-11-08 21:20:04 -08001768
1769var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001770var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001771var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001772var inList = android.InList