blob: bc58df3ad177f9050fae63fedccf8b9932dc27bd [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
Colin Crossa97c5d32018-03-28 14:58:31 -0700337 frameworkResModule string
338
Colin Cross1369cdb2017-09-29 17:58:17 -0700339 jar android.Path
340 aidl android.Path
341}
342
343func sdkStringToNumber(ctx android.BaseContext, v string) int {
344 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900345 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900346 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700347 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900348 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700349 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
350 return -1
351 } else {
352 return i
353 }
354 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700355}
356
Colin Cross3144dfc2018-01-03 15:06:47 -0800357func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
358 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
359}
360
361func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
362 return j.shouldInstrument(ctx) &&
363 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
364 ctx.Config().UnbundledBuild())
365}
366
Colin Crossfc3674a2017-09-18 17:41:52 -0700367func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700368 i := sdkStringToNumber(ctx, v)
369 if i == -1 {
370 // Invalid sdk version, error handled by sdkStringToNumber.
371 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700372 }
373
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900374 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
375 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
376 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
377 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
378 if ctx.DeviceSpecific() || ctx.SocSpecific() {
379 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
380 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
381 }
382 }
383 version := strings.TrimPrefix(v, "system_")
384 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
385 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
386 v, allowed_versions)
387 }
388 }
389
Colin Crossfc3674a2017-09-18 17:41:52 -0700390 toFile := func(v string) sdkDep {
Jiyong Park750e5572018-01-31 00:20:13 +0900391 isCore := strings.HasPrefix(v, "core_")
392 if isCore {
393 v = strings.TrimPrefix(v, "core_")
394 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700395 dir := filepath.Join("prebuilts/sdk", v)
396 jar := filepath.Join(dir, "android.jar")
Jiyong Park750e5572018-01-31 00:20:13 +0900397 if isCore {
398 jar = filepath.Join(dir, "core.jar")
399 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700400 aidl := filepath.Join(dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800401 jarPath := android.ExistentPathForSource(ctx, jar)
402 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700403
Colin Cross6510f912017-11-29 00:27:14 -0800404 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700405 return sdkDep{
406 invalidVersion: true,
407 module: "sdk_v" + v,
408 }
409 }
410
Colin Crossfc3674a2017-09-18 17:41:52 -0700411 if !jarPath.Valid() {
412 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
413 return sdkDep{}
414 }
Colin Cross47ff2522017-10-02 14:22:08 -0700415
Colin Crossfc3674a2017-09-18 17:41:52 -0700416 if !aidlPath.Valid() {
417 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
418 return sdkDep{}
419 }
Colin Cross47ff2522017-10-02 14:22:08 -0700420
Colin Crossfc3674a2017-09-18 17:41:52 -0700421 return sdkDep{
422 useFiles: true,
423 jar: jarPath.Path(),
424 aidl: aidlPath.Path(),
425 }
426 }
427
Colin Crossa97c5d32018-03-28 14:58:31 -0700428 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700429 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700430 useModule: true,
431 module: m,
432 systemModules: m + "_system_modules",
433 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700434 }
435 if m == "core.current.stubs" {
436 ret.systemModules = "core-system-modules"
437 }
438 return ret
439 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700440
Colin Cross6510f912017-11-29 00:27:14 -0800441 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700442 return toFile(v)
443 }
444
445 switch v {
446 case "":
447 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700448 useDefaultLibs: true,
449 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700450 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700451 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700452 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700453 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700454 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700455 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700456 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700457 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700458 return toModule("core.current.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700459 default:
460 return toFile(v)
461 }
462}
463
Colin Crossbe1da472017-07-07 15:59:46 -0700464func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700465 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700466 if !Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800467 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700468 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700469 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800470 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700471 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
472 }
Colin Crossff3ae9d2018-04-10 16:15:18 -0700473 if !Bool(j.properties.No_framework_libs) {
Colin Crossfa5eb232017-10-01 20:33:03 -0700474 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
475 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700476 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800477 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700478 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
479 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700480 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800481 if Bool(j.deviceProperties.Optimize.Enabled) {
482 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
483 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
484 }
Colin Crossbe1da472017-07-07 15:59:46 -0700485 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700486 } else if j.deviceProperties.System_modules == nil {
487 ctx.PropertyErrorf("no_standard_libs",
488 "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 -0800489 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700490 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700491 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800492 if ctx.ModuleName() == "framework" {
493 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
494 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800495 if ctx.ModuleName() == "android_stubs_current" ||
496 ctx.ModuleName() == "android_system_stubs_current" ||
497 ctx.ModuleName() == "android_test_stubs_current" {
498 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
499 }
Colin Cross2fe66872015-03-30 17:20:39 -0700500 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700501
Colin Crossf506d872017-07-19 15:53:04 -0700502 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
503 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700504 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700505
506 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000507 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700508 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800509 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700510
511 if j.hasSrcExt(".proto") {
512 protoDeps(ctx, &j.protoProperties)
513 }
Colin Cross93e85952017-08-15 13:34:18 -0700514
515 if j.hasSrcExt(".kt") {
516 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
517 // Kotlin files
518 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
519 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800520
521 if j.shouldInstrumentStatic(ctx) {
522 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
523 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700524}
525
526func hasSrcExt(srcs []string, ext string) bool {
527 for _, src := range srcs {
528 if filepath.Ext(src) == ext {
529 return true
530 }
531 }
532
533 return false
534}
535
Nan Zhang61eaedb2017-11-02 13:28:15 -0700536func shardPaths(paths android.Paths, shardSize int) []android.Paths {
537 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
538 for len(paths) > shardSize {
539 ret = append(ret, paths[0:shardSize])
540 paths = paths[shardSize:]
541 }
542 if len(paths) > 0 {
543 ret = append(ret, paths)
544 }
545 return ret
546}
547
Colin Cross6af17aa2017-09-20 12:59:05 -0700548func (j *Module) hasSrcExt(ext string) bool {
549 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700550}
551
Colin Cross46c9b8b2017-06-22 16:51:17 -0700552func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700553 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700554
Colin Crossebe1a512017-11-14 13:12:14 -0800555 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
556 aidlIncludes = append(aidlIncludes,
557 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
558 aidlIncludes = append(aidlIncludes,
559 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700560
561 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700562 if aidlPreprocess.Valid() {
563 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700564 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700565 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700566 }
567
Colin Cross635c3b02016-05-18 15:37:25 -0700568 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800569 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700570 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800571 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700572 flags = append(flags, "-I"+src.String())
573 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700574
Martijn Coeneneab15642018-03-09 09:29:59 +0100575 if Bool(j.deviceProperties.Aidl.Generate_traces) {
576 flags = append(flags, "-t")
577 }
578
Colin Crossf03c82b2015-04-13 13:53:40 -0700579 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700580}
581
Colin Cross32f676a2017-09-06 13:41:06 -0700582type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800583 classpath classpath
584 bootClasspath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700585 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700586 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700587 staticJarResources android.Paths
588 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800589 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700590 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700591 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700592 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700593 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700594}
Colin Cross2fe66872015-03-30 17:20:39 -0700595
Colin Cross54250902017-12-05 09:28:08 -0800596func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
597 for _, f := range dep.Srcs() {
598 if f.Ext() != ".jar" {
599 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
600 ctx.OtherModuleName(dep.(blueprint.Module)))
601 }
602 }
603}
604
Jiyong Park2d492942018-03-05 17:44:10 +0900605type linkType int
606
607const (
608 javaCore linkType = iota
609 javaSdk
610 javaSystem
611 javaPlatform
612)
613
Colin Crossf19b9bb2018-03-26 14:42:44 -0700614func getLinkType(m *Module, name string) linkType {
Jiyong Park2d492942018-03-05 17:44:10 +0900615 ver := String(m.deviceProperties.Sdk_version)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700616 switch {
617 case name == "core.current.stubs" || ver == "core_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900618 return javaCore
Colin Crossf19b9bb2018-03-26 14:42:44 -0700619 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900620 return javaSystem
Colin Crossf19b9bb2018-03-26 14:42:44 -0700621 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900622 return javaPlatform
Colin Crossf19b9bb2018-03-26 14:42:44 -0700623 case name == "android_stubs_current" || ver == "current":
624 return javaSdk
625 case ver == "":
626 return javaPlatform
627 default:
628 if _, err := strconv.Atoi(ver); err != nil {
629 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
630 }
631 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900632 }
633}
634
Jiyong Park750e5572018-01-31 00:20:13 +0900635func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700636 if ctx.Host() {
637 return
638 }
639
640 myLinkType := getLinkType(from, ctx.ModuleName())
641 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900642 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."
643
644 switch myLinkType {
645 case javaCore:
646 if otherLinkType != javaCore {
647 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 +0900648 ctx.OtherModuleName(to))
649 }
Jiyong Park2d492942018-03-05 17:44:10 +0900650 break
651 case javaSdk:
652 if otherLinkType != javaCore && otherLinkType != javaSdk {
653 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
654 ctx.OtherModuleName(to))
655 }
656 break
657 case javaSystem:
658 if otherLinkType == javaPlatform {
659 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
660 ctx.OtherModuleName(to))
661 }
662 break
663 case javaPlatform:
664 // no restriction on link-type
665 break
Jiyong Park750e5572018-01-31 00:20:13 +0900666 }
667}
668
Colin Cross32f676a2017-09-06 13:41:06 -0700669func (j *Module) collectDeps(ctx android.ModuleContext) deps {
670 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700671
Colin Cross300f0382018-03-06 13:11:51 -0800672 if ctx.Device() {
673 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
674 if sdkDep.invalidVersion {
675 ctx.AddMissingDependencies([]string{sdkDep.module})
676 } else if sdkDep.useFiles {
677 // sdkDep.jar is actually equivalent to turbine header.jar.
678 deps.classpath = append(deps.classpath, sdkDep.jar)
679 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
680 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700681 }
682
Colin Crossd11fcda2017-10-23 17:59:01 -0700683 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700684 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700685 tag := ctx.OtherModuleDependencyTag(module)
686
Jiyong Park750e5572018-01-31 00:20:13 +0900687 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700688 switch tag {
689 case bootClasspathTag, libTag, staticLibTag:
690 checkLinkType(ctx, j, to, tag.(dependencyTag))
691 }
Jiyong Park750e5572018-01-31 00:20:13 +0900692 }
Colin Cross54250902017-12-05 09:28:08 -0800693 switch dep := module.(type) {
694 case Dependency:
695 switch tag {
696 case bootClasspathTag:
697 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
698 case libTag:
699 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
700 case staticLibTag:
701 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
702 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
703 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
704 case frameworkResTag:
705 if ctx.ModuleName() == "framework" {
706 // framework.jar has a one-off dependency on the R.java and Manifest.java files
707 // generated by framework-res.apk
708 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
709 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800710 case frameworkApkTag:
711 if ctx.ModuleName() == "android_stubs_current" ||
712 ctx.ModuleName() == "android_system_stubs_current" ||
713 ctx.ModuleName() == "android_test_stubs_current" {
714 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
715 // resource files out of there for aapt.
716 //
717 // Normally the package rule runs aapt, which includes the resource,
718 // but we're not running that in our package rule so just copy in the
719 // resource files here.
720 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
721 }
Colin Cross54250902017-12-05 09:28:08 -0800722 case kotlinStdlibTag:
723 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800724 }
725
726 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
727 case android.SourceFileProducer:
728 switch tag {
729 case libTag:
730 checkProducesJars(ctx, dep)
731 deps.classpath = append(deps.classpath, dep.Srcs()...)
732 case staticLibTag:
733 checkProducesJars(ctx, dep)
734 deps.classpath = append(deps.classpath, dep.Srcs()...)
735 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
736 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
737 case android.DefaultsDepTag, android.SourceDepTag:
738 // Nothing to do
739 default:
740 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
741 }
742 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700743 switch tag {
744 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700745 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700746 case systemModulesTag:
747 if deps.systemModules != nil {
748 panic("Found two system module dependencies")
749 }
750 sm := module.(*SystemModules)
751 if sm.outputFile == nil {
752 panic("Missing directory for system module dependency")
753 }
754 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700755 default:
756 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700757 }
Colin Crossec7a0422017-07-07 14:47:12 -0700758 }
Colin Cross2fe66872015-03-30 17:20:39 -0700759 })
760
Colin Cross32f676a2017-09-06 13:41:06 -0700761 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700762}
763
Nan Zhanged19fc32017-10-19 13:06:22 -0700764func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700765
Colin Crossf03c82b2015-04-13 13:53:40 -0700766 var flags javaBuilderFlags
767
Nan Zhanged19fc32017-10-19 13:06:22 -0700768 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700769 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800770 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700771 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700772 }
Colin Cross6510f912017-11-29 00:27:14 -0800773 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700774 // Override the -g flag passed globally to remove local variable debug info to reduce
775 // disk and memory usage.
776 javacFlags = append(javacFlags, "-g:source,lines")
777 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700778 if len(javacFlags) > 0 {
779 // optimization.
780 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
781 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700782 }
Colin Cross64162712017-08-08 13:17:59 -0700783
Andreas Gampef3e5b552018-01-22 21:27:21 -0800784 if len(j.properties.Errorprone.Javacflags) > 0 {
785 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
786 }
787
Nan Zhanged19fc32017-10-19 13:06:22 -0700788 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800789 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700790 if j.properties.Java_version != nil {
791 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700792 } else if ctx.Device() && sdk <= 23 {
793 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800794 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700795 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900796 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700797 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
798 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700799 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700800 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700801 }
802
Nan Zhanged19fc32017-10-19 13:06:22 -0700803 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800804 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
805 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800806
807 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
808 !Bool(j.properties.No_standard_libs) &&
809 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
810 // Give host-side tools a version of OpenJDK's standard libraries
811 // close to what they're targeting. As of Dec 2017, AOSP is only
812 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
813 //
814 // When building with OpenJDK 8, the following should have no
815 // effect since those jars would be available by default.
816 //
817 // When building with OpenJDK 9 but targeting a version < 1.8,
818 // putting them on the bootclasspath means that:
819 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
820 // b) references to existing APIs are not reinterpreted in an
821 // OpenJDK 9-specific way, eg. calls to subclasses of
822 // java.nio.Buffer as in http://b/70862583
823 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
824 flags.bootClasspath = append(flags.bootClasspath,
825 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
826 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800827 if Bool(j.properties.Use_tools_jar) {
828 flags.bootClasspath = append(flags.bootClasspath,
829 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
830 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800831 }
832
Nan Zhanged19fc32017-10-19 13:06:22 -0700833 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700834 if deps.systemModules != nil {
835 flags.systemModules = append(flags.systemModules, deps.systemModules)
836 }
837
Nan Zhanged19fc32017-10-19 13:06:22 -0700838 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700839 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700840 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700841 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700842 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
843 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700844 }
845
Nan Zhanged19fc32017-10-19 13:06:22 -0700846 return flags
847}
Colin Crossc0b06f12015-04-08 13:03:43 -0700848
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800849func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700850
Colin Crossebe1a512017-11-14 13:12:14 -0800851 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700852
853 deps := j.collectDeps(ctx)
854 flags := j.collectBuilderFlags(ctx, deps)
855
Colin Cross6510f912017-11-29 00:27:14 -0800856 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700857 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
858 }
859 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700860 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800861 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700862 }
863
Colin Crossaf050172017-11-15 23:01:59 -0800864 srcFiles = j.genSources(ctx, srcFiles, flags)
865
866 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700867 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800868 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700869
Colin Cross0a6e0072017-08-30 14:24:55 -0700870 var jars android.Paths
871
Colin Cross1ee23172017-10-18 14:44:18 -0700872 jarName := ctx.ModuleName() + ".jar"
873
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000874 javaSrcFiles := srcFiles.FilterByExt(".java")
875 var uniqueSrcFiles android.Paths
876 set := make(map[string]bool)
877 for _, v := range javaSrcFiles {
878 if _, found := set[v.String()]; !found {
879 set[v.String()] = true
880 uniqueSrcFiles = append(uniqueSrcFiles, v)
881 }
882 }
883
Colin Cross93e85952017-08-15 13:34:18 -0700884 if srcFiles.HasExt(".kt") {
885 // If there are kotlin files, compile them first but pass all the kotlin and java files
886 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
887 // won't emit any classes for them.
888
889 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000890
Colin Cross93e85952017-08-15 13:34:18 -0700891 if ctx.Device() {
892 flags.kotlincFlags += " -no-jdk"
893 }
894
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000895 var kotlinSrcFiles android.Paths
896 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
897 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
898
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000899 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700900 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
901 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
902
Colin Cross1ee23172017-10-18 14:44:18 -0700903 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000904 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700905 if ctx.Failed() {
906 return
907 }
908
909 // Make javac rule depend on the kotlinc rule
910 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000911
Colin Cross93e85952017-08-15 13:34:18 -0700912 // Jar kotlin classes into the final jar after javac
913 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000914
915 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
916 // (it's expected to be on device bootclasspath)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700917 if !Bool(j.properties.Renamed_kotlin_stdlib) {
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000918 jars = append(jars, deps.kotlinStdlib...)
919 }
Colin Cross93e85952017-08-15 13:34:18 -0700920 }
921
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800922 // Store the list of .java files that was passed to javac
923 j.compiledJavaSrcs = uniqueSrcFiles
924 j.compiledSrcJars = srcJars
925
Nan Zhang61eaedb2017-11-02 13:28:15 -0700926 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800927 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700928 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
929 enable_sharding = true
930 if len(j.properties.Annotation_processors) != 0 ||
931 len(j.properties.Annotation_processor_classes) != 0 {
932 ctx.PropertyErrorf("javac_shard_size",
933 "%q cannot be set when annotation processors are enabled.",
934 j.properties.Javac_shard_size)
935 }
936 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700937 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
938 if ctx.Failed() {
939 return
Nan Zhanged19fc32017-10-19 13:06:22 -0700940 }
941 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700942 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700943 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800944 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700945 // If error-prone is enabled, add an additional rule to compile the java files into
946 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700947 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700948 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
949 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700950 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700951 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700952 extraJarDeps = append(extraJarDeps, errorprone)
953 }
954
Nan Zhang61eaedb2017-11-02 13:28:15 -0700955 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800956 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700957 shardSize := int(*(j.properties.Javac_shard_size))
958 var shardSrcs []android.Paths
959 if len(uniqueSrcFiles) > 0 {
960 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
961 for idx, shardSrc := range shardSrcs {
962 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
963 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
964 jars = append(jars, classes)
965 }
966 }
967 if len(srcJars) > 0 {
968 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
969 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
970 jars = append(jars, classes)
971 }
972 } else {
973 classes := android.PathForModuleOut(ctx, "javac", jarName)
974 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
975 jars = append(jars, classes)
976 }
Colin Crossd6891432017-09-27 17:39:56 -0700977 if ctx.Failed() {
978 return
979 }
Colin Cross2fe66872015-03-30 17:20:39 -0700980 }
981
Colin Cross0f37af02017-09-27 17:42:05 -0700982 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
983 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
984
985 var resArgs []string
986 var resDeps android.Paths
987
988 resArgs = append(resArgs, dirArgs...)
989 resDeps = append(resDeps, dirDeps...)
990
991 resArgs = append(resArgs, fileArgs...)
992 resDeps = append(resDeps, fileDeps...)
993
Colin Crossff3ae9d2018-04-10 16:15:18 -0700994 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700995 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700996 resArgs = append(resArgs, srcArgs...)
997 resDeps = append(resDeps, srcDeps...)
998 }
Colin Cross40a36712017-09-27 17:41:35 -0700999
1000 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001001 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001002 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -07001003 if ctx.Failed() {
1004 return
1005 }
Colin Cross20978302015-04-10 17:05:07 -07001006
Colin Cross0a6e0072017-08-30 14:24:55 -07001007 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -07001008 }
1009
Colin Cross6ade34f2017-09-15 13:00:47 -07001010 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -07001011 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001012 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001013
Colin Cross366938f2017-12-11 16:29:02 -08001014 var manifest android.OptionalPath
1015 if j.properties.Manifest != nil {
1016 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1017 }
Colin Cross635acc92017-09-12 22:50:46 -07001018
Colin Cross0a6e0072017-08-30 14:24:55 -07001019 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001020 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -07001021 var outputFile android.Path
1022
1023 if len(jars) == 1 && !manifest.Valid() {
1024 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -08001025 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1026 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1027 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -07001028 outputFile = jars[0]
1029 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001030 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -07001031 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001032 outputFile = combinedJar
1033 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001034
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001035 // Use renamed kotlin standard library?
Colin Crossff3ae9d2018-04-10 16:15:18 -07001036 if srcFiles.HasExt(".kt") && Bool(j.properties.Renamed_kotlin_stdlib) {
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001037 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
1038 TransformJarJar(ctx, jarjarFile, outputFile,
1039 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
1040 outputFile = jarjarFile
1041 if ctx.Failed() {
1042 return
1043 }
1044 }
1045
Colin Cross0a6e0072017-08-30 14:24:55 -07001046 if j.properties.Jarjar_rules != nil {
1047 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001048 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001049 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001050 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1051 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -07001052 if ctx.Failed() {
1053 return
1054 }
1055 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001056 j.implementationJarFile = outputFile
1057 if j.headerJarFile == nil {
1058 j.headerJarFile = j.implementationJarFile
1059 }
Colin Cross2fe66872015-03-30 17:20:39 -07001060
Colin Cross6510f912017-11-29 00:27:14 -08001061 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001062 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1063 j.properties.Instrument = true
1064 }
1065 }
1066
Colin Cross3144dfc2018-01-03 15:06:47 -08001067 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001068 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1069 }
1070
1071 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001072 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001073 if ctx.Failed() {
1074 return
1075 }
Colin Cross2fe66872015-03-30 17:20:39 -07001076 }
Colin Crossb7a63242015-04-16 14:09:14 -07001077 ctx.CheckbuildFile(outputFile)
1078 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001079}
1080
Colin Cross8eadbf02017-10-24 17:46:00 -07001081func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001082 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1083
1084 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001085 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001086 // Compile java sources into turbine.jar.
1087 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1088 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1089 if ctx.Failed() {
1090 return nil
1091 }
1092 jars = append(jars, turbineJar)
1093 }
1094
1095 // Combine any static header libraries into classes-header.jar. If there is only
1096 // one input jar this step will be skipped.
1097 var headerJar android.Path
1098 jars = append(jars, deps.staticHeaderJars...)
1099
Colin Cross5c6ecc12017-10-23 18:12:27 -07001100 // we cannot skip the combine step for now if there is only one jar
1101 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1102 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1103 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1104 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001105
1106 if j.properties.Jarjar_rules != nil {
1107 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1108 // Transform classes.jar into classes-jarjar.jar
1109 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1110 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1111 headerJar = jarjarFile
1112 if ctx.Failed() {
1113 return nil
1114 }
1115 }
1116
1117 return headerJar
1118}
1119
Colin Crosscb933592017-11-22 13:49:43 -08001120func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1121 classesJar android.Path, jarName string) android.Path {
1122
Colin Cross7a3139e2017-12-19 13:57:50 -08001123 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001124
Colin Cross84c38822018-01-03 15:59:46 -08001125 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001126 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1127
1128 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1129
1130 j.jacocoReportClassesFile = jacocoReportClassesFile
1131
1132 return instrumentedJar
1133}
1134
Colin Crosscb933592017-11-22 13:49:43 -08001135// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1136// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1137func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1138 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001139 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001140 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001141 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001142 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001143 }
1144}
1145
Colin Cross59f1bb62017-09-27 17:59:10 -07001146func (j *Module) installable() bool {
Colin Cross38b40df2018-04-10 16:14:46 -07001147 return BoolDefault(j.properties.Installable, true)
Colin Cross59f1bb62017-09-27 17:59:10 -07001148}
1149
Colin Crossf506d872017-07-19 15:53:04 -07001150var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001151
Nan Zhanged19fc32017-10-19 13:06:22 -07001152func (j *Module) HeaderJars() android.Paths {
1153 return android.Paths{j.headerJarFile}
1154}
1155
1156func (j *Module) ImplementationJars() android.Paths {
1157 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001158}
1159
Colin Cross46c9b8b2017-06-22 16:51:17 -07001160func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001161 return j.exportAidlIncludeDirs
1162}
1163
Colin Cross46c9b8b2017-06-22 16:51:17 -07001164var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001165
Colin Cross46c9b8b2017-06-22 16:51:17 -07001166func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001167 return j.logtagsSrcs
1168}
1169
Colin Cross2fe66872015-03-30 17:20:39 -07001170//
1171// Java libraries (.jar file)
1172//
1173
Colin Crossf506d872017-07-19 15:53:04 -07001174type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001175 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001176}
1177
Colin Crossf506d872017-07-19 15:53:04 -07001178func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001179 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001180
Colin Cross59f1bb62017-09-27 17:59:10 -07001181 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001182 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1183 ctx.ModuleName()+".jar", j.outputFile)
1184 }
Colin Crossb7a63242015-04-16 14:09:14 -07001185}
1186
Colin Crossf506d872017-07-19 15:53:04 -07001187func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001188 j.deps(ctx)
1189}
1190
Colin Crossa60ead82017-10-02 18:10:21 -07001191func LibraryFactory(installable bool) func() android.Module {
1192 return func() android.Module {
1193 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001194
Colin Crossa60ead82017-10-02 18:10:21 -07001195 if !installable {
1196 module.properties.Installable = proptools.BoolPtr(false)
1197 }
Colin Cross2fe66872015-03-30 17:20:39 -07001198
Colin Crossa60ead82017-10-02 18:10:21 -07001199 module.AddProperties(
1200 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001201 &module.Module.deviceProperties,
1202 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001203
Colin Crossa60ead82017-10-02 18:10:21 -07001204 InitJavaModule(module, android.HostAndDeviceSupported)
1205 return module
1206 }
Colin Cross2fe66872015-03-30 17:20:39 -07001207}
1208
Colin Crossf506d872017-07-19 15:53:04 -07001209func LibraryHostFactory() android.Module {
1210 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001211
Colin Cross6af17aa2017-09-20 12:59:05 -07001212 module.AddProperties(
1213 &module.Module.properties,
1214 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001215
Colin Cross89536d42017-07-07 14:35:50 -07001216 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001217 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001218}
1219
1220//
Colin Cross05638fc2018-04-09 18:40:24 -07001221// Java Junit Tests
1222//
1223
1224type testProperties struct {
1225 // If true, add a static dependency on the platform junit library. Defaults to true.
1226 Junit *bool
1227
1228 // list of compatibility suites (for example "cts", "vts") that the module should be
1229 // installed into.
1230 Test_suites []string `android:"arch_variant"`
1231}
1232
1233type Test struct {
1234 Library
1235
1236 testProperties testProperties
1237}
1238
1239func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1240 j.deps(ctx)
Colin Cross38b40df2018-04-10 16:14:46 -07001241 if BoolDefault(j.testProperties.Junit, true) {
Colin Cross05638fc2018-04-09 18:40:24 -07001242 ctx.AddDependency(ctx.Module(), staticLibTag, "junit")
1243 }
1244}
1245
1246func TestFactory() android.Module {
1247 module := &Test{}
1248
1249 module.AddProperties(
1250 &module.Module.properties,
1251 &module.Module.deviceProperties,
1252 &module.Module.protoProperties,
1253 &module.testProperties)
1254
1255 InitJavaModule(module, android.HostAndDeviceSupported)
1256 android.InitDefaultableModule(module)
1257 return module
1258}
1259
1260func TestHostFactory() android.Module {
1261 module := &Test{}
1262
1263 module.AddProperties(
1264 &module.Module.properties,
1265 &module.Module.protoProperties,
1266 &module.testProperties)
1267
1268 InitJavaModule(module, android.HostSupported)
1269 android.InitDefaultableModule(module)
1270 return module
1271}
1272
1273//
Colin Cross2fe66872015-03-30 17:20:39 -07001274// Java Binaries (.jar file plus wrapper script)
1275//
1276
Colin Crossf506d872017-07-19 15:53:04 -07001277type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001278 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001279 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001280}
1281
Colin Crossf506d872017-07-19 15:53:04 -07001282type Binary struct {
1283 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001284
Colin Crossf506d872017-07-19 15:53:04 -07001285 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001286
Colin Cross6b4a32d2017-12-05 13:42:45 -08001287 isWrapperVariant bool
1288
Colin Crossc3315992017-12-08 19:12:36 -08001289 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001290 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001291}
1292
Alex Light24237172017-10-26 09:46:21 -07001293func (j *Binary) HostToolPath() android.OptionalPath {
1294 return android.OptionalPathForPath(j.binaryFile)
1295}
1296
Colin Crossf506d872017-07-19 15:53:04 -07001297func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001298 if ctx.Arch().ArchType == android.Common {
1299 // Compile the jar
1300 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001301 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001302 // Handle the binary wrapper
1303 j.isWrapperVariant = true
1304
Colin Cross366938f2017-12-11 16:29:02 -08001305 if j.binaryProperties.Wrapper != nil {
1306 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001307 } else {
1308 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1309 }
1310
1311 // Depend on the installed jar so that the wrapper doesn't get executed by
1312 // another build rule before the jar has been installed.
1313 jarFile := ctx.PrimaryModule().(*Binary).installFile
1314
1315 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1316 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001317 }
Colin Cross2fe66872015-03-30 17:20:39 -07001318}
1319
Colin Crossf506d872017-07-19 15:53:04 -07001320func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001321 if ctx.Arch().ArchType == android.Common {
1322 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001323 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001324 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001325 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001326}
1327
Colin Crossf506d872017-07-19 15:53:04 -07001328func BinaryFactory() android.Module {
1329 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001330
Colin Cross36242852017-06-23 15:06:31 -07001331 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001332 &module.Module.properties,
1333 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001334 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001335 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001336
Colin Cross6b4a32d2017-12-05 13:42:45 -08001337 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1338 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001339 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001340}
1341
Colin Crossf506d872017-07-19 15:53:04 -07001342func BinaryHostFactory() android.Module {
1343 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001344
Colin Cross36242852017-06-23 15:06:31 -07001345 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001346 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001347 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001348 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001349
Colin Cross6b4a32d2017-12-05 13:42:45 -08001350 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1351 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001352 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001353}
1354
1355//
1356// Java prebuilts
1357//
1358
Colin Cross74d73e22017-08-02 11:05:49 -07001359type ImportProperties struct {
1360 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001361
Nan Zhangea568a42017-11-08 21:20:04 -08001362 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001363
1364 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001365}
1366
1367type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001368 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001369 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001370
Colin Cross74d73e22017-08-02 11:05:49 -07001371 properties ImportProperties
1372
Colin Cross0a6e0072017-08-30 14:24:55 -07001373 classpathFiles android.Paths
1374 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001375}
1376
Colin Cross74d73e22017-08-02 11:05:49 -07001377func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001378 return &j.prebuilt
1379}
1380
Colin Cross74d73e22017-08-02 11:05:49 -07001381func (j *Import) PrebuiltSrcs() []string {
1382 return j.properties.Jars
1383}
1384
1385func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001386 return j.prebuilt.Name(j.ModuleBase.Name())
1387}
1388
Colin Cross74d73e22017-08-02 11:05:49 -07001389func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001390}
1391
Colin Cross74d73e22017-08-02 11:05:49 -07001392func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1393 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001394
Colin Crosse9a275b2017-10-16 17:09:48 -07001395 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001396 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001397 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001398}
1399
Colin Cross74d73e22017-08-02 11:05:49 -07001400var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001401
Nan Zhanged19fc32017-10-19 13:06:22 -07001402func (j *Import) HeaderJars() android.Paths {
1403 return j.classpathFiles
1404}
1405
1406func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001407 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001408}
1409
Colin Cross74d73e22017-08-02 11:05:49 -07001410func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001411 return nil
1412}
1413
Colin Cross74d73e22017-08-02 11:05:49 -07001414var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001415
Colin Cross74d73e22017-08-02 11:05:49 -07001416func ImportFactory() android.Module {
1417 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001418
Colin Cross74d73e22017-08-02 11:05:49 -07001419 module.AddProperties(&module.properties)
1420
1421 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001422 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1423 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001424}
1425
Colin Cross74d73e22017-08-02 11:05:49 -07001426func ImportFactoryHost() android.Module {
1427 module := &Import{}
1428
1429 module.AddProperties(&module.properties)
1430
1431 android.InitPrebuiltModule(module, &module.properties.Jars)
1432 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1433 return module
1434}
1435
Colin Cross89536d42017-07-07 14:35:50 -07001436//
1437// Defaults
1438//
1439type Defaults struct {
1440 android.ModuleBase
1441 android.DefaultsModuleBase
1442}
1443
1444func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1445}
1446
1447func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1448}
1449
1450func defaultsFactory() android.Module {
1451 return DefaultsFactory()
1452}
1453
1454func DefaultsFactory(props ...interface{}) android.Module {
1455 module := &Defaults{}
1456
1457 module.AddProperties(props...)
1458 module.AddProperties(
1459 &CompilerProperties{},
1460 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001461 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001462 )
1463
1464 android.InitDefaultsModule(module)
1465
1466 return module
1467}
Nan Zhangea568a42017-11-08 21:20:04 -08001468
1469var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001470var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001471var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001472var inList = android.InList