blob: fd0719cf3a06bbea1efcb9de8b7d3fda007e3f5c [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 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 Cross05638fc2018-04-09 18:40:24 -070042 android.RegisterModuleType("java_test", TestFactory)
43 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070044 android.RegisterModuleType("java_import", ImportFactory)
45 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070046
Colin Cross798bfce2016-10-12 14:28:16 -070047 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070048}
49
Colin Cross2fe66872015-03-30 17:20:39 -070050// TODO:
51// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070052// Renderscript
53// Post-jar passes:
54// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070055// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070056// DroidDoc
57// Findbugs
58
Colin Cross89536d42017-07-07 14:35:50 -070059type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070060 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
61 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070062 Srcs []string `android:"arch_variant"`
63
64 // list of source files that should not be used to build the Java module.
65 // This is most useful in the arch/multilib variants to remove non-common files
66 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070067
68 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070069 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070070
Colin Cross86a63ff2017-09-27 17:33:10 -070071 // list of directories that should be excluded from java_resource_dirs
72 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070073
Colin Cross0f37af02017-09-27 17:42:05 -070074 // list of files to use as Java resources
75 Java_resources []string `android:"arch_variant"`
76
77 // list of files that should be excluded from java_resources
78 Exclude_java_resources []string `android:"arch_variant"`
79
Colin Crossfa5eb232017-10-01 20:33:03 -070080 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070081 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070082 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070083
Colin Crossfa5eb232017-10-01 20:33:03 -070084 // don't build against the framework libraries (legacy-test, core-junit,
85 // ext, and framework for device targets)
86 No_framework_libs *bool
87
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000088 // Use renamed kotlin stdlib (com.android.kotlin.*). This allows kotlin usage without colliding
89 // with app-provided kotlin stdlib.
90 Renamed_kotlin_stdlib *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Colin Cross7d5136f2015-05-11 13:39:40 -070095 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross540eff82017-06-22 17:01:52 -0700104 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700105 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700106
107 // If not blank, set the java version passed to javac as -source and -target
108 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700109
110 // If set to false, don't allow this module to be installed. Defaults to true.
111 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700112
Colin Cross0f37af02017-09-27 17:42:05 -0700113 // If set to true, include sources used to compile the module in to the final jar
114 Include_srcs *bool
115
Colin Cross32f676a2017-09-06 13:41:06 -0700116 // List of modules to use as annotation processors
117 Annotation_processors []string
118
119 // List of classes to pass to javac to use as annotation processors
120 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700121
Nan Zhang61eaedb2017-11-02 13:28:15 -0700122 // The number of Java source entries each Javac instance can process
123 Javac_shard_size *int64
124
Nan Zhang5f8cb422018-02-06 10:34:32 -0800125 // Add host jdk tools.jar to bootclasspath
126 Use_tools_jar *bool
127
Colin Cross1369cdb2017-09-29 17:58:17 -0700128 Openjdk9 struct {
129 // List of source files that should only be used when passing -source 1.9
130 Srcs []string
131
132 // List of javac flags that should only be used when passing -source 1.9
133 Javacflags []string
134 }
Colin Crosscb933592017-11-22 13:49:43 -0800135
136 Jacoco struct {
137 // List of classes to include for instrumentation with jacoco to collect coverage
138 // information at runtime when building with coverage enabled. If unset defaults to all
139 // classes.
140 // Supports '*' as the last character of an entry in the list as a wildcard match.
141 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
142 // it matches classes in the package that have the class name as a prefix.
143 Include_filter []string
144
145 // List of classes to exclude from instrumentation with jacoco to collect coverage
146 // information at runtime when building with coverage enabled. Overrides classes selected
147 // by the include_filter property.
148 // Supports '*' as the last character of an entry in the list as a wildcard match.
149 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
150 // it matches classes in the package that have the class name as a prefix.
151 Exclude_filter []string
152 }
153
Andreas Gampef3e5b552018-01-22 21:27:21 -0800154 Errorprone struct {
155 // List of javac flags that should only be used when running errorprone.
156 Javacflags []string
157 }
158
Colin Cross0f2ee152017-12-14 15:22:43 -0800159 Proto struct {
160 // List of extra options that will be passed to the proto generator.
161 Output_params []string
162 }
163
Colin Crosscb933592017-11-22 13:49:43 -0800164 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700165}
166
Colin Cross89536d42017-07-07 14:35:50 -0700167type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700168 // list of module-specific flags that will be used for dex compiles
169 Dxflags []string `android:"arch_variant"`
170
Colin Cross7d5136f2015-05-11 13:39:40 -0700171 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800172 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700173
Colin Crossebe1a512017-11-14 13:12:14 -0800174 Aidl struct {
175 // Top level directories to pass to aidl tool
176 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700177
Colin Crossebe1a512017-11-14 13:12:14 -0800178 // Directories rooted at the Android.bp file to pass to aidl tool
179 Local_include_dirs []string
180
181 // directories that should be added as include directories for any aidl sources of modules
182 // that depend on this module, as well as to aidl for this module.
183 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100184
185 // whether to generate traces (for systrace) for this interface
186 Generate_traces *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800187 }
Colin Cross92430102017-10-09 14:59:32 -0700188
189 // If true, export a copy of the module as a -hostdex module for host testing.
190 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700191
Colin Cross1bd87802017-12-05 15:31:19 -0800192 Dex_preopt struct {
193 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
194 // true.
195 Enabled *bool
196
197 // If true, generate an app image (.art file) for this module.
198 App_image *bool
199
200 // If true, use a checked-in profile to guide optimization. Defaults to false unless
201 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
202 // that matches the name of this module, in which case it is defaulted to true.
203 Profile_guided *bool
204
205 // If set, provides the path to profile relative to the Android.bp file. If not set,
206 // defaults to searching for a file that matches the name of this module in the default
207 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
208 Profile *string
209 }
Colin Crossa22116e2017-10-19 14:18:58 -0700210
Colin Cross66dbc0b2017-12-28 12:23:20 -0800211 Optimize struct {
212 // If false, disable all optimization. Defaults to true for apps, false for
213 // libraries and tests.
214 Enabled *bool
215
216 // If true, optimize for size by removing unused code. Defaults to true for apps,
217 // false for libraries and tests.
218 Shrink *bool
219
220 // If true, optimize bytecode. Defaults to false.
221 Optimize *bool
222
223 // If true, obfuscate bytecode. Defaults to false.
224 Obfuscate *bool
225
226 // If true, do not use the flag files generated by aapt that automatically keep
227 // classes referenced by the app manifest. Defaults to false.
228 No_aapt_flags *bool
229
230 // Flags to pass to proguard.
231 Proguard_flags []string
232
233 // Specifies the locations of files containing proguard flags.
234 Proguard_flags_files []string
235 }
236
Colin Cross1369cdb2017-09-29 17:58:17 -0700237 // When targeting 1.9, override the modules to use with --system
238 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700239}
240
Colin Cross46c9b8b2017-06-22 16:51:17 -0700241// Module contains the properties and members used by all java module types
242type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700243 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700244 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700245
Colin Cross89536d42017-07-07 14:35:50 -0700246 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700247 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700248 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700249
Nan Zhanged19fc32017-10-19 13:06:22 -0700250 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
251 headerJarFile android.Path
252
253 // full implementation jar file suitable for static dependency of another module compile
254 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700255
Colin Cross6ade34f2017-09-15 13:00:47 -0700256 // output file containing classes.dex
257 dexJarFile android.Path
258
Colin Crosscb933592017-11-22 13:49:43 -0800259 // output file containing uninstrumented classes that will be instrumented by jacoco
260 jacocoReportClassesFile android.Path
261
Colin Cross66dbc0b2017-12-28 12:23:20 -0800262 // output file containing mapping of obfuscated names
263 proguardDictionary android.Path
264
Colin Crossb7a63242015-04-16 14:09:14 -0700265 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700266 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700267
Colin Cross635c3b02016-05-18 15:37:25 -0700268 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700269
Colin Cross635c3b02016-05-18 15:37:25 -0700270 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700271
Colin Cross2fe66872015-03-30 17:20:39 -0700272 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700273 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800274
275 // list of .java files and srcjars that was passed to javac
276 compiledJavaSrcs android.Paths
277 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800278
279 // list of extra progurad flag files
280 extraProguardFlagFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700281}
282
Colin Cross54250902017-12-05 09:28:08 -0800283func (j *Module) Srcs() android.Paths {
284 return android.Paths{j.implementationJarFile}
285}
286
287var _ android.SourceFileProducer = (*Module)(nil)
288
Colin Crossf506d872017-07-19 15:53:04 -0700289type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 HeaderJars() android.Paths
291 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700292 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700293}
294
Nan Zhangb2b33de2018-02-23 11:18:47 -0800295type SrcDependency interface {
296 CompiledSrcs() android.Paths
297 CompiledSrcJars() android.Paths
298}
299
300func (j *Module) CompiledSrcs() android.Paths {
301 return j.compiledJavaSrcs
302}
303
304func (j *Module) CompiledSrcJars() android.Paths {
305 return j.compiledSrcJars
306}
307
308var _ SrcDependency = (*Module)(nil)
309
Colin Cross89536d42017-07-07 14:35:50 -0700310func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
311 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
312 android.InitDefaultableModule(module)
313}
314
Colin Crossbe1da472017-07-07 15:59:46 -0700315type dependencyTag struct {
316 blueprint.BaseDependencyTag
317 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700318}
319
Colin Crossbe1da472017-07-07 15:59:46 -0700320var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700321 staticLibTag = dependencyTag{name: "staticlib"}
322 libTag = dependencyTag{name: "javalib"}
323 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700324 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700325 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800326 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700327 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800328 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700329)
Colin Cross2fe66872015-03-30 17:20:39 -0700330
Colin Crossfc3674a2017-09-18 17:41:52 -0700331type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700332 useModule, useFiles, useDefaultLibs, invalidVersion bool
333
Colin Cross1369cdb2017-09-29 17:58:17 -0700334 module string
335 systemModules string
336
337 jar android.Path
338 aidl android.Path
339}
340
341func sdkStringToNumber(ctx android.BaseContext, v string) int {
342 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900343 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900344 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700345 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900346 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700347 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
348 return -1
349 } else {
350 return i
351 }
352 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700353}
354
Colin Cross3144dfc2018-01-03 15:06:47 -0800355func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
356 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
357}
358
359func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
360 return j.shouldInstrument(ctx) &&
361 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
362 ctx.Config().UnbundledBuild())
363}
364
Colin Crossfc3674a2017-09-18 17:41:52 -0700365func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700366 i := sdkStringToNumber(ctx, v)
367 if i == -1 {
368 // Invalid sdk version, error handled by sdkStringToNumber.
369 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700370 }
371
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900372 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
373 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
374 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
375 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
376 if ctx.DeviceSpecific() || ctx.SocSpecific() {
377 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
378 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
379 }
380 }
381 version := strings.TrimPrefix(v, "system_")
382 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
383 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
384 v, allowed_versions)
385 }
386 }
387
Colin Crossfc3674a2017-09-18 17:41:52 -0700388 toFile := func(v string) sdkDep {
Jiyong Park750e5572018-01-31 00:20:13 +0900389 isCore := strings.HasPrefix(v, "core_")
390 if isCore {
391 v = strings.TrimPrefix(v, "core_")
392 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700393 dir := filepath.Join("prebuilts/sdk", v)
394 jar := filepath.Join(dir, "android.jar")
Jiyong Park750e5572018-01-31 00:20:13 +0900395 if isCore {
396 jar = filepath.Join(dir, "core.jar")
397 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700398 aidl := filepath.Join(dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800399 jarPath := android.ExistentPathForSource(ctx, jar)
400 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700401
Colin Cross6510f912017-11-29 00:27:14 -0800402 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700403 return sdkDep{
404 invalidVersion: true,
405 module: "sdk_v" + v,
406 }
407 }
408
Colin Crossfc3674a2017-09-18 17:41:52 -0700409 if !jarPath.Valid() {
410 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
411 return sdkDep{}
412 }
Colin Cross47ff2522017-10-02 14:22:08 -0700413
Colin Crossfc3674a2017-09-18 17:41:52 -0700414 if !aidlPath.Valid() {
415 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
416 return sdkDep{}
417 }
Colin Cross47ff2522017-10-02 14:22:08 -0700418
Colin Crossfc3674a2017-09-18 17:41:52 -0700419 return sdkDep{
420 useFiles: true,
421 jar: jarPath.Path(),
422 aidl: aidlPath.Path(),
423 }
424 }
425
Colin Crossf19b9bb2018-03-26 14:42:44 -0700426 toModule := func(m string) sdkDep {
427 ret := sdkDep{
428 useModule: true,
429 module: m,
430 systemModules: m + "_system_modules",
431 }
432 if m == "core.current.stubs" {
433 ret.systemModules = "core-system-modules"
434 }
435 return ret
436 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700437
Colin Cross6510f912017-11-29 00:27:14 -0800438 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700439 return toFile(v)
440 }
441
442 switch v {
443 case "":
444 return sdkDep{
445 useDefaultLibs: true,
446 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700447 case "current":
448 return toModule("android_stubs_current")
449 case "system_current":
450 return toModule("android_system_stubs_current")
451 case "test_current":
452 return toModule("android_test_stubs_current")
453 case "core_current":
454 return toModule("core.current.stubs")
Colin Crossfc3674a2017-09-18 17:41:52 -0700455 default:
456 return toFile(v)
457 }
458}
459
Colin Crossbe1da472017-07-07 15:59:46 -0700460func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700461 if ctx.Device() {
462 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800463 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700464 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700465 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800466 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700467 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
468 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700469 if !proptools.Bool(j.properties.No_framework_libs) {
470 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
471 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700472 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800473 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700474 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
475 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700476 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800477 if Bool(j.deviceProperties.Optimize.Enabled) {
478 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
479 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
480 }
Colin Crossbe1da472017-07-07 15:59:46 -0700481 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700482 } else if j.deviceProperties.System_modules == nil {
483 ctx.PropertyErrorf("no_standard_libs",
484 "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 -0800485 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700486 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700487 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800488 if ctx.ModuleName() == "framework" {
489 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
490 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800491 if ctx.ModuleName() == "android_stubs_current" ||
492 ctx.ModuleName() == "android_system_stubs_current" ||
493 ctx.ModuleName() == "android_test_stubs_current" {
494 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
495 }
Colin Cross2fe66872015-03-30 17:20:39 -0700496 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700497
Colin Crossf506d872017-07-19 15:53:04 -0700498 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
499 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700500 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700501
502 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000503 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700504 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800505 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700506
507 if j.hasSrcExt(".proto") {
508 protoDeps(ctx, &j.protoProperties)
509 }
Colin Cross93e85952017-08-15 13:34:18 -0700510
511 if j.hasSrcExt(".kt") {
512 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
513 // Kotlin files
514 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
515 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800516
517 if j.shouldInstrumentStatic(ctx) {
518 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
519 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700520}
521
522func hasSrcExt(srcs []string, ext string) bool {
523 for _, src := range srcs {
524 if filepath.Ext(src) == ext {
525 return true
526 }
527 }
528
529 return false
530}
531
Nan Zhang61eaedb2017-11-02 13:28:15 -0700532func shardPaths(paths android.Paths, shardSize int) []android.Paths {
533 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
534 for len(paths) > shardSize {
535 ret = append(ret, paths[0:shardSize])
536 paths = paths[shardSize:]
537 }
538 if len(paths) > 0 {
539 ret = append(ret, paths)
540 }
541 return ret
542}
543
Colin Cross6af17aa2017-09-20 12:59:05 -0700544func (j *Module) hasSrcExt(ext string) bool {
545 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700546}
547
Colin Cross46c9b8b2017-06-22 16:51:17 -0700548func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700549 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700550
Colin Crossebe1a512017-11-14 13:12:14 -0800551 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
552 aidlIncludes = append(aidlIncludes,
553 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
554 aidlIncludes = append(aidlIncludes,
555 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700556
557 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700558 if aidlPreprocess.Valid() {
559 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700560 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700561 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700562 }
563
Colin Cross635c3b02016-05-18 15:37:25 -0700564 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800565 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700566 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800567 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700568 flags = append(flags, "-I"+src.String())
569 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700570
Martijn Coeneneab15642018-03-09 09:29:59 +0100571 if Bool(j.deviceProperties.Aidl.Generate_traces) {
572 flags = append(flags, "-t")
573 }
574
Colin Crossf03c82b2015-04-13 13:53:40 -0700575 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700576}
577
Colin Cross32f676a2017-09-06 13:41:06 -0700578type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800579 classpath classpath
580 bootClasspath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700581 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700582 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700583 staticJarResources android.Paths
584 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800585 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700586 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700587 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700588 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700589 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700590}
Colin Cross2fe66872015-03-30 17:20:39 -0700591
Colin Cross54250902017-12-05 09:28:08 -0800592func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
593 for _, f := range dep.Srcs() {
594 if f.Ext() != ".jar" {
595 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
596 ctx.OtherModuleName(dep.(blueprint.Module)))
597 }
598 }
599}
600
Jiyong Park2d492942018-03-05 17:44:10 +0900601type linkType int
602
603const (
604 javaCore linkType = iota
605 javaSdk
606 javaSystem
607 javaPlatform
608)
609
Colin Crossf19b9bb2018-03-26 14:42:44 -0700610func getLinkType(m *Module, name string) linkType {
Jiyong Park2d492942018-03-05 17:44:10 +0900611 ver := String(m.deviceProperties.Sdk_version)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700612 switch {
613 case name == "core.current.stubs" || ver == "core_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900614 return javaCore
Colin Crossf19b9bb2018-03-26 14:42:44 -0700615 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900616 return javaSystem
Colin Crossf19b9bb2018-03-26 14:42:44 -0700617 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900618 return javaPlatform
Colin Crossf19b9bb2018-03-26 14:42:44 -0700619 case name == "android_stubs_current" || ver == "current":
620 return javaSdk
621 case ver == "":
622 return javaPlatform
623 default:
624 if _, err := strconv.Atoi(ver); err != nil {
625 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
626 }
627 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900628 }
629}
630
Jiyong Park750e5572018-01-31 00:20:13 +0900631func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700632 if ctx.Host() {
633 return
634 }
635
636 myLinkType := getLinkType(from, ctx.ModuleName())
637 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900638 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."
639
640 switch myLinkType {
641 case javaCore:
642 if otherLinkType != javaCore {
643 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 +0900644 ctx.OtherModuleName(to))
645 }
Jiyong Park2d492942018-03-05 17:44:10 +0900646 break
647 case javaSdk:
648 if otherLinkType != javaCore && otherLinkType != javaSdk {
649 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
650 ctx.OtherModuleName(to))
651 }
652 break
653 case javaSystem:
654 if otherLinkType == javaPlatform {
655 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
656 ctx.OtherModuleName(to))
657 }
658 break
659 case javaPlatform:
660 // no restriction on link-type
661 break
Jiyong Park750e5572018-01-31 00:20:13 +0900662 }
663}
664
Colin Cross32f676a2017-09-06 13:41:06 -0700665func (j *Module) collectDeps(ctx android.ModuleContext) deps {
666 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700667
Colin Cross300f0382018-03-06 13:11:51 -0800668 if ctx.Device() {
669 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
670 if sdkDep.invalidVersion {
671 ctx.AddMissingDependencies([]string{sdkDep.module})
672 } else if sdkDep.useFiles {
673 // sdkDep.jar is actually equivalent to turbine header.jar.
674 deps.classpath = append(deps.classpath, sdkDep.jar)
675 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
676 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700677 }
678
Colin Crossd11fcda2017-10-23 17:59:01 -0700679 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700680 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700681 tag := ctx.OtherModuleDependencyTag(module)
682
Jiyong Park750e5572018-01-31 00:20:13 +0900683 if to, ok := module.(*Library); ok {
684 checkLinkType(ctx, j, to, tag.(dependencyTag))
685 }
Colin Cross54250902017-12-05 09:28:08 -0800686 switch dep := module.(type) {
687 case Dependency:
688 switch tag {
689 case bootClasspathTag:
690 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
691 case libTag:
692 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
693 case staticLibTag:
694 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
695 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
696 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
697 case frameworkResTag:
698 if ctx.ModuleName() == "framework" {
699 // framework.jar has a one-off dependency on the R.java and Manifest.java files
700 // generated by framework-res.apk
701 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
702 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800703 case frameworkApkTag:
704 if ctx.ModuleName() == "android_stubs_current" ||
705 ctx.ModuleName() == "android_system_stubs_current" ||
706 ctx.ModuleName() == "android_test_stubs_current" {
707 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
708 // resource files out of there for aapt.
709 //
710 // Normally the package rule runs aapt, which includes the resource,
711 // but we're not running that in our package rule so just copy in the
712 // resource files here.
713 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
714 }
Colin Cross54250902017-12-05 09:28:08 -0800715 case kotlinStdlibTag:
716 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800717 }
718
719 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
720 case android.SourceFileProducer:
721 switch tag {
722 case libTag:
723 checkProducesJars(ctx, dep)
724 deps.classpath = append(deps.classpath, dep.Srcs()...)
725 case staticLibTag:
726 checkProducesJars(ctx, dep)
727 deps.classpath = append(deps.classpath, dep.Srcs()...)
728 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
729 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
730 case android.DefaultsDepTag, android.SourceDepTag:
731 // Nothing to do
732 default:
733 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
734 }
735 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700736 switch tag {
737 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700738 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700739 case systemModulesTag:
740 if deps.systemModules != nil {
741 panic("Found two system module dependencies")
742 }
743 sm := module.(*SystemModules)
744 if sm.outputFile == nil {
745 panic("Missing directory for system module dependency")
746 }
747 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700748 default:
749 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700750 }
Colin Crossec7a0422017-07-07 14:47:12 -0700751 }
Colin Cross2fe66872015-03-30 17:20:39 -0700752 })
753
Colin Cross32f676a2017-09-06 13:41:06 -0700754 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700755}
756
Nan Zhanged19fc32017-10-19 13:06:22 -0700757func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700758
Colin Crossf03c82b2015-04-13 13:53:40 -0700759 var flags javaBuilderFlags
760
Nan Zhanged19fc32017-10-19 13:06:22 -0700761 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700762 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800763 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700764 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700765 }
Colin Cross6510f912017-11-29 00:27:14 -0800766 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700767 // Override the -g flag passed globally to remove local variable debug info to reduce
768 // disk and memory usage.
769 javacFlags = append(javacFlags, "-g:source,lines")
770 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700771 if len(javacFlags) > 0 {
772 // optimization.
773 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
774 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700775 }
Colin Cross64162712017-08-08 13:17:59 -0700776
Andreas Gampef3e5b552018-01-22 21:27:21 -0800777 if len(j.properties.Errorprone.Javacflags) > 0 {
778 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
779 }
780
Nan Zhanged19fc32017-10-19 13:06:22 -0700781 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800782 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700783 if j.properties.Java_version != nil {
784 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700785 } else if ctx.Device() && sdk <= 23 {
786 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800787 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700788 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900789 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700790 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
791 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700792 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700793 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700794 }
795
Nan Zhanged19fc32017-10-19 13:06:22 -0700796 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800797 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
798 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800799
800 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
801 !Bool(j.properties.No_standard_libs) &&
802 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
803 // Give host-side tools a version of OpenJDK's standard libraries
804 // close to what they're targeting. As of Dec 2017, AOSP is only
805 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
806 //
807 // When building with OpenJDK 8, the following should have no
808 // effect since those jars would be available by default.
809 //
810 // When building with OpenJDK 9 but targeting a version < 1.8,
811 // putting them on the bootclasspath means that:
812 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
813 // b) references to existing APIs are not reinterpreted in an
814 // OpenJDK 9-specific way, eg. calls to subclasses of
815 // java.nio.Buffer as in http://b/70862583
816 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
817 flags.bootClasspath = append(flags.bootClasspath,
818 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
819 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800820 if Bool(j.properties.Use_tools_jar) {
821 flags.bootClasspath = append(flags.bootClasspath,
822 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
823 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800824 }
825
Nan Zhanged19fc32017-10-19 13:06:22 -0700826 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700827 if deps.systemModules != nil {
828 flags.systemModules = append(flags.systemModules, deps.systemModules)
829 }
830
Nan Zhanged19fc32017-10-19 13:06:22 -0700831 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700832 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700833 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700834 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700835 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
836 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700837 }
838
Nan Zhanged19fc32017-10-19 13:06:22 -0700839 return flags
840}
Colin Crossc0b06f12015-04-08 13:03:43 -0700841
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800842func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700843
Colin Crossebe1a512017-11-14 13:12:14 -0800844 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700845
846 deps := j.collectDeps(ctx)
847 flags := j.collectBuilderFlags(ctx, deps)
848
Colin Cross6510f912017-11-29 00:27:14 -0800849 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700850 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
851 }
852 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700853 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800854 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700855 }
856
Colin Crossaf050172017-11-15 23:01:59 -0800857 srcFiles = j.genSources(ctx, srcFiles, flags)
858
859 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700860 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800861 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700862
Colin Cross0a6e0072017-08-30 14:24:55 -0700863 var jars android.Paths
864
Colin Cross1ee23172017-10-18 14:44:18 -0700865 jarName := ctx.ModuleName() + ".jar"
866
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000867 javaSrcFiles := srcFiles.FilterByExt(".java")
868 var uniqueSrcFiles android.Paths
869 set := make(map[string]bool)
870 for _, v := range javaSrcFiles {
871 if _, found := set[v.String()]; !found {
872 set[v.String()] = true
873 uniqueSrcFiles = append(uniqueSrcFiles, v)
874 }
875 }
876
Colin Cross93e85952017-08-15 13:34:18 -0700877 if srcFiles.HasExt(".kt") {
878 // If there are kotlin files, compile them first but pass all the kotlin and java files
879 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
880 // won't emit any classes for them.
881
882 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000883
Colin Cross93e85952017-08-15 13:34:18 -0700884 if ctx.Device() {
885 flags.kotlincFlags += " -no-jdk"
886 }
887
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000888 var kotlinSrcFiles android.Paths
889 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
890 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
891
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000892 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700893 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
894 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
895
Colin Cross1ee23172017-10-18 14:44:18 -0700896 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000897 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700898 if ctx.Failed() {
899 return
900 }
901
902 // Make javac rule depend on the kotlinc rule
903 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000904
Colin Cross93e85952017-08-15 13:34:18 -0700905 // Jar kotlin classes into the final jar after javac
906 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000907
908 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
909 // (it's expected to be on device bootclasspath)
910 if !proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
911 jars = append(jars, deps.kotlinStdlib...)
912 }
Colin Cross93e85952017-08-15 13:34:18 -0700913 }
914
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800915 // Store the list of .java files that was passed to javac
916 j.compiledJavaSrcs = uniqueSrcFiles
917 j.compiledSrcJars = srcJars
918
Nan Zhang61eaedb2017-11-02 13:28:15 -0700919 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800920 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700921 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
922 enable_sharding = true
923 if len(j.properties.Annotation_processors) != 0 ||
924 len(j.properties.Annotation_processor_classes) != 0 {
925 ctx.PropertyErrorf("javac_shard_size",
926 "%q cannot be set when annotation processors are enabled.",
927 j.properties.Javac_shard_size)
928 }
929 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700930 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
931 if ctx.Failed() {
932 return
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 }
934 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700935 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700936 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800937 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700938 // If error-prone is enabled, add an additional rule to compile the java files into
939 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700940 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700941 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
942 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700943 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700944 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700945 extraJarDeps = append(extraJarDeps, errorprone)
946 }
947
Nan Zhang61eaedb2017-11-02 13:28:15 -0700948 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800949 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700950 shardSize := int(*(j.properties.Javac_shard_size))
951 var shardSrcs []android.Paths
952 if len(uniqueSrcFiles) > 0 {
953 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
954 for idx, shardSrc := range shardSrcs {
955 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
956 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
957 jars = append(jars, classes)
958 }
959 }
960 if len(srcJars) > 0 {
961 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
962 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
963 jars = append(jars, classes)
964 }
965 } else {
966 classes := android.PathForModuleOut(ctx, "javac", jarName)
967 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
968 jars = append(jars, classes)
969 }
Colin Crossd6891432017-09-27 17:39:56 -0700970 if ctx.Failed() {
971 return
972 }
Colin Cross2fe66872015-03-30 17:20:39 -0700973 }
974
Colin Cross0f37af02017-09-27 17:42:05 -0700975 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
976 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
977
978 var resArgs []string
979 var resDeps android.Paths
980
981 resArgs = append(resArgs, dirArgs...)
982 resDeps = append(resDeps, dirDeps...)
983
984 resArgs = append(resArgs, fileArgs...)
985 resDeps = append(resDeps, fileDeps...)
986
987 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700988 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700989 resArgs = append(resArgs, srcArgs...)
990 resDeps = append(resDeps, srcDeps...)
991 }
Colin Cross40a36712017-09-27 17:41:35 -0700992
993 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700994 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700995 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700996 if ctx.Failed() {
997 return
998 }
Colin Cross20978302015-04-10 17:05:07 -0700999
Colin Cross0a6e0072017-08-30 14:24:55 -07001000 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -07001001 }
1002
Colin Cross6ade34f2017-09-15 13:00:47 -07001003 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -07001004 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001005 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001006
Colin Cross366938f2017-12-11 16:29:02 -08001007 var manifest android.OptionalPath
1008 if j.properties.Manifest != nil {
1009 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1010 }
Colin Cross635acc92017-09-12 22:50:46 -07001011
Colin Cross0a6e0072017-08-30 14:24:55 -07001012 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001013 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -07001014 var outputFile android.Path
1015
1016 if len(jars) == 1 && !manifest.Valid() {
1017 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -08001018 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1019 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1020 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -07001021 outputFile = jars[0]
1022 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001023 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -07001024 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001025 outputFile = combinedJar
1026 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001027
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001028 // Use renamed kotlin standard library?
1029 if srcFiles.HasExt(".kt") && proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
1030 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
1031 TransformJarJar(ctx, jarjarFile, outputFile,
1032 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
1033 outputFile = jarjarFile
1034 if ctx.Failed() {
1035 return
1036 }
1037 }
1038
Colin Cross0a6e0072017-08-30 14:24:55 -07001039 if j.properties.Jarjar_rules != nil {
1040 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001041 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001042 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001043 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1044 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -07001045 if ctx.Failed() {
1046 return
1047 }
1048 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001049 j.implementationJarFile = outputFile
1050 if j.headerJarFile == nil {
1051 j.headerJarFile = j.implementationJarFile
1052 }
Colin Cross2fe66872015-03-30 17:20:39 -07001053
Colin Cross6510f912017-11-29 00:27:14 -08001054 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001055 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1056 j.properties.Instrument = true
1057 }
1058 }
1059
Colin Cross3144dfc2018-01-03 15:06:47 -08001060 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001061 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1062 }
1063
1064 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001065 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001066 if ctx.Failed() {
1067 return
1068 }
Colin Cross2fe66872015-03-30 17:20:39 -07001069 }
Colin Crossb7a63242015-04-16 14:09:14 -07001070 ctx.CheckbuildFile(outputFile)
1071 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001072}
1073
Colin Cross8eadbf02017-10-24 17:46:00 -07001074func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001075 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1076
1077 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001078 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001079 // Compile java sources into turbine.jar.
1080 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1081 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1082 if ctx.Failed() {
1083 return nil
1084 }
1085 jars = append(jars, turbineJar)
1086 }
1087
1088 // Combine any static header libraries into classes-header.jar. If there is only
1089 // one input jar this step will be skipped.
1090 var headerJar android.Path
1091 jars = append(jars, deps.staticHeaderJars...)
1092
Colin Cross5c6ecc12017-10-23 18:12:27 -07001093 // we cannot skip the combine step for now if there is only one jar
1094 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1095 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1096 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1097 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001098
1099 if j.properties.Jarjar_rules != nil {
1100 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1101 // Transform classes.jar into classes-jarjar.jar
1102 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1103 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1104 headerJar = jarjarFile
1105 if ctx.Failed() {
1106 return nil
1107 }
1108 }
1109
1110 return headerJar
1111}
1112
Colin Crosscb933592017-11-22 13:49:43 -08001113func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1114 classesJar android.Path, jarName string) android.Path {
1115
Colin Cross7a3139e2017-12-19 13:57:50 -08001116 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001117
Colin Cross84c38822018-01-03 15:59:46 -08001118 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001119 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1120
1121 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1122
1123 j.jacocoReportClassesFile = jacocoReportClassesFile
1124
1125 return instrumentedJar
1126}
1127
Colin Crosscb933592017-11-22 13:49:43 -08001128// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1129// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1130func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1131 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001132 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001133 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001134 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001135 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001136 }
1137}
1138
Colin Cross59f1bb62017-09-27 17:59:10 -07001139func (j *Module) installable() bool {
1140 return j.properties.Installable == nil || *j.properties.Installable
1141}
1142
Colin Crossf506d872017-07-19 15:53:04 -07001143var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001144
Nan Zhanged19fc32017-10-19 13:06:22 -07001145func (j *Module) HeaderJars() android.Paths {
1146 return android.Paths{j.headerJarFile}
1147}
1148
1149func (j *Module) ImplementationJars() android.Paths {
1150 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001151}
1152
Colin Cross46c9b8b2017-06-22 16:51:17 -07001153func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001154 return j.exportAidlIncludeDirs
1155}
1156
Colin Cross46c9b8b2017-06-22 16:51:17 -07001157var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001158
Colin Cross46c9b8b2017-06-22 16:51:17 -07001159func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001160 return j.logtagsSrcs
1161}
1162
Colin Cross2fe66872015-03-30 17:20:39 -07001163//
1164// Java libraries (.jar file)
1165//
1166
Colin Crossf506d872017-07-19 15:53:04 -07001167type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001168 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001169}
1170
Colin Crossf506d872017-07-19 15:53:04 -07001171func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001172 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001173
Colin Cross59f1bb62017-09-27 17:59:10 -07001174 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001175 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1176 ctx.ModuleName()+".jar", j.outputFile)
1177 }
Colin Crossb7a63242015-04-16 14:09:14 -07001178}
1179
Colin Crossf506d872017-07-19 15:53:04 -07001180func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001181 j.deps(ctx)
1182}
1183
Colin Crossa60ead82017-10-02 18:10:21 -07001184func LibraryFactory(installable bool) func() android.Module {
1185 return func() android.Module {
1186 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001187
Colin Crossa60ead82017-10-02 18:10:21 -07001188 if !installable {
1189 module.properties.Installable = proptools.BoolPtr(false)
1190 }
Colin Cross2fe66872015-03-30 17:20:39 -07001191
Colin Crossa60ead82017-10-02 18:10:21 -07001192 module.AddProperties(
1193 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001194 &module.Module.deviceProperties,
1195 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001196
Colin Crossa60ead82017-10-02 18:10:21 -07001197 InitJavaModule(module, android.HostAndDeviceSupported)
1198 return module
1199 }
Colin Cross2fe66872015-03-30 17:20:39 -07001200}
1201
Colin Crossf506d872017-07-19 15:53:04 -07001202func LibraryHostFactory() android.Module {
1203 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001204
Colin Cross6af17aa2017-09-20 12:59:05 -07001205 module.AddProperties(
1206 &module.Module.properties,
1207 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001208
Colin Cross89536d42017-07-07 14:35:50 -07001209 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001210 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001211}
1212
1213//
Colin Cross05638fc2018-04-09 18:40:24 -07001214// Java Junit Tests
1215//
1216
1217type testProperties struct {
1218 // If true, add a static dependency on the platform junit library. Defaults to true.
1219 Junit *bool
1220
1221 // list of compatibility suites (for example "cts", "vts") that the module should be
1222 // installed into.
1223 Test_suites []string `android:"arch_variant"`
1224}
1225
1226type Test struct {
1227 Library
1228
1229 testProperties testProperties
1230}
1231
1232func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1233 j.deps(ctx)
1234 if j.testProperties.Junit == nil || *j.testProperties.Junit == true {
1235 ctx.AddDependency(ctx.Module(), staticLibTag, "junit")
1236 }
1237}
1238
1239func TestFactory() android.Module {
1240 module := &Test{}
1241
1242 module.AddProperties(
1243 &module.Module.properties,
1244 &module.Module.deviceProperties,
1245 &module.Module.protoProperties,
1246 &module.testProperties)
1247
1248 InitJavaModule(module, android.HostAndDeviceSupported)
1249 android.InitDefaultableModule(module)
1250 return module
1251}
1252
1253func TestHostFactory() android.Module {
1254 module := &Test{}
1255
1256 module.AddProperties(
1257 &module.Module.properties,
1258 &module.Module.protoProperties,
1259 &module.testProperties)
1260
1261 InitJavaModule(module, android.HostSupported)
1262 android.InitDefaultableModule(module)
1263 return module
1264}
1265
1266//
Colin Cross2fe66872015-03-30 17:20:39 -07001267// Java Binaries (.jar file plus wrapper script)
1268//
1269
Colin Crossf506d872017-07-19 15:53:04 -07001270type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001271 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001272 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001273}
1274
Colin Crossf506d872017-07-19 15:53:04 -07001275type Binary struct {
1276 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001277
Colin Crossf506d872017-07-19 15:53:04 -07001278 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001279
Colin Cross6b4a32d2017-12-05 13:42:45 -08001280 isWrapperVariant bool
1281
Colin Crossc3315992017-12-08 19:12:36 -08001282 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001283 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001284}
1285
Alex Light24237172017-10-26 09:46:21 -07001286func (j *Binary) HostToolPath() android.OptionalPath {
1287 return android.OptionalPathForPath(j.binaryFile)
1288}
1289
Colin Crossf506d872017-07-19 15:53:04 -07001290func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001291 if ctx.Arch().ArchType == android.Common {
1292 // Compile the jar
1293 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001294 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001295 // Handle the binary wrapper
1296 j.isWrapperVariant = true
1297
Colin Cross366938f2017-12-11 16:29:02 -08001298 if j.binaryProperties.Wrapper != nil {
1299 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001300 } else {
1301 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1302 }
1303
1304 // Depend on the installed jar so that the wrapper doesn't get executed by
1305 // another build rule before the jar has been installed.
1306 jarFile := ctx.PrimaryModule().(*Binary).installFile
1307
1308 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1309 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001310 }
Colin Cross2fe66872015-03-30 17:20:39 -07001311}
1312
Colin Crossf506d872017-07-19 15:53:04 -07001313func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001314 if ctx.Arch().ArchType == android.Common {
1315 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001316 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001317 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001318 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001319}
1320
Colin Crossf506d872017-07-19 15:53:04 -07001321func BinaryFactory() android.Module {
1322 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001323
Colin Cross36242852017-06-23 15:06:31 -07001324 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001325 &module.Module.properties,
1326 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001327 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001328 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001329
Colin Cross6b4a32d2017-12-05 13:42:45 -08001330 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1331 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001332 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001333}
1334
Colin Crossf506d872017-07-19 15:53:04 -07001335func BinaryHostFactory() android.Module {
1336 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001337
Colin Cross36242852017-06-23 15:06:31 -07001338 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001339 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001340 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001341 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001342
Colin Cross6b4a32d2017-12-05 13:42:45 -08001343 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1344 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001345 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001346}
1347
1348//
1349// Java prebuilts
1350//
1351
Colin Cross74d73e22017-08-02 11:05:49 -07001352type ImportProperties struct {
1353 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001354
Nan Zhangea568a42017-11-08 21:20:04 -08001355 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001356
1357 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001358}
1359
1360type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001361 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001362 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001363
Colin Cross74d73e22017-08-02 11:05:49 -07001364 properties ImportProperties
1365
Colin Cross0a6e0072017-08-30 14:24:55 -07001366 classpathFiles android.Paths
1367 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001368}
1369
Colin Cross74d73e22017-08-02 11:05:49 -07001370func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001371 return &j.prebuilt
1372}
1373
Colin Cross74d73e22017-08-02 11:05:49 -07001374func (j *Import) PrebuiltSrcs() []string {
1375 return j.properties.Jars
1376}
1377
1378func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001379 return j.prebuilt.Name(j.ModuleBase.Name())
1380}
1381
Colin Cross74d73e22017-08-02 11:05:49 -07001382func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001383}
1384
Colin Cross74d73e22017-08-02 11:05:49 -07001385func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1386 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001387
Colin Crosse9a275b2017-10-16 17:09:48 -07001388 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001389 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001390 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001391}
1392
Colin Cross74d73e22017-08-02 11:05:49 -07001393var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001394
Nan Zhanged19fc32017-10-19 13:06:22 -07001395func (j *Import) HeaderJars() android.Paths {
1396 return j.classpathFiles
1397}
1398
1399func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001400 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001401}
1402
Colin Cross74d73e22017-08-02 11:05:49 -07001403func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001404 return nil
1405}
1406
Colin Cross74d73e22017-08-02 11:05:49 -07001407var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001408
Colin Cross74d73e22017-08-02 11:05:49 -07001409func ImportFactory() android.Module {
1410 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001411
Colin Cross74d73e22017-08-02 11:05:49 -07001412 module.AddProperties(&module.properties)
1413
1414 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001415 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1416 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001417}
1418
Colin Cross74d73e22017-08-02 11:05:49 -07001419func ImportFactoryHost() android.Module {
1420 module := &Import{}
1421
1422 module.AddProperties(&module.properties)
1423
1424 android.InitPrebuiltModule(module, &module.properties.Jars)
1425 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1426 return module
1427}
1428
Colin Cross89536d42017-07-07 14:35:50 -07001429//
1430// Defaults
1431//
1432type Defaults struct {
1433 android.ModuleBase
1434 android.DefaultsModuleBase
1435}
1436
1437func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1438}
1439
1440func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1441}
1442
1443func defaultsFactory() android.Module {
1444 return DefaultsFactory()
1445}
1446
1447func DefaultsFactory(props ...interface{}) android.Module {
1448 module := &Defaults{}
1449
1450 module.AddProperties(props...)
1451 module.AddProperties(
1452 &CompilerProperties{},
1453 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001454 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001455 )
1456
1457 android.InitDefaultsModule(module)
1458
1459 return module
1460}
Nan Zhangea568a42017-11-08 21:20:04 -08001461
1462var Bool = proptools.Bool
1463var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001464var inList = android.InList