blob: b587990d77277d82065049c0fc8a647475a5029a [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 (
22 "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 Cross2fe66872015-03-30 17:20:39 -070032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Cross89536d42017-07-07 14:35:50 -070035 android.RegisterModuleType("java_defaults", defaultsFactory)
36
Colin Crossa60ead82017-10-02 18:10:21 -070037 android.RegisterModuleType("java_library", LibraryFactory(true))
38 android.RegisterModuleType("java_library_static", LibraryFactory(false))
Colin Crossf506d872017-07-19 15:53:04 -070039 android.RegisterModuleType("java_library_host", LibraryHostFactory)
40 android.RegisterModuleType("java_binary", BinaryFactory)
41 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070042 android.RegisterModuleType("java_import", ImportFactory)
43 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070044
Colin Cross798bfce2016-10-12 14:28:16 -070045 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070046}
47
Colin Cross2fe66872015-03-30 17:20:39 -070048// TODO:
49// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070050// Renderscript
51// Post-jar passes:
52// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070053// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070054// DroidDoc
55// Findbugs
56
Colin Cross89536d42017-07-07 14:35:50 -070057type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070058 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
59 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070060 Srcs []string `android:"arch_variant"`
61
62 // list of source files that should not be used to build the Java module.
63 // This is most useful in the arch/multilib variants to remove non-common files
64 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070065
66 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070067 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
Colin Cross86a63ff2017-09-27 17:33:10 -070069 // list of directories that should be excluded from java_resource_dirs
70 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070071
Colin Cross0f37af02017-09-27 17:42:05 -070072 // list of files to use as Java resources
73 Java_resources []string `android:"arch_variant"`
74
75 // list of files that should be excluded from java_resources
76 Exclude_java_resources []string `android:"arch_variant"`
77
Colin Crossfa5eb232017-10-01 20:33:03 -070078 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070080 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070081
Colin Crossfa5eb232017-10-01 20:33:03 -070082 // don't build against the framework libraries (legacy-test, core-junit,
83 // ext, and framework for device targets)
84 No_framework_libs *bool
85
Colin Cross7d5136f2015-05-11 13:39:40 -070086 // list of module-specific flags that will be used for javac compiles
87 Javacflags []string `android:"arch_variant"`
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070090 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070091
92 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070096 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070097
Colin Cross540eff82017-06-22 17:01:52 -070098 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -070099 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700100
101 // If not blank, set the java version passed to javac as -source and -target
102 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700103
104 // If set to false, don't allow this module to be installed. Defaults to true.
105 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700106
Colin Cross0f37af02017-09-27 17:42:05 -0700107 // If set to true, include sources used to compile the module in to the final jar
108 Include_srcs *bool
109
Colin Cross32f676a2017-09-06 13:41:06 -0700110 // List of modules to use as annotation processors
111 Annotation_processors []string
112
113 // List of classes to pass to javac to use as annotation processors
114 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700115
Nan Zhang61eaedb2017-11-02 13:28:15 -0700116 // The number of Java source entries each Javac instance can process
117 Javac_shard_size *int64
118
Nan Zhang5f8cb422018-02-06 10:34:32 -0800119 // Add host jdk tools.jar to bootclasspath
120 Use_tools_jar *bool
121
Colin Cross1369cdb2017-09-29 17:58:17 -0700122 Openjdk9 struct {
123 // List of source files that should only be used when passing -source 1.9
124 Srcs []string
125
126 // List of javac flags that should only be used when passing -source 1.9
127 Javacflags []string
128 }
Colin Crosscb933592017-11-22 13:49:43 -0800129
130 Jacoco struct {
131 // List of classes to include for instrumentation with jacoco to collect coverage
132 // information at runtime when building with coverage enabled. If unset defaults to all
133 // classes.
134 // Supports '*' as the last character of an entry in the list as a wildcard match.
135 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
136 // it matches classes in the package that have the class name as a prefix.
137 Include_filter []string
138
139 // List of classes to exclude from instrumentation with jacoco to collect coverage
140 // information at runtime when building with coverage enabled. Overrides classes selected
141 // by the include_filter property.
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 Exclude_filter []string
146 }
147
Andreas Gampef3e5b552018-01-22 21:27:21 -0800148 Errorprone struct {
149 // List of javac flags that should only be used when running errorprone.
150 Javacflags []string
151 }
152
Colin Cross0f2ee152017-12-14 15:22:43 -0800153 Proto struct {
154 // List of extra options that will be passed to the proto generator.
155 Output_params []string
156 }
157
Colin Crosscb933592017-11-22 13:49:43 -0800158 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700159}
160
Colin Cross89536d42017-07-07 14:35:50 -0700161type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700162 // list of module-specific flags that will be used for dex compiles
163 Dxflags []string `android:"arch_variant"`
164
Colin Cross7d5136f2015-05-11 13:39:40 -0700165 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800166 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700167
Colin Crossebe1a512017-11-14 13:12:14 -0800168 Aidl struct {
169 // Top level directories to pass to aidl tool
170 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700171
Colin Crossebe1a512017-11-14 13:12:14 -0800172 // Directories rooted at the Android.bp file to pass to aidl tool
173 Local_include_dirs []string
174
175 // directories that should be added as include directories for any aidl sources of modules
176 // that depend on this module, as well as to aidl for this module.
177 Export_include_dirs []string
178 }
Colin Cross92430102017-10-09 14:59:32 -0700179
180 // If true, export a copy of the module as a -hostdex module for host testing.
181 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700182
Colin Cross1bd87802017-12-05 15:31:19 -0800183 Dex_preopt struct {
184 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
185 // true.
186 Enabled *bool
187
188 // If true, generate an app image (.art file) for this module.
189 App_image *bool
190
191 // If true, use a checked-in profile to guide optimization. Defaults to false unless
192 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
193 // that matches the name of this module, in which case it is defaulted to true.
194 Profile_guided *bool
195
196 // If set, provides the path to profile relative to the Android.bp file. If not set,
197 // defaults to searching for a file that matches the name of this module in the default
198 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
199 Profile *string
200 }
Colin Crossa22116e2017-10-19 14:18:58 -0700201
Colin Cross66dbc0b2017-12-28 12:23:20 -0800202 Optimize struct {
203 // If false, disable all optimization. Defaults to true for apps, false for
204 // libraries and tests.
205 Enabled *bool
206
207 // If true, optimize for size by removing unused code. Defaults to true for apps,
208 // false for libraries and tests.
209 Shrink *bool
210
211 // If true, optimize bytecode. Defaults to false.
212 Optimize *bool
213
214 // If true, obfuscate bytecode. Defaults to false.
215 Obfuscate *bool
216
217 // If true, do not use the flag files generated by aapt that automatically keep
218 // classes referenced by the app manifest. Defaults to false.
219 No_aapt_flags *bool
220
221 // Flags to pass to proguard.
222 Proguard_flags []string
223
224 // Specifies the locations of files containing proguard flags.
225 Proguard_flags_files []string
226 }
227
Colin Cross1369cdb2017-09-29 17:58:17 -0700228 // When targeting 1.9, override the modules to use with --system
229 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700230}
231
Colin Cross46c9b8b2017-06-22 16:51:17 -0700232// Module contains the properties and members used by all java module types
233type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700234 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700235 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700236
Colin Cross89536d42017-07-07 14:35:50 -0700237 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700238 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700239 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700240
Nan Zhanged19fc32017-10-19 13:06:22 -0700241 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
242 headerJarFile android.Path
243
244 // full implementation jar file suitable for static dependency of another module compile
245 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700246
Colin Cross6ade34f2017-09-15 13:00:47 -0700247 // output file containing classes.dex
248 dexJarFile android.Path
249
Colin Crosscb933592017-11-22 13:49:43 -0800250 // output file containing uninstrumented classes that will be instrumented by jacoco
251 jacocoReportClassesFile android.Path
252
Colin Cross66dbc0b2017-12-28 12:23:20 -0800253 // output file containing mapping of obfuscated names
254 proguardDictionary android.Path
255
Colin Crossb7a63242015-04-16 14:09:14 -0700256 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700257 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700258
Colin Cross635c3b02016-05-18 15:37:25 -0700259 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700260
Colin Cross635c3b02016-05-18 15:37:25 -0700261 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700262
Colin Cross2fe66872015-03-30 17:20:39 -0700263 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700264 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800265
266 // list of .java files and srcjars that was passed to javac
267 compiledJavaSrcs android.Paths
268 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800269
270 // list of extra progurad flag files
271 extraProguardFlagFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700272}
273
Colin Cross54250902017-12-05 09:28:08 -0800274func (j *Module) Srcs() android.Paths {
275 return android.Paths{j.implementationJarFile}
276}
277
278var _ android.SourceFileProducer = (*Module)(nil)
279
Colin Crossf506d872017-07-19 15:53:04 -0700280type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700281 HeaderJars() android.Paths
282 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700283 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700284}
285
Colin Cross89536d42017-07-07 14:35:50 -0700286func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
287 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
288 android.InitDefaultableModule(module)
289}
290
Colin Crossbe1da472017-07-07 15:59:46 -0700291type dependencyTag struct {
292 blueprint.BaseDependencyTag
293 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700294}
295
Colin Crossbe1da472017-07-07 15:59:46 -0700296var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700297 staticLibTag = dependencyTag{name: "staticlib"}
298 libTag = dependencyTag{name: "javalib"}
299 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700300 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700301 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross93e85952017-08-15 13:34:18 -0700302 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700304)
Colin Cross2fe66872015-03-30 17:20:39 -0700305
Colin Crossfc3674a2017-09-18 17:41:52 -0700306type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700307 useModule, useFiles, useDefaultLibs, invalidVersion bool
308
Colin Cross1369cdb2017-09-29 17:58:17 -0700309 module string
310 systemModules string
311
312 jar android.Path
313 aidl android.Path
314}
315
316func sdkStringToNumber(ctx android.BaseContext, v string) int {
317 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900318 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900319 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700320 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900321 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700322 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
323 return -1
324 } else {
325 return i
326 }
327 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700328}
329
Colin Cross3144dfc2018-01-03 15:06:47 -0800330func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
331 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
332}
333
334func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
335 return j.shouldInstrument(ctx) &&
336 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
337 ctx.Config().UnbundledBuild())
338}
339
Colin Crossfc3674a2017-09-18 17:41:52 -0700340func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700341 i := sdkStringToNumber(ctx, v)
342 if i == -1 {
343 // Invalid sdk version, error handled by sdkStringToNumber.
344 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700345 }
346
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900347 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
348 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
349 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
350 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
351 if ctx.DeviceSpecific() || ctx.SocSpecific() {
352 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
353 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
354 }
355 }
356 version := strings.TrimPrefix(v, "system_")
357 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
358 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
359 v, allowed_versions)
360 }
361 }
362
Colin Crossfc3674a2017-09-18 17:41:52 -0700363 toFile := func(v string) sdkDep {
Jiyong Park750e5572018-01-31 00:20:13 +0900364 isCore := strings.HasPrefix(v, "core_")
365 if isCore {
366 v = strings.TrimPrefix(v, "core_")
367 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700368 dir := filepath.Join("prebuilts/sdk", v)
369 jar := filepath.Join(dir, "android.jar")
Jiyong Park750e5572018-01-31 00:20:13 +0900370 if isCore {
371 jar = filepath.Join(dir, "core.jar")
372 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700373 aidl := filepath.Join(dir, "framework.aidl")
374 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
375 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700376
Colin Cross6510f912017-11-29 00:27:14 -0800377 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700378 return sdkDep{
379 invalidVersion: true,
380 module: "sdk_v" + v,
381 }
382 }
383
Colin Crossfc3674a2017-09-18 17:41:52 -0700384 if !jarPath.Valid() {
385 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
386 return sdkDep{}
387 }
Colin Cross47ff2522017-10-02 14:22:08 -0700388
Colin Crossfc3674a2017-09-18 17:41:52 -0700389 if !aidlPath.Valid() {
390 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
391 return sdkDep{}
392 }
Colin Cross47ff2522017-10-02 14:22:08 -0700393
Colin Crossfc3674a2017-09-18 17:41:52 -0700394 return sdkDep{
395 useFiles: true,
396 jar: jarPath.Path(),
397 aidl: aidlPath.Path(),
398 }
399 }
400
Colin Cross2ebc4762017-10-20 14:00:31 -0700401 //toModule := func(m string) sdkDep {
402 // return sdkDep{
403 // useModule: true,
404 // module: m,
405 // systemModules: m + "_system_modules",
406 // }
407 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700408
Colin Cross6510f912017-11-29 00:27:14 -0800409 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700410 return toFile(v)
411 }
412
413 switch v {
414 case "":
415 return sdkDep{
416 useDefaultLibs: true,
417 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700418 // TODO(ccross): re-enable these once we generate stubs, until then
419 // use the stubs in prebuilts/sdk/*current
420 //case "current":
421 // return toModule("android_stubs_current")
422 //case "system_current":
423 // return toModule("android_system_stubs_current")
424 //case "test_current":
425 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700426 default:
427 return toFile(v)
428 }
429}
430
Colin Crossbe1da472017-07-07 15:59:46 -0700431func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700432 if ctx.Device() {
433 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800434 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700435 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700436 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800437 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700438 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
439 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700440 if !proptools.Bool(j.properties.No_framework_libs) {
441 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
442 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700443 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800444 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700445 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
446 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700447 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800448 if Bool(j.deviceProperties.Optimize.Enabled) {
449 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
450 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
451 }
Colin Crossbe1da472017-07-07 15:59:46 -0700452 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700453 } else if j.deviceProperties.System_modules == nil {
454 ctx.PropertyErrorf("no_standard_libs",
455 "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 -0800456 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700457 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700458 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800459 if ctx.ModuleName() == "framework" {
460 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
461 }
Colin Cross2fe66872015-03-30 17:20:39 -0700462 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700463
Colin Crossf506d872017-07-19 15:53:04 -0700464 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
465 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700466 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700467
468 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700469 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800470 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700471
472 if j.hasSrcExt(".proto") {
473 protoDeps(ctx, &j.protoProperties)
474 }
Colin Cross93e85952017-08-15 13:34:18 -0700475
476 if j.hasSrcExt(".kt") {
477 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
478 // Kotlin files
479 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
480 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800481
482 if j.shouldInstrumentStatic(ctx) {
483 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
484 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700485}
486
487func hasSrcExt(srcs []string, ext string) bool {
488 for _, src := range srcs {
489 if filepath.Ext(src) == ext {
490 return true
491 }
492 }
493
494 return false
495}
496
Nan Zhang61eaedb2017-11-02 13:28:15 -0700497func shardPaths(paths android.Paths, shardSize int) []android.Paths {
498 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
499 for len(paths) > shardSize {
500 ret = append(ret, paths[0:shardSize])
501 paths = paths[shardSize:]
502 }
503 if len(paths) > 0 {
504 ret = append(ret, paths)
505 }
506 return ret
507}
508
Colin Cross6af17aa2017-09-20 12:59:05 -0700509func (j *Module) hasSrcExt(ext string) bool {
510 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700511}
512
Colin Cross46c9b8b2017-06-22 16:51:17 -0700513func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700514 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700515
Colin Crossebe1a512017-11-14 13:12:14 -0800516 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
517 aidlIncludes = append(aidlIncludes,
518 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
519 aidlIncludes = append(aidlIncludes,
520 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700521
522 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700523 if aidlPreprocess.Valid() {
524 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700525 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700526 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700527 }
528
Colin Cross635c3b02016-05-18 15:37:25 -0700529 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800530 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700531 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crosse243c232017-10-21 15:16:37 -0700532 if src := android.ExistentPathForSource(ctx, "", ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700533 flags = append(flags, "-I"+src.String())
534 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700535
Colin Crossf03c82b2015-04-13 13:53:40 -0700536 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700537}
538
Colin Cross32f676a2017-09-06 13:41:06 -0700539type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700540 classpath android.Paths
541 bootClasspath android.Paths
542 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700543 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700544 staticJarResources android.Paths
545 aidlIncludeDirs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700546 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700547 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700548 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700549 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700550}
Colin Cross2fe66872015-03-30 17:20:39 -0700551
Colin Cross54250902017-12-05 09:28:08 -0800552func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
553 for _, f := range dep.Srcs() {
554 if f.Ext() != ".jar" {
555 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
556 ctx.OtherModuleName(dep.(blueprint.Module)))
557 }
558 }
559}
560
Jiyong Park750e5572018-01-31 00:20:13 +0900561func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
562 if strings.HasPrefix(String(from.deviceProperties.Sdk_version), "core_") {
563 if !strings.HasPrefix(String(to.deviceProperties.Sdk_version), "core_") {
564 ctx.ModuleErrorf("depends on other library %q using non-core Java APIs",
565 ctx.OtherModuleName(to))
566 }
567 }
568}
569
Colin Cross32f676a2017-09-06 13:41:06 -0700570func (j *Module) collectDeps(ctx android.ModuleContext) deps {
571 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700572
Nan Zhangea568a42017-11-08 21:20:04 -0800573 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross47ff2522017-10-02 14:22:08 -0700574 if sdkDep.invalidVersion {
575 ctx.AddMissingDependencies([]string{sdkDep.module})
576 } else if sdkDep.useFiles {
Nan Zhanged19fc32017-10-19 13:06:22 -0700577 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Crossfc3674a2017-09-18 17:41:52 -0700578 deps.classpath = append(deps.classpath, sdkDep.jar)
579 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
580 }
581
Colin Crossd11fcda2017-10-23 17:59:01 -0700582 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700583 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700584 tag := ctx.OtherModuleDependencyTag(module)
585
Jiyong Park750e5572018-01-31 00:20:13 +0900586 if to, ok := module.(*Library); ok {
587 checkLinkType(ctx, j, to, tag.(dependencyTag))
588 }
Colin Cross54250902017-12-05 09:28:08 -0800589 switch dep := module.(type) {
590 case Dependency:
591 switch tag {
592 case bootClasspathTag:
593 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
594 case libTag:
595 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
596 case staticLibTag:
597 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
598 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
599 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
600 case frameworkResTag:
601 if ctx.ModuleName() == "framework" {
602 // framework.jar has a one-off dependency on the R.java and Manifest.java files
603 // generated by framework-res.apk
604 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
605 }
606 case kotlinStdlibTag:
607 deps.kotlinStdlib = dep.HeaderJars()
608 default:
609 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
610 }
611
612 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
613 case android.SourceFileProducer:
614 switch tag {
615 case libTag:
616 checkProducesJars(ctx, dep)
617 deps.classpath = append(deps.classpath, dep.Srcs()...)
618 case staticLibTag:
619 checkProducesJars(ctx, dep)
620 deps.classpath = append(deps.classpath, dep.Srcs()...)
621 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
622 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
623 case android.DefaultsDepTag, android.SourceDepTag:
624 // Nothing to do
625 default:
626 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
627 }
628 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700629 switch tag {
630 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700631 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700632 case systemModulesTag:
633 if deps.systemModules != nil {
634 panic("Found two system module dependencies")
635 }
636 sm := module.(*SystemModules)
637 if sm.outputFile == nil {
638 panic("Missing directory for system module dependency")
639 }
640 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700641 default:
642 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700643 }
Colin Crossec7a0422017-07-07 14:47:12 -0700644 }
Colin Cross2fe66872015-03-30 17:20:39 -0700645 })
646
Colin Cross32f676a2017-09-06 13:41:06 -0700647 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700648}
649
Nan Zhanged19fc32017-10-19 13:06:22 -0700650func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700651
Colin Crossf03c82b2015-04-13 13:53:40 -0700652 var flags javaBuilderFlags
653
Nan Zhanged19fc32017-10-19 13:06:22 -0700654 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700655 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800656 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700657 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700658 }
Colin Cross6510f912017-11-29 00:27:14 -0800659 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700660 // Override the -g flag passed globally to remove local variable debug info to reduce
661 // disk and memory usage.
662 javacFlags = append(javacFlags, "-g:source,lines")
663 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700664 if len(javacFlags) > 0 {
665 // optimization.
666 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
667 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700668 }
Colin Cross64162712017-08-08 13:17:59 -0700669
Andreas Gampef3e5b552018-01-22 21:27:21 -0800670 if len(j.properties.Errorprone.Javacflags) > 0 {
671 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
672 }
673
Nan Zhanged19fc32017-10-19 13:06:22 -0700674 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800675 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700676 if j.properties.Java_version != nil {
677 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700678 } else if ctx.Device() && sdk <= 23 {
679 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800680 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700681 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900682 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700683 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
684 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700685 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700686 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700687 }
688
Nan Zhanged19fc32017-10-19 13:06:22 -0700689 // classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700690 flags.bootClasspath.AddPaths(deps.bootClasspath)
691 flags.classpath.AddPaths(deps.classpath)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800692
693 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
694 !Bool(j.properties.No_standard_libs) &&
695 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
696 // Give host-side tools a version of OpenJDK's standard libraries
697 // close to what they're targeting. As of Dec 2017, AOSP is only
698 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
699 //
700 // When building with OpenJDK 8, the following should have no
701 // effect since those jars would be available by default.
702 //
703 // When building with OpenJDK 9 but targeting a version < 1.8,
704 // putting them on the bootclasspath means that:
705 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
706 // b) references to existing APIs are not reinterpreted in an
707 // OpenJDK 9-specific way, eg. calls to subclasses of
708 // java.nio.Buffer as in http://b/70862583
709 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
710 flags.bootClasspath = append(flags.bootClasspath,
711 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
712 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800713 if Bool(j.properties.Use_tools_jar) {
714 flags.bootClasspath = append(flags.bootClasspath,
715 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
716 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800717 }
718
Nan Zhanged19fc32017-10-19 13:06:22 -0700719 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700720 if deps.systemModules != nil {
721 flags.systemModules = append(flags.systemModules, deps.systemModules)
722 }
723
Nan Zhanged19fc32017-10-19 13:06:22 -0700724 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700725 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700726 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700727 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700728 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
729 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700730 }
731
Nan Zhanged19fc32017-10-19 13:06:22 -0700732 return flags
733}
Colin Crossc0b06f12015-04-08 13:03:43 -0700734
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800735func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700736
Colin Crossebe1a512017-11-14 13:12:14 -0800737 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700738
739 deps := j.collectDeps(ctx)
740 flags := j.collectBuilderFlags(ctx, deps)
741
Colin Cross6510f912017-11-29 00:27:14 -0800742 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700743 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
744 }
745 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700746 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800747 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700748 }
749
Colin Crossaf050172017-11-15 23:01:59 -0800750 srcFiles = j.genSources(ctx, srcFiles, flags)
751
752 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700753 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800754 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700755
Colin Cross0a6e0072017-08-30 14:24:55 -0700756 var jars android.Paths
757
Colin Cross1ee23172017-10-18 14:44:18 -0700758 jarName := ctx.ModuleName() + ".jar"
759
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000760 javaSrcFiles := srcFiles.FilterByExt(".java")
761 var uniqueSrcFiles android.Paths
762 set := make(map[string]bool)
763 for _, v := range javaSrcFiles {
764 if _, found := set[v.String()]; !found {
765 set[v.String()] = true
766 uniqueSrcFiles = append(uniqueSrcFiles, v)
767 }
768 }
769
Colin Cross93e85952017-08-15 13:34:18 -0700770 if srcFiles.HasExt(".kt") {
771 // If there are kotlin files, compile them first but pass all the kotlin and java files
772 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
773 // won't emit any classes for them.
774
775 flags.kotlincFlags = "-no-stdlib"
776 if ctx.Device() {
777 flags.kotlincFlags += " -no-jdk"
778 }
779
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000780 var kotlinSrcFiles android.Paths
781 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
782 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
783
Colin Cross93e85952017-08-15 13:34:18 -0700784 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
785 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
786
Colin Cross1ee23172017-10-18 14:44:18 -0700787 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000788 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700789 if ctx.Failed() {
790 return
791 }
792
793 // Make javac rule depend on the kotlinc rule
794 flags.classpath = append(flags.classpath, kotlinJar)
795 // Jar kotlin classes into the final jar after javac
796 jars = append(jars, kotlinJar)
797 jars = append(jars, deps.kotlinStdlib...)
798 }
799
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800800 // Store the list of .java files that was passed to javac
801 j.compiledJavaSrcs = uniqueSrcFiles
802 j.compiledSrcJars = srcJars
803
Nan Zhang61eaedb2017-11-02 13:28:15 -0700804 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800805 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700806 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
807 enable_sharding = true
808 if len(j.properties.Annotation_processors) != 0 ||
809 len(j.properties.Annotation_processor_classes) != 0 {
810 ctx.PropertyErrorf("javac_shard_size",
811 "%q cannot be set when annotation processors are enabled.",
812 j.properties.Javac_shard_size)
813 }
814 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700815 // If sdk jar is java module, then directly return classesJar as header.jar
816 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
817 j.Name() != "android_test_stubs_current" {
818 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
819 if ctx.Failed() {
820 return
821 }
822 }
823 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700824 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700825 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800826 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700827 // If error-prone is enabled, add an additional rule to compile the java files into
828 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700829 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700830 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
831 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700832 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700833 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700834 extraJarDeps = append(extraJarDeps, errorprone)
835 }
836
Nan Zhang61eaedb2017-11-02 13:28:15 -0700837 if enable_sharding {
838 flags.classpath.AddPaths([]android.Path{j.headerJarFile})
839 shardSize := int(*(j.properties.Javac_shard_size))
840 var shardSrcs []android.Paths
841 if len(uniqueSrcFiles) > 0 {
842 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
843 for idx, shardSrc := range shardSrcs {
844 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
845 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
846 jars = append(jars, classes)
847 }
848 }
849 if len(srcJars) > 0 {
850 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
851 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
852 jars = append(jars, classes)
853 }
854 } else {
855 classes := android.PathForModuleOut(ctx, "javac", jarName)
856 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
857 jars = append(jars, classes)
858 }
Colin Crossd6891432017-09-27 17:39:56 -0700859 if ctx.Failed() {
860 return
861 }
Colin Cross2fe66872015-03-30 17:20:39 -0700862 }
863
Colin Cross0f37af02017-09-27 17:42:05 -0700864 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
865 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
866
867 var resArgs []string
868 var resDeps android.Paths
869
870 resArgs = append(resArgs, dirArgs...)
871 resDeps = append(resDeps, dirDeps...)
872
873 resArgs = append(resArgs, fileArgs...)
874 resDeps = append(resDeps, fileDeps...)
875
876 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700877 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700878 resArgs = append(resArgs, srcArgs...)
879 resDeps = append(resDeps, srcDeps...)
880 }
Colin Cross40a36712017-09-27 17:41:35 -0700881
882 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700883 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700884 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700885 if ctx.Failed() {
886 return
887 }
Colin Cross20978302015-04-10 17:05:07 -0700888
Colin Cross0a6e0072017-08-30 14:24:55 -0700889 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700890 }
891
Colin Cross6ade34f2017-09-15 13:00:47 -0700892 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700893 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700894
Colin Cross366938f2017-12-11 16:29:02 -0800895 var manifest android.OptionalPath
896 if j.properties.Manifest != nil {
897 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
898 }
Colin Cross635acc92017-09-12 22:50:46 -0700899
Colin Cross0a6e0072017-08-30 14:24:55 -0700900 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700901 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700902 var outputFile android.Path
903
904 if len(jars) == 1 && !manifest.Valid() {
905 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -0800906 // TODO(ccross): this leaves any module-info.class files, but those should only come from
907 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
908 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -0700909 outputFile = jars[0]
910 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700911 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700912 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700913 outputFile = combinedJar
914 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700915
916 if j.properties.Jarjar_rules != nil {
917 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700918 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700919 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700920 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
921 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700922 if ctx.Failed() {
923 return
924 }
925 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700926 j.implementationJarFile = outputFile
927 if j.headerJarFile == nil {
928 j.headerJarFile = j.implementationJarFile
929 }
Colin Cross2fe66872015-03-30 17:20:39 -0700930
Colin Cross6510f912017-11-29 00:27:14 -0800931 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -0800932 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
933 j.properties.Instrument = true
934 }
935 }
936
Colin Cross3144dfc2018-01-03 15:06:47 -0800937 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -0800938 outputFile = j.instrument(ctx, flags, outputFile, jarName)
939 }
940
941 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -0800942 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -0700943 if ctx.Failed() {
944 return
945 }
Colin Cross2fe66872015-03-30 17:20:39 -0700946 }
Colin Crossb7a63242015-04-16 14:09:14 -0700947 ctx.CheckbuildFile(outputFile)
948 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700949}
950
Colin Cross8eadbf02017-10-24 17:46:00 -0700951func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -0700952 deps deps, flags javaBuilderFlags, jarName string) android.Path {
953
954 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -0700955 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700956 // Compile java sources into turbine.jar.
957 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
958 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
959 if ctx.Failed() {
960 return nil
961 }
962 jars = append(jars, turbineJar)
963 }
964
965 // Combine any static header libraries into classes-header.jar. If there is only
966 // one input jar this step will be skipped.
967 var headerJar android.Path
968 jars = append(jars, deps.staticHeaderJars...)
969
Colin Cross5c6ecc12017-10-23 18:12:27 -0700970 // we cannot skip the combine step for now if there is only one jar
971 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
972 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
973 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
974 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -0700975
976 if j.properties.Jarjar_rules != nil {
977 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
978 // Transform classes.jar into classes-jarjar.jar
979 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
980 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
981 headerJar = jarjarFile
982 if ctx.Failed() {
983 return nil
984 }
985 }
986
987 return headerJar
988}
989
Colin Crosscb933592017-11-22 13:49:43 -0800990func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
991 classesJar android.Path, jarName string) android.Path {
992
Colin Cross7a3139e2017-12-19 13:57:50 -0800993 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -0800994
Colin Cross84c38822018-01-03 15:59:46 -0800995 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -0800996 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
997
998 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
999
1000 j.jacocoReportClassesFile = jacocoReportClassesFile
1001
1002 return instrumentedJar
1003}
1004
Colin Crosscb933592017-11-22 13:49:43 -08001005// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1006// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1007func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1008 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001009 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001010 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001011 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001012 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001013 }
1014}
1015
Colin Cross59f1bb62017-09-27 17:59:10 -07001016func (j *Module) installable() bool {
1017 return j.properties.Installable == nil || *j.properties.Installable
1018}
1019
Colin Crossf506d872017-07-19 15:53:04 -07001020var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001021
Nan Zhanged19fc32017-10-19 13:06:22 -07001022func (j *Module) HeaderJars() android.Paths {
1023 return android.Paths{j.headerJarFile}
1024}
1025
1026func (j *Module) ImplementationJars() android.Paths {
1027 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001028}
1029
Colin Cross46c9b8b2017-06-22 16:51:17 -07001030func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001031 return j.exportAidlIncludeDirs
1032}
1033
Colin Cross46c9b8b2017-06-22 16:51:17 -07001034var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001035
Colin Cross46c9b8b2017-06-22 16:51:17 -07001036func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001037 return j.logtagsSrcs
1038}
1039
Colin Cross2fe66872015-03-30 17:20:39 -07001040//
1041// Java libraries (.jar file)
1042//
1043
Colin Crossf506d872017-07-19 15:53:04 -07001044type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001045 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001046}
1047
Colin Crossf506d872017-07-19 15:53:04 -07001048func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001049 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001050
Colin Cross59f1bb62017-09-27 17:59:10 -07001051 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001052 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1053 ctx.ModuleName()+".jar", j.outputFile)
1054 }
Colin Crossb7a63242015-04-16 14:09:14 -07001055}
1056
Colin Crossf506d872017-07-19 15:53:04 -07001057func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001058 j.deps(ctx)
1059}
1060
Colin Crossa60ead82017-10-02 18:10:21 -07001061func LibraryFactory(installable bool) func() android.Module {
1062 return func() android.Module {
1063 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001064
Colin Crossa60ead82017-10-02 18:10:21 -07001065 if !installable {
1066 module.properties.Installable = proptools.BoolPtr(false)
1067 }
Colin Cross2fe66872015-03-30 17:20:39 -07001068
Colin Crossa60ead82017-10-02 18:10:21 -07001069 module.AddProperties(
1070 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001071 &module.Module.deviceProperties,
1072 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001073
Colin Crossa60ead82017-10-02 18:10:21 -07001074 InitJavaModule(module, android.HostAndDeviceSupported)
1075 return module
1076 }
Colin Cross2fe66872015-03-30 17:20:39 -07001077}
1078
Colin Crossf506d872017-07-19 15:53:04 -07001079func LibraryHostFactory() android.Module {
1080 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001081
Colin Cross6af17aa2017-09-20 12:59:05 -07001082 module.AddProperties(
1083 &module.Module.properties,
1084 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001085
Colin Cross89536d42017-07-07 14:35:50 -07001086 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001087 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001088}
1089
1090//
1091// Java Binaries (.jar file plus wrapper script)
1092//
1093
Colin Crossf506d872017-07-19 15:53:04 -07001094type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001095 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001096 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001097}
1098
Colin Crossf506d872017-07-19 15:53:04 -07001099type Binary struct {
1100 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001101
Colin Crossf506d872017-07-19 15:53:04 -07001102 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001103
Colin Cross6b4a32d2017-12-05 13:42:45 -08001104 isWrapperVariant bool
1105
Colin Crossc3315992017-12-08 19:12:36 -08001106 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001107 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001108}
1109
Alex Light24237172017-10-26 09:46:21 -07001110func (j *Binary) HostToolPath() android.OptionalPath {
1111 return android.OptionalPathForPath(j.binaryFile)
1112}
1113
Colin Crossf506d872017-07-19 15:53:04 -07001114func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001115 if ctx.Arch().ArchType == android.Common {
1116 // Compile the jar
1117 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001118 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001119 // Handle the binary wrapper
1120 j.isWrapperVariant = true
1121
Colin Cross366938f2017-12-11 16:29:02 -08001122 if j.binaryProperties.Wrapper != nil {
1123 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001124 } else {
1125 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1126 }
1127
1128 // Depend on the installed jar so that the wrapper doesn't get executed by
1129 // another build rule before the jar has been installed.
1130 jarFile := ctx.PrimaryModule().(*Binary).installFile
1131
1132 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1133 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001134 }
Colin Cross2fe66872015-03-30 17:20:39 -07001135}
1136
Colin Crossf506d872017-07-19 15:53:04 -07001137func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001138 if ctx.Arch().ArchType == android.Common {
1139 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001140 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001141 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001142 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001143}
1144
Colin Crossf506d872017-07-19 15:53:04 -07001145func BinaryFactory() android.Module {
1146 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001147
Colin Cross36242852017-06-23 15:06:31 -07001148 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001149 &module.Module.properties,
1150 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001151 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001152 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001153
Colin Cross6b4a32d2017-12-05 13:42:45 -08001154 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1155 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001156 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001157}
1158
Colin Crossf506d872017-07-19 15:53:04 -07001159func BinaryHostFactory() android.Module {
1160 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001161
Colin Cross36242852017-06-23 15:06:31 -07001162 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001163 &module.Module.properties,
1164 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001165 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001166 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001167
Colin Cross6b4a32d2017-12-05 13:42:45 -08001168 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1169 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001170 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001171}
1172
1173//
1174// Java prebuilts
1175//
1176
Colin Cross74d73e22017-08-02 11:05:49 -07001177type ImportProperties struct {
1178 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001179
Nan Zhangea568a42017-11-08 21:20:04 -08001180 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001181
1182 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001183}
1184
1185type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001186 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001187 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001188
Colin Cross74d73e22017-08-02 11:05:49 -07001189 properties ImportProperties
1190
Colin Cross0a6e0072017-08-30 14:24:55 -07001191 classpathFiles android.Paths
1192 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001193}
1194
Colin Cross74d73e22017-08-02 11:05:49 -07001195func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001196 return &j.prebuilt
1197}
1198
Colin Cross74d73e22017-08-02 11:05:49 -07001199func (j *Import) PrebuiltSrcs() []string {
1200 return j.properties.Jars
1201}
1202
1203func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001204 return j.prebuilt.Name(j.ModuleBase.Name())
1205}
1206
Colin Cross74d73e22017-08-02 11:05:49 -07001207func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001208}
1209
Colin Cross74d73e22017-08-02 11:05:49 -07001210func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1211 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001212
Colin Crosse9a275b2017-10-16 17:09:48 -07001213 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001214 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001215 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001216}
1217
Colin Cross74d73e22017-08-02 11:05:49 -07001218var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001219
Nan Zhanged19fc32017-10-19 13:06:22 -07001220func (j *Import) HeaderJars() android.Paths {
1221 return j.classpathFiles
1222}
1223
1224func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001225 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001226}
1227
Colin Cross74d73e22017-08-02 11:05:49 -07001228func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001229 return nil
1230}
1231
Colin Cross74d73e22017-08-02 11:05:49 -07001232var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001233
Colin Cross74d73e22017-08-02 11:05:49 -07001234func ImportFactory() android.Module {
1235 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001236
Colin Cross74d73e22017-08-02 11:05:49 -07001237 module.AddProperties(&module.properties)
1238
1239 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001240 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1241 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001242}
1243
Colin Cross74d73e22017-08-02 11:05:49 -07001244func ImportFactoryHost() android.Module {
1245 module := &Import{}
1246
1247 module.AddProperties(&module.properties)
1248
1249 android.InitPrebuiltModule(module, &module.properties.Jars)
1250 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1251 return module
1252}
1253
Colin Cross2fe66872015-03-30 17:20:39 -07001254func inList(s string, l []string) bool {
1255 for _, e := range l {
1256 if e == s {
1257 return true
1258 }
1259 }
1260 return false
1261}
Colin Cross89536d42017-07-07 14:35:50 -07001262
1263//
1264// Defaults
1265//
1266type Defaults struct {
1267 android.ModuleBase
1268 android.DefaultsModuleBase
1269}
1270
1271func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1272}
1273
1274func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1275}
1276
1277func defaultsFactory() android.Module {
1278 return DefaultsFactory()
1279}
1280
1281func DefaultsFactory(props ...interface{}) android.Module {
1282 module := &Defaults{}
1283
1284 module.AddProperties(props...)
1285 module.AddProperties(
1286 &CompilerProperties{},
1287 &CompilerDeviceProperties{},
1288 )
1289
1290 android.InitDefaultsModule(module)
1291
1292 return module
1293}
Nan Zhangea568a42017-11-08 21:20:04 -08001294
1295var Bool = proptools.Bool
1296var String = proptools.String