blob: 43af2c6b6b21802bf735b45eb580c93b36fdc748 [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
138 Jacoco struct {
139 // List of classes to include for instrumentation with jacoco to collect coverage
140 // information at runtime when building with coverage enabled. If unset defaults to all
141 // classes.
142 // Supports '*' as the last character of an entry in the list as a wildcard match.
143 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
144 // it matches classes in the package that have the class name as a prefix.
145 Include_filter []string
146
147 // List of classes to exclude from instrumentation with jacoco to collect coverage
148 // information at runtime when building with coverage enabled. Overrides classes selected
149 // by the include_filter property.
150 // Supports '*' as the last character of an entry in the list as a wildcard match.
151 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
152 // it matches classes in the package that have the class name as a prefix.
153 Exclude_filter []string
154 }
155
Andreas Gampef3e5b552018-01-22 21:27:21 -0800156 Errorprone struct {
157 // List of javac flags that should only be used when running errorprone.
158 Javacflags []string
159 }
160
Colin Cross0f2ee152017-12-14 15:22:43 -0800161 Proto struct {
162 // List of extra options that will be passed to the proto generator.
163 Output_params []string
164 }
165
Colin Crosscb933592017-11-22 13:49:43 -0800166 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700167}
168
Colin Cross89536d42017-07-07 14:35:50 -0700169type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700170 // list of module-specific flags that will be used for dex compiles
171 Dxflags []string `android:"arch_variant"`
172
Colin Cross83bb3162018-06-25 15:48:06 -0700173 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
174 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800175 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700176
Colin Cross83bb3162018-06-25 15:48:06 -0700177 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
178 // Defaults to sdk_version if not set.
179 Min_sdk_version *string
180
Colin Cross6af2e492018-05-22 11:12:33 -0700181 // if true, compile against the platform APIs instead of an SDK.
182 Platform_apis *bool
183
Colin Crossebe1a512017-11-14 13:12:14 -0800184 Aidl struct {
185 // Top level directories to pass to aidl tool
186 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700187
Colin Crossebe1a512017-11-14 13:12:14 -0800188 // Directories rooted at the Android.bp file to pass to aidl tool
189 Local_include_dirs []string
190
191 // directories that should be added as include directories for any aidl sources of modules
192 // that depend on this module, as well as to aidl for this module.
193 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100194
195 // whether to generate traces (for systrace) for this interface
196 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100197
198 // whether to generate Binder#GetTransaction name method.
199 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800200 }
Colin Cross92430102017-10-09 14:59:32 -0700201
202 // If true, export a copy of the module as a -hostdex module for host testing.
203 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700204
David Brazdil17ef5632018-06-27 10:27:45 +0100205 // If set to true, compile dex regardless of installable. Defaults to false.
206 Compile_dex *bool
207
Colin Cross1bd87802017-12-05 15:31:19 -0800208 Dex_preopt struct {
209 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
210 // true.
211 Enabled *bool
212
213 // If true, generate an app image (.art file) for this module.
214 App_image *bool
215
216 // If true, use a checked-in profile to guide optimization. Defaults to false unless
217 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
218 // that matches the name of this module, in which case it is defaulted to true.
219 Profile_guided *bool
220
221 // If set, provides the path to profile relative to the Android.bp file. If not set,
222 // defaults to searching for a file that matches the name of this module in the default
223 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
224 Profile *string
225 }
Colin Crossa22116e2017-10-19 14:18:58 -0700226
Colin Cross66dbc0b2017-12-28 12:23:20 -0800227 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700228 // If false, disable all optimization. Defaults to true for android_app and android_test
229 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800230 Enabled *bool
231
232 // If true, optimize for size by removing unused code. Defaults to true for apps,
233 // false for libraries and tests.
234 Shrink *bool
235
236 // If true, optimize bytecode. Defaults to false.
237 Optimize *bool
238
239 // If true, obfuscate bytecode. Defaults to false.
240 Obfuscate *bool
241
242 // If true, do not use the flag files generated by aapt that automatically keep
243 // classes referenced by the app manifest. Defaults to false.
244 No_aapt_flags *bool
245
246 // Flags to pass to proguard.
247 Proguard_flags []string
248
249 // Specifies the locations of files containing proguard flags.
250 Proguard_flags_files []string
251 }
252
Colin Cross1369cdb2017-09-29 17:58:17 -0700253 // When targeting 1.9, override the modules to use with --system
254 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700255}
256
Colin Cross46c9b8b2017-06-22 16:51:17 -0700257// Module contains the properties and members used by all java module types
258type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700259 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700260 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700261
Colin Cross89536d42017-07-07 14:35:50 -0700262 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700263 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700264 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700265
Nan Zhanged19fc32017-10-19 13:06:22 -0700266 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
267 headerJarFile android.Path
268
269 // full implementation jar file suitable for static dependency of another module compile
270 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700271
Colin Cross6ade34f2017-09-15 13:00:47 -0700272 // output file containing classes.dex
273 dexJarFile android.Path
274
Colin Crosscb933592017-11-22 13:49:43 -0800275 // output file containing uninstrumented classes that will be instrumented by jacoco
276 jacocoReportClassesFile android.Path
277
Colin Cross66dbc0b2017-12-28 12:23:20 -0800278 // output file containing mapping of obfuscated names
279 proguardDictionary android.Path
280
Colin Crossb7a63242015-04-16 14:09:14 -0700281 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700282 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700283
Colin Cross635c3b02016-05-18 15:37:25 -0700284 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700285
Colin Cross635c3b02016-05-18 15:37:25 -0700286 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700287
Colin Cross2fe66872015-03-30 17:20:39 -0700288 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700289 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800290
291 // list of .java files and srcjars that was passed to javac
292 compiledJavaSrcs android.Paths
293 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800294
295 // list of extra progurad flag files
296 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900297
298 // list of SDK lib names that this java moudule is exporting
299 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700300}
301
Colin Cross54250902017-12-05 09:28:08 -0800302func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700303 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800304}
305
306var _ android.SourceFileProducer = (*Module)(nil)
307
Colin Crossf506d872017-07-19 15:53:04 -0700308type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700309 HeaderJars() android.Paths
310 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700311 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900312 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700313}
314
Jiyong Parkc678ad32018-04-10 13:07:10 +0900315type SdkLibraryDependency interface {
316 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900317 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900318}
319
Nan Zhangb2b33de2018-02-23 11:18:47 -0800320type SrcDependency interface {
321 CompiledSrcs() android.Paths
322 CompiledSrcJars() android.Paths
323}
324
325func (j *Module) CompiledSrcs() android.Paths {
326 return j.compiledJavaSrcs
327}
328
329func (j *Module) CompiledSrcJars() android.Paths {
330 return j.compiledSrcJars
331}
332
333var _ SrcDependency = (*Module)(nil)
334
Colin Cross89536d42017-07-07 14:35:50 -0700335func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
336 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
337 android.InitDefaultableModule(module)
338}
339
Colin Crossbe1da472017-07-07 15:59:46 -0700340type dependencyTag struct {
341 blueprint.BaseDependencyTag
342 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700343}
344
Colin Crossbe1da472017-07-07 15:59:46 -0700345var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700346 staticLibTag = dependencyTag{name: "staticlib"}
347 libTag = dependencyTag{name: "javalib"}
Colin Cross6a77c982018-06-19 22:43:34 -0700348 annoTag = dependencyTag{name: "annotation processor"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700349 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700350 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700351 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800352 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700353 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800354 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700355)
Colin Cross2fe66872015-03-30 17:20:39 -0700356
Colin Crossfc3674a2017-09-18 17:41:52 -0700357type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700358 useModule, useFiles, useDefaultLibs, invalidVersion bool
359
Colin Cross86a60ae2018-05-29 14:44:55 -0700360 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700361 systemModules string
362
Colin Crossa97c5d32018-03-28 14:58:31 -0700363 frameworkResModule string
364
Colin Cross86a60ae2018-05-29 14:44:55 -0700365 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700366 aidl android.Path
367}
368
Colin Cross3144dfc2018-01-03 15:06:47 -0800369func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
370 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
371}
372
373func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
374 return j.shouldInstrument(ctx) &&
375 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
376 ctx.Config().UnbundledBuild())
377}
378
Colin Cross83bb3162018-06-25 15:48:06 -0700379func (j *Module) sdkVersion() string {
380 return String(j.deviceProperties.Sdk_version)
381}
382
383func (j *Module) minSdkVersion() string {
384 if j.deviceProperties.Min_sdk_version != nil {
385 return *j.deviceProperties.Min_sdk_version
386 }
387 return j.sdkVersion()
388}
389
390type sdkContext interface {
391 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
392 sdkVersion() string
393 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
394 minSdkVersion() string
395}
396
397func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
398 switch v {
399 case "", "current", "system_current", "test_current", "core_current":
400 return ctx.Config().DefaultAppTargetSdk()
401 default:
402 return v
403 }
404}
405
406// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
407// it returns android.FutureApiLevel (10000).
408func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
409 switch v {
410 case "", "current", "test_current", "system_current", "core_current":
411 return ctx.Config().DefaultAppTargetSdkInt(), nil
412 default:
413 n := android.GetNumericSdkVersion(v)
414 if i, err := strconv.Atoi(n); err != nil {
415 return -1, fmt.Errorf("invalid sdk version %q", n)
416 } else {
417 return i, nil
418 }
419 }
420}
421
422func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
423 n, err := sdkVersionToNumber(ctx, v)
424 if err != nil {
425 return "", err
426 }
427 return strconv.Itoa(n), nil
428}
429
430func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
431 v := sdkContext.sdkVersion()
432 i, err := sdkVersionToNumber(ctx, v)
433 if err != nil {
434 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700435 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700436 }
437
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900438 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
439 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
440 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
441 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
442 if ctx.DeviceSpecific() || ctx.SocSpecific() {
443 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
444 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
445 }
446 }
447 version := strings.TrimPrefix(v, "system_")
448 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
449 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
450 v, allowed_versions)
451 }
452 }
453
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100454 toPrebuilt := func(sdk string) sdkDep {
455 var api, v string
456 if strings.Contains(sdk, "_") {
457 t := strings.Split(sdk, "_")
458 api = t[0]
459 v = t[1]
460 } else {
461 api = "public"
462 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900463 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100464 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700465 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100466 // There's no aidl for other SDKs yet.
467 // TODO(77525052): Add aidl files for other SDKs too.
468 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
469 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800470 jarPath := android.ExistentPathForSource(ctx, jar)
471 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700472 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700473
Colin Cross6510f912017-11-29 00:27:14 -0800474 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700475 return sdkDep{
476 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700477 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700478 }
479 }
480
Colin Crossfc3674a2017-09-18 17:41:52 -0700481 if !jarPath.Valid() {
482 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
483 return sdkDep{}
484 }
Colin Cross47ff2522017-10-02 14:22:08 -0700485
Colin Crossfc3674a2017-09-18 17:41:52 -0700486 if !aidlPath.Valid() {
487 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
488 return sdkDep{}
489 }
Colin Cross47ff2522017-10-02 14:22:08 -0700490
Colin Crossfc3674a2017-09-18 17:41:52 -0700491 return sdkDep{
492 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700493 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700494 aidl: aidlPath.Path(),
495 }
496 }
497
Colin Crossa97c5d32018-03-28 14:58:31 -0700498 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700499 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700500 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700501 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700502 systemModules: m + "_system_modules",
503 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700504 }
505 if m == "core.current.stubs" {
506 ret.systemModules = "core-system-modules"
507 }
508 return ret
509 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700510
Colin Cross6510f912017-11-29 00:27:14 -0800511 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100512 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700513 }
514
515 switch v {
516 case "":
517 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700518 useDefaultLibs: true,
519 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700520 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700521 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700522 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700523 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700524 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700525 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700526 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700527 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700528 return toModule("core.current.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700529 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100530 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700531 }
532}
533
Colin Crossbe1da472017-07-07 15:59:46 -0700534func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700535 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700536 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700537 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700538 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700539 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800540 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700541 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
542 }
Colin Crossff3ae9d2018-04-10 16:15:18 -0700543 if !Bool(j.properties.No_framework_libs) {
Colin Crossfa5eb232017-10-01 20:33:03 -0700544 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
545 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700546 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800547 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700548 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
549 }
Colin Cross86a60ae2018-05-29 14:44:55 -0700550 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800551 if Bool(j.deviceProperties.Optimize.Enabled) {
552 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
553 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
554 }
Colin Crossbe1da472017-07-07 15:59:46 -0700555 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700556 } else if j.deviceProperties.System_modules == nil {
557 ctx.PropertyErrorf("no_standard_libs",
558 "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 -0800559 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700560 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700561 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800562 if ctx.ModuleName() == "framework" {
563 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
564 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800565 if ctx.ModuleName() == "android_stubs_current" ||
566 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang16c0a312018-06-13 17:42:48 -0700567 ctx.ModuleName() == "android_test_stubs_current" ||
568 ctx.ModuleName() == "metalava_android_stubs_current" ||
569 ctx.ModuleName() == "metalava_android_system_stubs_current" ||
570 ctx.ModuleName() == "metalava_android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800571 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
572 }
Colin Cross2fe66872015-03-30 17:20:39 -0700573 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700574
Colin Crossf506d872017-07-19 15:53:04 -0700575 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
576 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100577 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700578 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100579 }, annoTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700580 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000581 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700582 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800583 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700584
585 if j.hasSrcExt(".proto") {
586 protoDeps(ctx, &j.protoProperties)
587 }
Colin Cross93e85952017-08-15 13:34:18 -0700588
589 if j.hasSrcExt(".kt") {
590 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
591 // Kotlin files
592 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
593 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800594
595 if j.shouldInstrumentStatic(ctx) {
596 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
597 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700598}
599
600func hasSrcExt(srcs []string, ext string) bool {
601 for _, src := range srcs {
602 if filepath.Ext(src) == ext {
603 return true
604 }
605 }
606
607 return false
608}
609
Nan Zhang61eaedb2017-11-02 13:28:15 -0700610func shardPaths(paths android.Paths, shardSize int) []android.Paths {
611 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
612 for len(paths) > shardSize {
613 ret = append(ret, paths[0:shardSize])
614 paths = paths[shardSize:]
615 }
616 if len(paths) > 0 {
617 ret = append(ret, paths)
618 }
619 return ret
620}
621
Colin Cross6af17aa2017-09-20 12:59:05 -0700622func (j *Module) hasSrcExt(ext string) bool {
623 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700624}
625
Colin Cross46c9b8b2017-06-22 16:51:17 -0700626func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700627 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700628
Colin Crossebe1a512017-11-14 13:12:14 -0800629 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
630 aidlIncludes = append(aidlIncludes,
631 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
632 aidlIncludes = append(aidlIncludes,
633 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700634
Steven Moreland667f6882018-07-26 12:55:08 -0700635 flags := []string{"-b"}
636
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700637 if aidlPreprocess.Valid() {
638 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700639 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700640 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700641 }
642
Colin Cross635c3b02016-05-18 15:37:25 -0700643 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800644 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700645 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800646 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700647 flags = append(flags, "-I"+src.String())
648 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700649
Martijn Coeneneab15642018-03-09 09:29:59 +0100650 if Bool(j.deviceProperties.Aidl.Generate_traces) {
651 flags = append(flags, "-t")
652 }
653
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100654 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
655 flags = append(flags, "--transaction_names")
656 }
657
Colin Crossf03c82b2015-04-13 13:53:40 -0700658 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700659}
660
Colin Cross32f676a2017-09-06 13:41:06 -0700661type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800662 classpath classpath
663 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700664 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700665 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700666 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700667 staticJarResources android.Paths
668 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800669 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700670 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700671 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700672 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700673 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700674}
Colin Cross2fe66872015-03-30 17:20:39 -0700675
Colin Cross54250902017-12-05 09:28:08 -0800676func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
677 for _, f := range dep.Srcs() {
678 if f.Ext() != ".jar" {
679 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
680 ctx.OtherModuleName(dep.(blueprint.Module)))
681 }
682 }
683}
684
Jiyong Park2d492942018-03-05 17:44:10 +0900685type linkType int
686
687const (
688 javaCore linkType = iota
689 javaSdk
690 javaSystem
691 javaPlatform
692)
693
Colin Crossf19b9bb2018-03-26 14:42:44 -0700694func getLinkType(m *Module, name string) linkType {
Colin Cross83bb3162018-06-25 15:48:06 -0700695 ver := m.sdkVersion()
Colin Cross86a60ae2018-05-29 14:44:55 -0700696 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700697 switch {
Nan Zhangec93cd92018-07-13 16:36:04 -0700698 case name == "core.current.stubs" || ver == "core_current" || noStdLibs || name == "stub-annotations" ||
699 name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900700 return javaCore
Nan Zhang16c0a312018-06-13 17:42:48 -0700701 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_") || name == "metalava_android_system_stubs_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900702 return javaSystem
Nan Zhang16c0a312018-06-13 17:42:48 -0700703 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_") || name == "metalava_android_test_stubs_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900704 return javaPlatform
Nan Zhang16c0a312018-06-13 17:42:48 -0700705 case name == "android_stubs_current" || ver == "current" || name == "metalava_android_stubs_current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700706 return javaSdk
707 case ver == "":
708 return javaPlatform
709 default:
710 if _, err := strconv.Atoi(ver); err != nil {
711 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
712 }
713 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900714 }
715}
716
Jiyong Park750e5572018-01-31 00:20:13 +0900717func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700718 if ctx.Host() {
719 return
720 }
721
722 myLinkType := getLinkType(from, ctx.ModuleName())
723 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900724 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."
725
726 switch myLinkType {
727 case javaCore:
728 if otherLinkType != javaCore {
729 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 +0900730 ctx.OtherModuleName(to))
731 }
Jiyong Park2d492942018-03-05 17:44:10 +0900732 break
733 case javaSdk:
734 if otherLinkType != javaCore && otherLinkType != javaSdk {
735 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
736 ctx.OtherModuleName(to))
737 }
738 break
739 case javaSystem:
740 if otherLinkType == javaPlatform {
741 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
742 ctx.OtherModuleName(to))
743 }
744 break
745 case javaPlatform:
746 // no restriction on link-type
747 break
Jiyong Park750e5572018-01-31 00:20:13 +0900748 }
749}
750
Colin Cross32f676a2017-09-06 13:41:06 -0700751func (j *Module) collectDeps(ctx android.ModuleContext) deps {
752 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700753
Colin Cross300f0382018-03-06 13:11:51 -0800754 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700755 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800756 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700757 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800758 } else if sdkDep.useFiles {
759 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700760 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800761 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
762 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700763 }
764
Colin Crossd11fcda2017-10-23 17:59:01 -0700765 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700766 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700767 tag := ctx.OtherModuleDependencyTag(module)
768
Jiyong Park750e5572018-01-31 00:20:13 +0900769 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700770 switch tag {
771 case bootClasspathTag, libTag, staticLibTag:
772 checkLinkType(ctx, j, to, tag.(dependencyTag))
773 }
Jiyong Park750e5572018-01-31 00:20:13 +0900774 }
Colin Cross54250902017-12-05 09:28:08 -0800775 switch dep := module.(type) {
776 case Dependency:
777 switch tag {
778 case bootClasspathTag:
779 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
780 case libTag:
781 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900782 // sdk lib names from dependencies are re-exported
783 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800784 case staticLibTag:
785 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
786 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
787 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900788 // sdk lib names from dependencies are re-exported
789 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700790 case annoTag:
791 deps.processorPath = append(deps.processorPath, dep.ImplementationJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800792 case frameworkResTag:
793 if ctx.ModuleName() == "framework" {
794 // framework.jar has a one-off dependency on the R.java and Manifest.java files
795 // generated by framework-res.apk
796 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
797 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800798 case frameworkApkTag:
799 if ctx.ModuleName() == "android_stubs_current" ||
800 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang16c0a312018-06-13 17:42:48 -0700801 ctx.ModuleName() == "android_test_stubs_current" ||
802 ctx.ModuleName() == "metalava_android_stubs_current" ||
803 ctx.ModuleName() == "metalava_android_system_stubs_current" ||
804 ctx.ModuleName() == "metalava_android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800805 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
806 // resource files out of there for aapt.
807 //
808 // Normally the package rule runs aapt, which includes the resource,
809 // but we're not running that in our package rule so just copy in the
810 // resource files here.
811 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
812 }
Colin Cross54250902017-12-05 09:28:08 -0800813 case kotlinStdlibTag:
814 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800815 }
816
817 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900818 case SdkLibraryDependency:
819 switch tag {
820 case libTag:
821 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900822 // names of sdk libs that are directly depended are exported
823 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900824 default:
825 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
826 }
Colin Cross54250902017-12-05 09:28:08 -0800827 case android.SourceFileProducer:
828 switch tag {
829 case libTag:
830 checkProducesJars(ctx, dep)
831 deps.classpath = append(deps.classpath, dep.Srcs()...)
832 case staticLibTag:
833 checkProducesJars(ctx, dep)
834 deps.classpath = append(deps.classpath, dep.Srcs()...)
835 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
836 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
837 case android.DefaultsDepTag, android.SourceDepTag:
838 // Nothing to do
839 default:
840 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
841 }
842 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700843 switch tag {
844 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700845 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700846 case systemModulesTag:
847 if deps.systemModules != nil {
848 panic("Found two system module dependencies")
849 }
850 sm := module.(*SystemModules)
851 if sm.outputFile == nil {
852 panic("Missing directory for system module dependency")
853 }
854 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700855 default:
856 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700857 }
Colin Crossec7a0422017-07-07 14:47:12 -0700858 }
Colin Cross2fe66872015-03-30 17:20:39 -0700859 })
860
Jiyong Park1be96912018-05-28 18:02:19 +0900861 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
862
Colin Cross32f676a2017-09-06 13:41:06 -0700863 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700864}
865
Colin Cross83bb3162018-06-25 15:48:06 -0700866func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700867 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700868 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
869 if err != nil {
870 ctx.PropertyErrorf("sdk_version", "%s", err)
871 }
Nan Zhang357466b2018-04-17 17:38:36 -0700872 if javaVersion != "" {
873 ret = javaVersion
874 } else if ctx.Device() && sdk <= 23 {
875 ret = "1.7"
876 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
877 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700878 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700879 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
880 ret = "1.8"
881 } else {
882 ret = "1.9"
883 }
884
885 return ret
886}
887
Nan Zhanged19fc32017-10-19 13:06:22 -0700888func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700889
Colin Crossf03c82b2015-04-13 13:53:40 -0700890 var flags javaBuilderFlags
891
Nan Zhanged19fc32017-10-19 13:06:22 -0700892 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700893 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800894 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700895 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700896 }
Colin Cross6510f912017-11-29 00:27:14 -0800897 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700898 // Override the -g flag passed globally to remove local variable debug info to reduce
899 // disk and memory usage.
900 javacFlags = append(javacFlags, "-g:source,lines")
901 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700902 if len(javacFlags) > 0 {
903 // optimization.
904 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
905 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700906 }
Colin Cross64162712017-08-08 13:17:59 -0700907
Colin Cross66548102018-06-19 22:47:35 -0700908 if ctx.Config().RunErrorProne() {
909 if config.ErrorProneClasspath == nil {
910 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
911 }
912
913 errorProneFlags := []string{
914 "-Xplugin:ErrorProne",
915 "${config.ErrorProneChecks}",
916 }
917 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
918
919 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
920 "'" + strings.Join(errorProneFlags, " ") + "'"
921 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800922 }
923
Nan Zhanged19fc32017-10-19 13:06:22 -0700924 // javaVersion flag.
Colin Cross83bb3162018-06-25 15:48:06 -0700925 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
Colin Cross64162712017-08-08 13:17:59 -0700926
Nan Zhanged19fc32017-10-19 13:06:22 -0700927 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800928 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
929 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700930 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800931
932 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
933 !Bool(j.properties.No_standard_libs) &&
934 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
935 // Give host-side tools a version of OpenJDK's standard libraries
936 // close to what they're targeting. As of Dec 2017, AOSP is only
937 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
938 //
939 // When building with OpenJDK 8, the following should have no
940 // effect since those jars would be available by default.
941 //
942 // When building with OpenJDK 9 but targeting a version < 1.8,
943 // putting them on the bootclasspath means that:
944 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
945 // b) references to existing APIs are not reinterpreted in an
946 // OpenJDK 9-specific way, eg. calls to subclasses of
947 // java.nio.Buffer as in http://b/70862583
948 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
949 flags.bootClasspath = append(flags.bootClasspath,
950 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
951 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800952 if Bool(j.properties.Use_tools_jar) {
953 flags.bootClasspath = append(flags.bootClasspath,
954 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
955 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800956 }
957
Nan Zhanged19fc32017-10-19 13:06:22 -0700958 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700959 if deps.systemModules != nil {
960 flags.systemModules = append(flags.systemModules, deps.systemModules)
961 }
962
Nan Zhanged19fc32017-10-19 13:06:22 -0700963 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700964 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700965 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700966 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700967 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
968 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700969 }
970
Nan Zhanged19fc32017-10-19 13:06:22 -0700971 return flags
972}
Colin Crossc0b06f12015-04-08 13:03:43 -0700973
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800974func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700975
Colin Crossebe1a512017-11-14 13:12:14 -0800976 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700977
978 deps := j.collectDeps(ctx)
979 flags := j.collectBuilderFlags(ctx, deps)
980
Colin Cross6510f912017-11-29 00:27:14 -0800981 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700982 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
983 }
984 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700985 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800986 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700987 }
988
Colin Crossaf050172017-11-15 23:01:59 -0800989 srcFiles = j.genSources(ctx, srcFiles, flags)
990
991 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700992 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800993 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700994
Colin Cross0a6e0072017-08-30 14:24:55 -0700995 var jars android.Paths
996
Colin Cross1ee23172017-10-18 14:44:18 -0700997 jarName := ctx.ModuleName() + ".jar"
998
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000999 javaSrcFiles := srcFiles.FilterByExt(".java")
1000 var uniqueSrcFiles android.Paths
1001 set := make(map[string]bool)
1002 for _, v := range javaSrcFiles {
1003 if _, found := set[v.String()]; !found {
1004 set[v.String()] = true
1005 uniqueSrcFiles = append(uniqueSrcFiles, v)
1006 }
1007 }
1008
Colin Cross37f6d792018-07-12 12:28:41 -07001009 var stripFiles []string
1010
Colin Cross93e85952017-08-15 13:34:18 -07001011 if srcFiles.HasExt(".kt") {
1012 // If there are kotlin files, compile them first but pass all the kotlin and java files
1013 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1014 // won't emit any classes for them.
1015
1016 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001017
Colin Cross93e85952017-08-15 13:34:18 -07001018 if ctx.Device() {
1019 flags.kotlincFlags += " -no-jdk"
1020 }
1021
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001022 var kotlinSrcFiles android.Paths
1023 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1024 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1025
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001026 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001027 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1028 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1029
Colin Cross1ee23172017-10-18 14:44:18 -07001030 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001031 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001032 if ctx.Failed() {
1033 return
1034 }
1035
1036 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001037 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001038 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001039
Colin Cross93e85952017-08-15 13:34:18 -07001040 // Jar kotlin classes into the final jar after javac
1041 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001042
Colin Cross37f6d792018-07-12 12:28:41 -07001043 if Bool(j.properties.Renamed_kotlin_stdlib) {
1044 // Remove any kotlin-reflect related files
1045 // TODO(pszczepaniak): Support kotlin-reflect
Colin Cross4c03f682018-07-15 08:16:31 -07001046 stripFiles = append(stripFiles,
1047 "**/*.kotlin_module",
Colin Cross29788aa2018-07-13 21:23:59 -07001048 "**/*.kotlin_builtins")
Colin Cross37f6d792018-07-12 12:28:41 -07001049 } else {
1050 // Only add kotlin-stdlib if not using (on-device) renamed stdlib
1051 // (it's expected to be on device bootclasspath)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001052 jars = append(jars, deps.kotlinStdlib...)
1053 }
Colin Cross93e85952017-08-15 13:34:18 -07001054 }
1055
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001056 // Store the list of .java files that was passed to javac
1057 j.compiledJavaSrcs = uniqueSrcFiles
1058 j.compiledSrcJars = srcJars
1059
Nan Zhang61eaedb2017-11-02 13:28:15 -07001060 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001061 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001062 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1063 enable_sharding = true
1064 if len(j.properties.Annotation_processors) != 0 ||
1065 len(j.properties.Annotation_processor_classes) != 0 {
1066 ctx.PropertyErrorf("javac_shard_size",
1067 "%q cannot be set when annotation processors are enabled.",
1068 j.properties.Javac_shard_size)
1069 }
1070 }
Colin Crossf19b9bb2018-03-26 14:42:44 -07001071 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
1072 if ctx.Failed() {
1073 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001074 }
1075 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001076 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001077 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001078 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001079 // If error-prone is enabled, add an additional rule to compile the java files into
1080 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001081 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001082 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1083 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001084 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001085 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001086 extraJarDeps = append(extraJarDeps, errorprone)
1087 }
1088
Nan Zhang61eaedb2017-11-02 13:28:15 -07001089 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001090 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001091 shardSize := int(*(j.properties.Javac_shard_size))
1092 var shardSrcs []android.Paths
1093 if len(uniqueSrcFiles) > 0 {
1094 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1095 for idx, shardSrc := range shardSrcs {
1096 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1097 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1098 jars = append(jars, classes)
1099 }
1100 }
1101 if len(srcJars) > 0 {
1102 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1103 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1104 jars = append(jars, classes)
1105 }
1106 } else {
1107 classes := android.PathForModuleOut(ctx, "javac", jarName)
1108 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1109 jars = append(jars, classes)
1110 }
Colin Crossd6891432017-09-27 17:39:56 -07001111 if ctx.Failed() {
1112 return
1113 }
Colin Cross2fe66872015-03-30 17:20:39 -07001114 }
1115
Colin Cross0f37af02017-09-27 17:42:05 -07001116 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
1117 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1118
1119 var resArgs []string
1120 var resDeps android.Paths
1121
1122 resArgs = append(resArgs, dirArgs...)
1123 resDeps = append(resDeps, dirDeps...)
1124
1125 resArgs = append(resArgs, fileArgs...)
1126 resDeps = append(resDeps, fileDeps...)
1127
Colin Crossff3ae9d2018-04-10 16:15:18 -07001128 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001129 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001130 resArgs = append(resArgs, srcArgs...)
1131 resDeps = append(resDeps, srcDeps...)
1132 }
Colin Cross40a36712017-09-27 17:41:35 -07001133
1134 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001135 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001136 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -07001137 if ctx.Failed() {
1138 return
1139 }
Colin Cross20978302015-04-10 17:05:07 -07001140
Colin Cross0a6e0072017-08-30 14:24:55 -07001141 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -07001142 }
1143
Colin Cross6ade34f2017-09-15 13:00:47 -07001144 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -07001145 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001146 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001147
Colin Cross366938f2017-12-11 16:29:02 -08001148 var manifest android.OptionalPath
1149 if j.properties.Manifest != nil {
1150 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1151 }
Colin Cross635acc92017-09-12 22:50:46 -07001152
Colin Cross0a6e0072017-08-30 14:24:55 -07001153 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001154 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001155 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001156
1157 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001158 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1159 // Optimization: skip the combine step if there is nothing to do
1160 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1161 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1162 // any if len(jars) == 1.
1163 outputFile = moduleOutPath
1164 } else {
1165 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1166 ctx.Build(pctx, android.BuildParams{
1167 Rule: android.Cp,
1168 Input: jars[0],
1169 Output: combinedJar,
1170 })
1171 outputFile = combinedJar
1172 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001173 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001174 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001175 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
1176 false, stripFiles, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001177 outputFile = combinedJar
1178 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001179
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001180 // Use renamed kotlin standard library?
Colin Crossff3ae9d2018-04-10 16:15:18 -07001181 if srcFiles.HasExt(".kt") && Bool(j.properties.Renamed_kotlin_stdlib) {
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001182 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
1183 TransformJarJar(ctx, jarjarFile, outputFile,
1184 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
1185 outputFile = jarjarFile
1186 if ctx.Failed() {
1187 return
1188 }
1189 }
1190
Colin Cross0a6e0072017-08-30 14:24:55 -07001191 if j.properties.Jarjar_rules != nil {
1192 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001193 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001194 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001195 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1196 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -07001197 if ctx.Failed() {
1198 return
1199 }
1200 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001201 j.implementationJarFile = outputFile
1202 if j.headerJarFile == nil {
1203 j.headerJarFile = j.implementationJarFile
1204 }
Colin Cross2fe66872015-03-30 17:20:39 -07001205
Colin Cross6510f912017-11-29 00:27:14 -08001206 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001207 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1208 j.properties.Instrument = true
1209 }
1210 }
1211
Colin Cross3144dfc2018-01-03 15:06:47 -08001212 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001213 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1214 }
1215
Colin Cross9ae1b922018-06-26 17:59:05 -07001216 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001217 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001218 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001219 if ctx.Failed() {
1220 return
1221 }
Colin Cross3063b782018-08-15 11:19:12 -07001222 outputFile = dexOutputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001223 }
Colin Crossb7a63242015-04-16 14:09:14 -07001224 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001225
1226 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1227 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001228}
1229
Colin Cross8eadbf02017-10-24 17:46:00 -07001230func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001231 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1232
1233 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001234 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001235 // Compile java sources into turbine.jar.
1236 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1237 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1238 if ctx.Failed() {
1239 return nil
1240 }
1241 jars = append(jars, turbineJar)
1242 }
1243
1244 // Combine any static header libraries into classes-header.jar. If there is only
1245 // one input jar this step will be skipped.
1246 var headerJar android.Path
1247 jars = append(jars, deps.staticHeaderJars...)
1248
Colin Cross5c6ecc12017-10-23 18:12:27 -07001249 // we cannot skip the combine step for now if there is only one jar
1250 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1251 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001252 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1253 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001254 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001255
1256 if j.properties.Jarjar_rules != nil {
1257 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1258 // Transform classes.jar into classes-jarjar.jar
1259 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1260 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1261 headerJar = jarjarFile
1262 if ctx.Failed() {
1263 return nil
1264 }
1265 }
1266
1267 return headerJar
1268}
1269
Colin Crosscb933592017-11-22 13:49:43 -08001270func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001271 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001272
Colin Cross7a3139e2017-12-19 13:57:50 -08001273 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001274
Colin Cross84c38822018-01-03 15:59:46 -08001275 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001276 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1277
1278 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1279
1280 j.jacocoReportClassesFile = jacocoReportClassesFile
1281
1282 return instrumentedJar
1283}
1284
Colin Crossf506d872017-07-19 15:53:04 -07001285var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001286
Nan Zhanged19fc32017-10-19 13:06:22 -07001287func (j *Module) HeaderJars() android.Paths {
1288 return android.Paths{j.headerJarFile}
1289}
1290
1291func (j *Module) ImplementationJars() android.Paths {
1292 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001293}
1294
Colin Cross46c9b8b2017-06-22 16:51:17 -07001295func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001296 return j.exportAidlIncludeDirs
1297}
1298
Jiyong Park1be96912018-05-28 18:02:19 +09001299func (j *Module) ExportedSdkLibs() []string {
1300 return j.exportedSdkLibs
1301}
1302
Colin Cross46c9b8b2017-06-22 16:51:17 -07001303var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001304
Colin Cross46c9b8b2017-06-22 16:51:17 -07001305func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001306 return j.logtagsSrcs
1307}
1308
Colin Cross2fe66872015-03-30 17:20:39 -07001309//
1310// Java libraries (.jar file)
1311//
1312
Colin Crossf506d872017-07-19 15:53:04 -07001313type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001314 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001315}
1316
Colin Crossf506d872017-07-19 15:53:04 -07001317func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001318 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001319
Colin Cross9ae1b922018-06-26 17:59:05 -07001320 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001321 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1322 ctx.ModuleName()+".jar", j.outputFile)
1323 }
Colin Crossb7a63242015-04-16 14:09:14 -07001324}
1325
Colin Crossf506d872017-07-19 15:53:04 -07001326func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001327 j.deps(ctx)
1328}
1329
Colin Cross9ae1b922018-06-26 17:59:05 -07001330func LibraryFactory() android.Module {
1331 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001332
Colin Cross9ae1b922018-06-26 17:59:05 -07001333 module.AddProperties(
1334 &module.Module.properties,
1335 &module.Module.deviceProperties,
1336 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001337
Colin Cross9ae1b922018-06-26 17:59:05 -07001338 InitJavaModule(module, android.HostAndDeviceSupported)
1339 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001340}
1341
Colin Crossf506d872017-07-19 15:53:04 -07001342func LibraryHostFactory() android.Module {
1343 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001344
Colin Cross6af17aa2017-09-20 12:59:05 -07001345 module.AddProperties(
1346 &module.Module.properties,
1347 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001348
Colin Cross9ae1b922018-06-26 17:59:05 -07001349 module.Module.properties.Installable = proptools.BoolPtr(true)
1350
Colin Cross89536d42017-07-07 14:35:50 -07001351 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001352 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001353}
1354
1355//
Colin Crossb628ea52018-08-14 16:42:33 -07001356// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001357//
1358
1359type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001360 // list of compatibility suites (for example "cts", "vts") that the module should be
1361 // installed into.
1362 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001363
1364 // the name of the test configuration (for example "AndroidTest.xml") that should be
1365 // installed with the module.
1366 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001367
1368 // list of files or filegroup modules that provide data that should be installed alongside
1369 // the test
1370 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001371}
1372
1373type Test struct {
1374 Library
1375
1376 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001377
1378 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001379 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001380}
1381
1382func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1383 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config)
Colin Crossd96ca352018-08-10 16:06:24 -07001384 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001385
1386 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001387}
1388
1389func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1390 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001391 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Colin Crossd96ca352018-08-10 16:06:24 -07001392 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001393}
1394
1395func TestFactory() android.Module {
1396 module := &Test{}
1397
1398 module.AddProperties(
1399 &module.Module.properties,
1400 &module.Module.deviceProperties,
1401 &module.Module.protoProperties,
1402 &module.testProperties)
1403
Colin Cross9ae1b922018-06-26 17:59:05 -07001404 module.Module.properties.Installable = proptools.BoolPtr(true)
1405
Colin Cross05638fc2018-04-09 18:40:24 -07001406 InitJavaModule(module, android.HostAndDeviceSupported)
1407 android.InitDefaultableModule(module)
1408 return module
1409}
1410
1411func TestHostFactory() android.Module {
1412 module := &Test{}
1413
1414 module.AddProperties(
1415 &module.Module.properties,
1416 &module.Module.protoProperties,
1417 &module.testProperties)
1418
Colin Cross9ae1b922018-06-26 17:59:05 -07001419 module.Module.properties.Installable = proptools.BoolPtr(true)
1420
Colin Cross05638fc2018-04-09 18:40:24 -07001421 InitJavaModule(module, android.HostSupported)
1422 android.InitDefaultableModule(module)
1423 return module
1424}
1425
1426//
Colin Cross2fe66872015-03-30 17:20:39 -07001427// Java Binaries (.jar file plus wrapper script)
1428//
1429
Colin Crossf506d872017-07-19 15:53:04 -07001430type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001431 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001432 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001433}
1434
Colin Crossf506d872017-07-19 15:53:04 -07001435type Binary struct {
1436 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001437
Colin Crossf506d872017-07-19 15:53:04 -07001438 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001439
Colin Cross6b4a32d2017-12-05 13:42:45 -08001440 isWrapperVariant bool
1441
Colin Crossc3315992017-12-08 19:12:36 -08001442 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001443 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001444}
1445
Alex Light24237172017-10-26 09:46:21 -07001446func (j *Binary) HostToolPath() android.OptionalPath {
1447 return android.OptionalPathForPath(j.binaryFile)
1448}
1449
Colin Crossf506d872017-07-19 15:53:04 -07001450func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001451 if ctx.Arch().ArchType == android.Common {
1452 // Compile the jar
1453 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001454 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001455 // Handle the binary wrapper
1456 j.isWrapperVariant = true
1457
Colin Cross366938f2017-12-11 16:29:02 -08001458 if j.binaryProperties.Wrapper != nil {
1459 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001460 } else {
1461 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1462 }
1463
1464 // Depend on the installed jar so that the wrapper doesn't get executed by
1465 // another build rule before the jar has been installed.
1466 jarFile := ctx.PrimaryModule().(*Binary).installFile
1467
1468 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1469 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001470 }
Colin Cross2fe66872015-03-30 17:20:39 -07001471}
1472
Colin Crossf506d872017-07-19 15:53:04 -07001473func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001474 if ctx.Arch().ArchType == android.Common {
1475 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001476 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001477 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001478 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001479}
1480
Colin Crossf506d872017-07-19 15:53:04 -07001481func BinaryFactory() android.Module {
1482 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001483
Colin Cross36242852017-06-23 15:06:31 -07001484 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001485 &module.Module.properties,
1486 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001487 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001488 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001489
Colin Cross9ae1b922018-06-26 17:59:05 -07001490 module.Module.properties.Installable = proptools.BoolPtr(true)
1491
Colin Cross6b4a32d2017-12-05 13:42:45 -08001492 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1493 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001494 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001495}
1496
Colin Crossf506d872017-07-19 15:53:04 -07001497func BinaryHostFactory() android.Module {
1498 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001499
Colin Cross36242852017-06-23 15:06:31 -07001500 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001501 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001502 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001503 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001504
Colin Cross9ae1b922018-06-26 17:59:05 -07001505 module.Module.properties.Installable = proptools.BoolPtr(true)
1506
Colin Cross6b4a32d2017-12-05 13:42:45 -08001507 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1508 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001509 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001510}
1511
1512//
1513// Java prebuilts
1514//
1515
Colin Cross74d73e22017-08-02 11:05:49 -07001516type ImportProperties struct {
1517 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001518
Nan Zhangea568a42017-11-08 21:20:04 -08001519 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001520
1521 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001522
1523 // List of shared java libs that this module has dependencies to
1524 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001525
1526 // List of files to remove from the jar file(s)
1527 Exclude_files []string
1528
1529 // List of directories to remove from the jar file(s)
1530 Exclude_dirs []string
Colin Cross74d73e22017-08-02 11:05:49 -07001531}
1532
1533type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001534 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001535 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001536
Colin Cross74d73e22017-08-02 11:05:49 -07001537 properties ImportProperties
1538
Colin Cross0a6e0072017-08-30 14:24:55 -07001539 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001540 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001541}
1542
Colin Cross83bb3162018-06-25 15:48:06 -07001543func (j *Import) sdkVersion() string {
1544 return String(j.properties.Sdk_version)
1545}
1546
1547func (j *Import) minSdkVersion() string {
1548 return j.sdkVersion()
1549}
1550
Colin Cross74d73e22017-08-02 11:05:49 -07001551func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001552 return &j.prebuilt
1553}
1554
Colin Cross74d73e22017-08-02 11:05:49 -07001555func (j *Import) PrebuiltSrcs() []string {
1556 return j.properties.Jars
1557}
1558
1559func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001560 return j.prebuilt.Name(j.ModuleBase.Name())
1561}
1562
Colin Cross74d73e22017-08-02 11:05:49 -07001563func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001564 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Jiyong Park1be96912018-05-28 18:02:19 +09001565 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001566}
1567
Colin Cross74d73e22017-08-02 11:05:49 -07001568func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001569 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001570
Colin Crosse9a275b2017-10-16 17:09:48 -07001571 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Colin Cross37f6d792018-07-12 12:28:41 -07001572 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1573 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Crosse9a275b2017-10-16 17:09:48 -07001574 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001575
1576 ctx.VisitDirectDeps(func(module android.Module) {
1577 otherName := ctx.OtherModuleName(module)
1578 tag := ctx.OtherModuleDependencyTag(module)
1579
1580 switch dep := module.(type) {
1581 case Dependency:
1582 switch tag {
1583 case libTag, staticLibTag:
1584 // sdk lib names from dependencies are re-exported
1585 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1586 }
1587 case SdkLibraryDependency:
1588 switch tag {
1589 case libTag:
1590 // names of sdk libs that are directly depended are exported
1591 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1592 }
1593 }
1594 })
1595
1596 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001597 if Bool(j.properties.Installable) {
1598 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1599 ctx.ModuleName()+".jar", outputFile)
1600 }
Colin Cross2fe66872015-03-30 17:20:39 -07001601}
1602
Colin Cross74d73e22017-08-02 11:05:49 -07001603var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001604
Nan Zhanged19fc32017-10-19 13:06:22 -07001605func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001606 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001607}
1608
1609func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001610 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001611}
1612
Colin Cross74d73e22017-08-02 11:05:49 -07001613func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001614 return nil
1615}
1616
Jiyong Park1be96912018-05-28 18:02:19 +09001617func (j *Import) ExportedSdkLibs() []string {
1618 return j.exportedSdkLibs
1619}
1620
Colin Cross74d73e22017-08-02 11:05:49 -07001621var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001622
Colin Cross74d73e22017-08-02 11:05:49 -07001623func ImportFactory() android.Module {
1624 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001625
Colin Cross74d73e22017-08-02 11:05:49 -07001626 module.AddProperties(&module.properties)
1627
1628 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001629 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1630 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001631}
1632
Colin Cross74d73e22017-08-02 11:05:49 -07001633func ImportFactoryHost() android.Module {
1634 module := &Import{}
1635
1636 module.AddProperties(&module.properties)
1637
1638 android.InitPrebuiltModule(module, &module.properties.Jars)
1639 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1640 return module
1641}
1642
Colin Cross89536d42017-07-07 14:35:50 -07001643//
1644// Defaults
1645//
1646type Defaults struct {
1647 android.ModuleBase
1648 android.DefaultsModuleBase
1649}
1650
1651func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1652}
1653
1654func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1655}
1656
1657func defaultsFactory() android.Module {
1658 return DefaultsFactory()
1659}
1660
1661func DefaultsFactory(props ...interface{}) android.Module {
1662 module := &Defaults{}
1663
1664 module.AddProperties(props...)
1665 module.AddProperties(
1666 &CompilerProperties{},
1667 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001668 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001669 )
1670
1671 android.InitDefaultsModule(module)
1672
1673 return module
1674}
Nan Zhangea568a42017-11-08 21:20:04 -08001675
1676var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001677var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001678var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001679var inList = android.InList