blob: 969b0637e41850c50b7bdd73c6fb0e6402419fe1 [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 Cross6af2e492018-05-22 11:12:33 -0700174 // if true, compile against the platform APIs instead of an SDK.
175 Platform_apis *bool
176
Colin Crossebe1a512017-11-14 13:12:14 -0800177 Aidl struct {
178 // Top level directories to pass to aidl tool
179 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700180
Colin Crossebe1a512017-11-14 13:12:14 -0800181 // Directories rooted at the Android.bp file to pass to aidl tool
182 Local_include_dirs []string
183
184 // directories that should be added as include directories for any aidl sources of modules
185 // that depend on this module, as well as to aidl for this module.
186 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100187
188 // whether to generate traces (for systrace) for this interface
189 Generate_traces *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800190 }
Colin Cross92430102017-10-09 14:59:32 -0700191
192 // If true, export a copy of the module as a -hostdex module for host testing.
193 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700194
Colin Cross1bd87802017-12-05 15:31:19 -0800195 Dex_preopt struct {
196 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
197 // true.
198 Enabled *bool
199
200 // If true, generate an app image (.art file) for this module.
201 App_image *bool
202
203 // If true, use a checked-in profile to guide optimization. Defaults to false unless
204 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
205 // that matches the name of this module, in which case it is defaulted to true.
206 Profile_guided *bool
207
208 // If set, provides the path to profile relative to the Android.bp file. If not set,
209 // defaults to searching for a file that matches the name of this module in the default
210 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
211 Profile *string
212 }
Colin Crossa22116e2017-10-19 14:18:58 -0700213
Colin Cross66dbc0b2017-12-28 12:23:20 -0800214 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700215 // If false, disable all optimization. Defaults to true for android_app and android_test
216 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800217 Enabled *bool
218
219 // If true, optimize for size by removing unused code. Defaults to true for apps,
220 // false for libraries and tests.
221 Shrink *bool
222
223 // If true, optimize bytecode. Defaults to false.
224 Optimize *bool
225
226 // If true, obfuscate bytecode. Defaults to false.
227 Obfuscate *bool
228
229 // If true, do not use the flag files generated by aapt that automatically keep
230 // classes referenced by the app manifest. Defaults to false.
231 No_aapt_flags *bool
232
233 // Flags to pass to proguard.
234 Proguard_flags []string
235
236 // Specifies the locations of files containing proguard flags.
237 Proguard_flags_files []string
238 }
239
Colin Cross1369cdb2017-09-29 17:58:17 -0700240 // When targeting 1.9, override the modules to use with --system
241 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700242}
243
Colin Cross46c9b8b2017-06-22 16:51:17 -0700244// Module contains the properties and members used by all java module types
245type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700246 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700247 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700248
Colin Cross89536d42017-07-07 14:35:50 -0700249 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700250 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700251 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700252
Nan Zhanged19fc32017-10-19 13:06:22 -0700253 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
254 headerJarFile android.Path
255
256 // full implementation jar file suitable for static dependency of another module compile
257 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700258
Colin Cross6ade34f2017-09-15 13:00:47 -0700259 // output file containing classes.dex
260 dexJarFile android.Path
261
Colin Crosscb933592017-11-22 13:49:43 -0800262 // output file containing uninstrumented classes that will be instrumented by jacoco
263 jacocoReportClassesFile android.Path
264
Colin Cross66dbc0b2017-12-28 12:23:20 -0800265 // output file containing mapping of obfuscated names
266 proguardDictionary android.Path
267
Colin Crossb7a63242015-04-16 14:09:14 -0700268 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700269 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700270
Colin Cross635c3b02016-05-18 15:37:25 -0700271 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700272
Colin Cross635c3b02016-05-18 15:37:25 -0700273 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700274
Colin Cross2fe66872015-03-30 17:20:39 -0700275 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700276 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800277
278 // list of .java files and srcjars that was passed to javac
279 compiledJavaSrcs android.Paths
280 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800281
282 // list of extra progurad flag files
283 extraProguardFlagFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700284}
285
Colin Cross54250902017-12-05 09:28:08 -0800286func (j *Module) Srcs() android.Paths {
287 return android.Paths{j.implementationJarFile}
288}
289
290var _ android.SourceFileProducer = (*Module)(nil)
291
Colin Crossf506d872017-07-19 15:53:04 -0700292type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 HeaderJars() android.Paths
294 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700295 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700296}
297
Jiyong Parkc678ad32018-04-10 13:07:10 +0900298type SdkLibraryDependency interface {
299 HeaderJars(linkType linkType) android.Paths
300}
301
Nan Zhangb2b33de2018-02-23 11:18:47 -0800302type SrcDependency interface {
303 CompiledSrcs() android.Paths
304 CompiledSrcJars() android.Paths
305}
306
307func (j *Module) CompiledSrcs() android.Paths {
308 return j.compiledJavaSrcs
309}
310
311func (j *Module) CompiledSrcJars() android.Paths {
312 return j.compiledSrcJars
313}
314
315var _ SrcDependency = (*Module)(nil)
316
Colin Cross89536d42017-07-07 14:35:50 -0700317func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
318 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
319 android.InitDefaultableModule(module)
320}
321
Colin Crossbe1da472017-07-07 15:59:46 -0700322type dependencyTag struct {
323 blueprint.BaseDependencyTag
324 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700325}
326
Colin Crossbe1da472017-07-07 15:59:46 -0700327var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700328 staticLibTag = dependencyTag{name: "staticlib"}
329 libTag = dependencyTag{name: "javalib"}
330 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700331 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700332 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800333 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700334 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800335 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700336)
Colin Cross2fe66872015-03-30 17:20:39 -0700337
Colin Crossfc3674a2017-09-18 17:41:52 -0700338type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700339 useModule, useFiles, useDefaultLibs, invalidVersion bool
340
Colin Cross86a60ae2018-05-29 14:44:55 -0700341 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700342 systemModules string
343
Colin Crossa97c5d32018-03-28 14:58:31 -0700344 frameworkResModule string
345
Colin Cross86a60ae2018-05-29 14:44:55 -0700346 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700347 aidl android.Path
348}
349
350func sdkStringToNumber(ctx android.BaseContext, v string) int {
351 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900352 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900353 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700354 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900355 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700356 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
357 return -1
358 } else {
359 return i
360 }
361 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700362}
363
Colin Cross3144dfc2018-01-03 15:06:47 -0800364func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
365 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
366}
367
368func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
369 return j.shouldInstrument(ctx) &&
370 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
371 ctx.Config().UnbundledBuild())
372}
373
Colin Crossfc3674a2017-09-18 17:41:52 -0700374func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700375 i := sdkStringToNumber(ctx, v)
376 if i == -1 {
377 // Invalid sdk version, error handled by sdkStringToNumber.
378 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700379 }
380
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900381 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
382 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
383 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
384 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
385 if ctx.DeviceSpecific() || ctx.SocSpecific() {
386 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
387 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
388 }
389 }
390 version := strings.TrimPrefix(v, "system_")
391 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
392 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
393 v, allowed_versions)
394 }
395 }
396
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100397 toPrebuilt := func(sdk string) sdkDep {
398 var api, v string
399 if strings.Contains(sdk, "_") {
400 t := strings.Split(sdk, "_")
401 api = t[0]
402 v = t[1]
403 } else {
404 api = "public"
405 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900406 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100407 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700408 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100409 // There's no aidl for other SDKs yet.
410 // TODO(77525052): Add aidl files for other SDKs too.
411 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
412 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800413 jarPath := android.ExistentPathForSource(ctx, jar)
414 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700415 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700416
Colin Cross6510f912017-11-29 00:27:14 -0800417 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700418 return sdkDep{
419 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700420 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700421 }
422 }
423
Colin Crossfc3674a2017-09-18 17:41:52 -0700424 if !jarPath.Valid() {
425 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
426 return sdkDep{}
427 }
Colin Cross47ff2522017-10-02 14:22:08 -0700428
Colin Crossfc3674a2017-09-18 17:41:52 -0700429 if !aidlPath.Valid() {
430 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
431 return sdkDep{}
432 }
Colin Cross47ff2522017-10-02 14:22:08 -0700433
Colin Crossfc3674a2017-09-18 17:41:52 -0700434 return sdkDep{
435 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700436 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700437 aidl: aidlPath.Path(),
438 }
439 }
440
Colin Crossa97c5d32018-03-28 14:58:31 -0700441 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700442 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700443 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700444 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700445 systemModules: m + "_system_modules",
446 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700447 }
448 if m == "core.current.stubs" {
449 ret.systemModules = "core-system-modules"
450 }
451 return ret
452 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700453
Colin Cross6510f912017-11-29 00:27:14 -0800454 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100455 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700456 }
457
458 switch v {
459 case "":
460 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700461 useDefaultLibs: true,
462 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700463 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700464 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700465 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700466 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700467 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700468 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700469 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700470 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700471 return toModule("core.current.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700472 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100473 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700474 }
475}
476
Colin Crossbe1da472017-07-07 15:59:46 -0700477func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700478 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700479 if !Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800480 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700481 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700482 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800483 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700484 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
485 }
Colin Crossff3ae9d2018-04-10 16:15:18 -0700486 if !Bool(j.properties.No_framework_libs) {
Colin Crossfa5eb232017-10-01 20:33:03 -0700487 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
488 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700489 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800490 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700491 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
492 }
Colin Cross86a60ae2018-05-29 14:44:55 -0700493 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800494 if Bool(j.deviceProperties.Optimize.Enabled) {
495 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
496 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
497 }
Colin Crossbe1da472017-07-07 15:59:46 -0700498 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700499 } else if j.deviceProperties.System_modules == nil {
500 ctx.PropertyErrorf("no_standard_libs",
501 "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 -0800502 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700503 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700504 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800505 if ctx.ModuleName() == "framework" {
506 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
507 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800508 if ctx.ModuleName() == "android_stubs_current" ||
509 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang16c0a312018-06-13 17:42:48 -0700510 ctx.ModuleName() == "android_test_stubs_current" ||
511 ctx.ModuleName() == "metalava_android_stubs_current" ||
512 ctx.ModuleName() == "metalava_android_system_stubs_current" ||
513 ctx.ModuleName() == "metalava_android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800514 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
515 }
Colin Cross2fe66872015-03-30 17:20:39 -0700516 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700517
Colin Crossf506d872017-07-19 15:53:04 -0700518 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
519 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700520 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700521
522 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000523 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700524 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800525 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700526
527 if j.hasSrcExt(".proto") {
528 protoDeps(ctx, &j.protoProperties)
529 }
Colin Cross93e85952017-08-15 13:34:18 -0700530
531 if j.hasSrcExt(".kt") {
532 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
533 // Kotlin files
534 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
535 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800536
537 if j.shouldInstrumentStatic(ctx) {
538 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
539 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700540}
541
542func hasSrcExt(srcs []string, ext string) bool {
543 for _, src := range srcs {
544 if filepath.Ext(src) == ext {
545 return true
546 }
547 }
548
549 return false
550}
551
Nan Zhang61eaedb2017-11-02 13:28:15 -0700552func shardPaths(paths android.Paths, shardSize int) []android.Paths {
553 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
554 for len(paths) > shardSize {
555 ret = append(ret, paths[0:shardSize])
556 paths = paths[shardSize:]
557 }
558 if len(paths) > 0 {
559 ret = append(ret, paths)
560 }
561 return ret
562}
563
Colin Cross6af17aa2017-09-20 12:59:05 -0700564func (j *Module) hasSrcExt(ext string) bool {
565 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700566}
567
Colin Cross46c9b8b2017-06-22 16:51:17 -0700568func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700569 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700570
Colin Crossebe1a512017-11-14 13:12:14 -0800571 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
572 aidlIncludes = append(aidlIncludes,
573 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
574 aidlIncludes = append(aidlIncludes,
575 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700576
577 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700578 if aidlPreprocess.Valid() {
579 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700580 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700581 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700582 }
583
Colin Cross635c3b02016-05-18 15:37:25 -0700584 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800585 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700586 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800587 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700588 flags = append(flags, "-I"+src.String())
589 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700590
Martijn Coeneneab15642018-03-09 09:29:59 +0100591 if Bool(j.deviceProperties.Aidl.Generate_traces) {
592 flags = append(flags, "-t")
593 }
594
Colin Crossf03c82b2015-04-13 13:53:40 -0700595 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700596}
597
Colin Cross32f676a2017-09-06 13:41:06 -0700598type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800599 classpath classpath
600 bootClasspath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700601 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700602 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700603 staticJarResources android.Paths
604 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800605 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700606 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700607 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700608 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700609 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700610}
Colin Cross2fe66872015-03-30 17:20:39 -0700611
Colin Cross54250902017-12-05 09:28:08 -0800612func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
613 for _, f := range dep.Srcs() {
614 if f.Ext() != ".jar" {
615 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
616 ctx.OtherModuleName(dep.(blueprint.Module)))
617 }
618 }
619}
620
Jiyong Park2d492942018-03-05 17:44:10 +0900621type linkType int
622
623const (
624 javaCore linkType = iota
625 javaSdk
626 javaSystem
627 javaPlatform
628)
629
Colin Crossf19b9bb2018-03-26 14:42:44 -0700630func getLinkType(m *Module, name string) linkType {
Jiyong Park2d492942018-03-05 17:44:10 +0900631 ver := String(m.deviceProperties.Sdk_version)
Colin Cross86a60ae2018-05-29 14:44:55 -0700632 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700633 switch {
Nan Zhang16c0a312018-06-13 17:42:48 -0700634 case name == "core.current.stubs" || ver == "core_current" || noStdLibs || name == "stub-annotations":
Jiyong Park2d492942018-03-05 17:44:10 +0900635 return javaCore
Nan Zhang16c0a312018-06-13 17:42:48 -0700636 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_") || name == "metalava_android_system_stubs_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900637 return javaSystem
Nan Zhang16c0a312018-06-13 17:42:48 -0700638 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_") || name == "metalava_android_test_stubs_current":
Jiyong Park2d492942018-03-05 17:44:10 +0900639 return javaPlatform
Nan Zhang16c0a312018-06-13 17:42:48 -0700640 case name == "android_stubs_current" || ver == "current" || name == "metalava_android_stubs_current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700641 return javaSdk
642 case ver == "":
643 return javaPlatform
644 default:
645 if _, err := strconv.Atoi(ver); err != nil {
646 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
647 }
648 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900649 }
650}
651
Jiyong Park750e5572018-01-31 00:20:13 +0900652func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700653 if ctx.Host() {
654 return
655 }
656
657 myLinkType := getLinkType(from, ctx.ModuleName())
658 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900659 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."
660
661 switch myLinkType {
662 case javaCore:
663 if otherLinkType != javaCore {
664 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 +0900665 ctx.OtherModuleName(to))
666 }
Jiyong Park2d492942018-03-05 17:44:10 +0900667 break
668 case javaSdk:
669 if otherLinkType != javaCore && otherLinkType != javaSdk {
670 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
671 ctx.OtherModuleName(to))
672 }
673 break
674 case javaSystem:
675 if otherLinkType == javaPlatform {
676 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
677 ctx.OtherModuleName(to))
678 }
679 break
680 case javaPlatform:
681 // no restriction on link-type
682 break
Jiyong Park750e5572018-01-31 00:20:13 +0900683 }
684}
685
Colin Cross32f676a2017-09-06 13:41:06 -0700686func (j *Module) collectDeps(ctx android.ModuleContext) deps {
687 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700688
Colin Cross300f0382018-03-06 13:11:51 -0800689 if ctx.Device() {
690 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
691 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700692 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800693 } else if sdkDep.useFiles {
694 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700695 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800696 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
697 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700698 }
699
Colin Crossd11fcda2017-10-23 17:59:01 -0700700 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700701 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700702 tag := ctx.OtherModuleDependencyTag(module)
703
Jiyong Park750e5572018-01-31 00:20:13 +0900704 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700705 switch tag {
706 case bootClasspathTag, libTag, staticLibTag:
707 checkLinkType(ctx, j, to, tag.(dependencyTag))
708 }
Jiyong Park750e5572018-01-31 00:20:13 +0900709 }
Colin Cross54250902017-12-05 09:28:08 -0800710 switch dep := module.(type) {
711 case Dependency:
712 switch tag {
713 case bootClasspathTag:
714 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
715 case libTag:
716 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
717 case staticLibTag:
718 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
719 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
720 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
721 case frameworkResTag:
722 if ctx.ModuleName() == "framework" {
723 // framework.jar has a one-off dependency on the R.java and Manifest.java files
724 // generated by framework-res.apk
725 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
726 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800727 case frameworkApkTag:
728 if ctx.ModuleName() == "android_stubs_current" ||
729 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang16c0a312018-06-13 17:42:48 -0700730 ctx.ModuleName() == "android_test_stubs_current" ||
731 ctx.ModuleName() == "metalava_android_stubs_current" ||
732 ctx.ModuleName() == "metalava_android_system_stubs_current" ||
733 ctx.ModuleName() == "metalava_android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800734 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
735 // resource files out of there for aapt.
736 //
737 // Normally the package rule runs aapt, which includes the resource,
738 // but we're not running that in our package rule so just copy in the
739 // resource files here.
740 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
741 }
Colin Cross54250902017-12-05 09:28:08 -0800742 case kotlinStdlibTag:
743 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800744 }
745
746 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900747 case SdkLibraryDependency:
748 switch tag {
749 case libTag:
750 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
751 default:
752 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
753 }
Colin Cross54250902017-12-05 09:28:08 -0800754 case android.SourceFileProducer:
755 switch tag {
756 case libTag:
757 checkProducesJars(ctx, dep)
758 deps.classpath = append(deps.classpath, dep.Srcs()...)
759 case staticLibTag:
760 checkProducesJars(ctx, dep)
761 deps.classpath = append(deps.classpath, dep.Srcs()...)
762 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
763 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
764 case android.DefaultsDepTag, android.SourceDepTag:
765 // Nothing to do
766 default:
767 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
768 }
769 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700770 switch tag {
771 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700772 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700773 case systemModulesTag:
774 if deps.systemModules != nil {
775 panic("Found two system module dependencies")
776 }
777 sm := module.(*SystemModules)
778 if sm.outputFile == nil {
779 panic("Missing directory for system module dependency")
780 }
781 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700782 default:
783 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700784 }
Colin Crossec7a0422017-07-07 14:47:12 -0700785 }
Colin Cross2fe66872015-03-30 17:20:39 -0700786 })
787
Colin Cross32f676a2017-09-06 13:41:06 -0700788 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700789}
790
Nan Zhang357466b2018-04-17 17:38:36 -0700791func getJavaVersion(ctx android.ModuleContext, javaVersion, sdkVersion string) string {
792 var ret string
793 sdk := sdkStringToNumber(ctx, sdkVersion)
794 if javaVersion != "" {
795 ret = javaVersion
796 } else if ctx.Device() && sdk <= 23 {
797 ret = "1.7"
798 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
799 ret = "1.8"
800 } else if ctx.Device() && sdkVersion != "" && sdk == android.FutureApiLevel {
801 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
802 ret = "1.8"
803 } else {
804 ret = "1.9"
805 }
806
807 return ret
808}
809
Nan Zhanged19fc32017-10-19 13:06:22 -0700810func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700811
Colin Crossf03c82b2015-04-13 13:53:40 -0700812 var flags javaBuilderFlags
813
Nan Zhanged19fc32017-10-19 13:06:22 -0700814 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700815 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800816 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700817 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700818 }
Colin Cross6510f912017-11-29 00:27:14 -0800819 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700820 // Override the -g flag passed globally to remove local variable debug info to reduce
821 // disk and memory usage.
822 javacFlags = append(javacFlags, "-g:source,lines")
823 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700824 if len(javacFlags) > 0 {
825 // optimization.
826 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
827 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700828 }
Colin Cross64162712017-08-08 13:17:59 -0700829
Andreas Gampef3e5b552018-01-22 21:27:21 -0800830 if len(j.properties.Errorprone.Javacflags) > 0 {
831 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
832 }
833
Nan Zhanged19fc32017-10-19 13:06:22 -0700834 // javaVersion flag.
Nan Zhang357466b2018-04-17 17:38:36 -0700835 flags.javaVersion = getJavaVersion(ctx,
836 String(j.properties.Java_version), String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700837
Nan Zhanged19fc32017-10-19 13:06:22 -0700838 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800839 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
840 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800841
842 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
843 !Bool(j.properties.No_standard_libs) &&
844 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
845 // Give host-side tools a version of OpenJDK's standard libraries
846 // close to what they're targeting. As of Dec 2017, AOSP is only
847 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
848 //
849 // When building with OpenJDK 8, the following should have no
850 // effect since those jars would be available by default.
851 //
852 // When building with OpenJDK 9 but targeting a version < 1.8,
853 // putting them on the bootclasspath means that:
854 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
855 // b) references to existing APIs are not reinterpreted in an
856 // OpenJDK 9-specific way, eg. calls to subclasses of
857 // java.nio.Buffer as in http://b/70862583
858 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
859 flags.bootClasspath = append(flags.bootClasspath,
860 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
861 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800862 if Bool(j.properties.Use_tools_jar) {
863 flags.bootClasspath = append(flags.bootClasspath,
864 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
865 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800866 }
867
Nan Zhanged19fc32017-10-19 13:06:22 -0700868 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700869 if deps.systemModules != nil {
870 flags.systemModules = append(flags.systemModules, deps.systemModules)
871 }
872
Nan Zhanged19fc32017-10-19 13:06:22 -0700873 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700874 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700875 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700876 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700877 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
878 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700879 }
880
Nan Zhanged19fc32017-10-19 13:06:22 -0700881 return flags
882}
Colin Crossc0b06f12015-04-08 13:03:43 -0700883
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800884func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700885
Colin Crossebe1a512017-11-14 13:12:14 -0800886 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700887
888 deps := j.collectDeps(ctx)
889 flags := j.collectBuilderFlags(ctx, deps)
890
Colin Cross6510f912017-11-29 00:27:14 -0800891 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700892 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
893 }
894 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700895 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800896 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700897 }
898
Colin Crossaf050172017-11-15 23:01:59 -0800899 srcFiles = j.genSources(ctx, srcFiles, flags)
900
901 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700902 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800903 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700904
Colin Cross0a6e0072017-08-30 14:24:55 -0700905 var jars android.Paths
906
Colin Cross1ee23172017-10-18 14:44:18 -0700907 jarName := ctx.ModuleName() + ".jar"
908
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000909 javaSrcFiles := srcFiles.FilterByExt(".java")
910 var uniqueSrcFiles android.Paths
911 set := make(map[string]bool)
912 for _, v := range javaSrcFiles {
913 if _, found := set[v.String()]; !found {
914 set[v.String()] = true
915 uniqueSrcFiles = append(uniqueSrcFiles, v)
916 }
917 }
918
Colin Cross93e85952017-08-15 13:34:18 -0700919 if srcFiles.HasExt(".kt") {
920 // If there are kotlin files, compile them first but pass all the kotlin and java files
921 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
922 // won't emit any classes for them.
923
924 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000925
Colin Cross93e85952017-08-15 13:34:18 -0700926 if ctx.Device() {
927 flags.kotlincFlags += " -no-jdk"
928 }
929
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000930 var kotlinSrcFiles android.Paths
931 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
932 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
933
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000934 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700935 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
936 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
937
Colin Cross1ee23172017-10-18 14:44:18 -0700938 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000939 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700940 if ctx.Failed() {
941 return
942 }
943
944 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -0700945 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -0700946 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000947
Colin Cross93e85952017-08-15 13:34:18 -0700948 // Jar kotlin classes into the final jar after javac
949 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000950
951 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
952 // (it's expected to be on device bootclasspath)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700953 if !Bool(j.properties.Renamed_kotlin_stdlib) {
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000954 jars = append(jars, deps.kotlinStdlib...)
955 }
Colin Cross93e85952017-08-15 13:34:18 -0700956 }
957
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800958 // Store the list of .java files that was passed to javac
959 j.compiledJavaSrcs = uniqueSrcFiles
960 j.compiledSrcJars = srcJars
961
Nan Zhang61eaedb2017-11-02 13:28:15 -0700962 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800963 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700964 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
965 enable_sharding = true
966 if len(j.properties.Annotation_processors) != 0 ||
967 len(j.properties.Annotation_processor_classes) != 0 {
968 ctx.PropertyErrorf("javac_shard_size",
969 "%q cannot be set when annotation processors are enabled.",
970 j.properties.Javac_shard_size)
971 }
972 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700973 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
974 if ctx.Failed() {
975 return
Nan Zhanged19fc32017-10-19 13:06:22 -0700976 }
977 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700978 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700979 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800980 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700981 // If error-prone is enabled, add an additional rule to compile the java files into
982 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700983 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700984 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
985 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700986 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700987 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700988 extraJarDeps = append(extraJarDeps, errorprone)
989 }
990
Nan Zhang61eaedb2017-11-02 13:28:15 -0700991 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800992 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700993 shardSize := int(*(j.properties.Javac_shard_size))
994 var shardSrcs []android.Paths
995 if len(uniqueSrcFiles) > 0 {
996 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
997 for idx, shardSrc := range shardSrcs {
998 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
999 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1000 jars = append(jars, classes)
1001 }
1002 }
1003 if len(srcJars) > 0 {
1004 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1005 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1006 jars = append(jars, classes)
1007 }
1008 } else {
1009 classes := android.PathForModuleOut(ctx, "javac", jarName)
1010 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1011 jars = append(jars, classes)
1012 }
Colin Crossd6891432017-09-27 17:39:56 -07001013 if ctx.Failed() {
1014 return
1015 }
Colin Cross2fe66872015-03-30 17:20:39 -07001016 }
1017
Colin Cross0f37af02017-09-27 17:42:05 -07001018 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
1019 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1020
1021 var resArgs []string
1022 var resDeps android.Paths
1023
1024 resArgs = append(resArgs, dirArgs...)
1025 resDeps = append(resDeps, dirDeps...)
1026
1027 resArgs = append(resArgs, fileArgs...)
1028 resDeps = append(resDeps, fileDeps...)
1029
Colin Crossff3ae9d2018-04-10 16:15:18 -07001030 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001031 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001032 resArgs = append(resArgs, srcArgs...)
1033 resDeps = append(resDeps, srcDeps...)
1034 }
Colin Cross40a36712017-09-27 17:41:35 -07001035
1036 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001037 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001038 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -07001039 if ctx.Failed() {
1040 return
1041 }
Colin Cross20978302015-04-10 17:05:07 -07001042
Colin Cross0a6e0072017-08-30 14:24:55 -07001043 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -07001044 }
1045
Colin Cross6ade34f2017-09-15 13:00:47 -07001046 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -07001047 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001048 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001049
Colin Cross366938f2017-12-11 16:29:02 -08001050 var manifest android.OptionalPath
1051 if j.properties.Manifest != nil {
1052 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1053 }
Colin Cross635acc92017-09-12 22:50:46 -07001054
Colin Cross0a6e0072017-08-30 14:24:55 -07001055 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001056 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -07001057 var outputFile android.Path
1058
1059 if len(jars) == 1 && !manifest.Valid() {
1060 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -08001061 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1062 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1063 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -07001064 outputFile = jars[0]
1065 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001066 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -07001067 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001068 outputFile = combinedJar
1069 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001070
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001071 // Use renamed kotlin standard library?
Colin Crossff3ae9d2018-04-10 16:15:18 -07001072 if srcFiles.HasExt(".kt") && Bool(j.properties.Renamed_kotlin_stdlib) {
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001073 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
1074 TransformJarJar(ctx, jarjarFile, outputFile,
1075 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
1076 outputFile = jarjarFile
1077 if ctx.Failed() {
1078 return
1079 }
1080 }
1081
Colin Cross0a6e0072017-08-30 14:24:55 -07001082 if j.properties.Jarjar_rules != nil {
1083 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001084 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001085 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001086 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1087 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -07001088 if ctx.Failed() {
1089 return
1090 }
1091 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001092 j.implementationJarFile = outputFile
1093 if j.headerJarFile == nil {
1094 j.headerJarFile = j.implementationJarFile
1095 }
Colin Cross2fe66872015-03-30 17:20:39 -07001096
Colin Cross6510f912017-11-29 00:27:14 -08001097 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001098 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1099 j.properties.Instrument = true
1100 }
1101 }
1102
Colin Cross3144dfc2018-01-03 15:06:47 -08001103 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001104 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1105 }
1106
1107 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001108 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001109 if ctx.Failed() {
1110 return
1111 }
Colin Cross2fe66872015-03-30 17:20:39 -07001112 }
Colin Crossb7a63242015-04-16 14:09:14 -07001113 ctx.CheckbuildFile(outputFile)
1114 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001115}
1116
Colin Cross8eadbf02017-10-24 17:46:00 -07001117func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001118 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1119
1120 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001121 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001122 // Compile java sources into turbine.jar.
1123 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1124 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1125 if ctx.Failed() {
1126 return nil
1127 }
1128 jars = append(jars, turbineJar)
1129 }
1130
1131 // Combine any static header libraries into classes-header.jar. If there is only
1132 // one input jar this step will be skipped.
1133 var headerJar android.Path
1134 jars = append(jars, deps.staticHeaderJars...)
1135
Colin Cross5c6ecc12017-10-23 18:12:27 -07001136 // we cannot skip the combine step for now if there is only one jar
1137 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1138 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1139 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1140 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001141
1142 if j.properties.Jarjar_rules != nil {
1143 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1144 // Transform classes.jar into classes-jarjar.jar
1145 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1146 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1147 headerJar = jarjarFile
1148 if ctx.Failed() {
1149 return nil
1150 }
1151 }
1152
1153 return headerJar
1154}
1155
Colin Crosscb933592017-11-22 13:49:43 -08001156func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1157 classesJar android.Path, jarName string) android.Path {
1158
Colin Cross7a3139e2017-12-19 13:57:50 -08001159 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001160
Colin Cross84c38822018-01-03 15:59:46 -08001161 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001162 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1163
1164 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1165
1166 j.jacocoReportClassesFile = jacocoReportClassesFile
1167
1168 return instrumentedJar
1169}
1170
Colin Crosscb933592017-11-22 13:49:43 -08001171// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1172// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1173func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1174 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001175 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001176 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001177 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001178 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001179 }
1180}
1181
Colin Cross59f1bb62017-09-27 17:59:10 -07001182func (j *Module) installable() bool {
Colin Cross38b40df2018-04-10 16:14:46 -07001183 return BoolDefault(j.properties.Installable, true)
Colin Cross59f1bb62017-09-27 17:59:10 -07001184}
1185
Colin Crossf506d872017-07-19 15:53:04 -07001186var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001187
Nan Zhanged19fc32017-10-19 13:06:22 -07001188func (j *Module) HeaderJars() android.Paths {
1189 return android.Paths{j.headerJarFile}
1190}
1191
1192func (j *Module) ImplementationJars() android.Paths {
1193 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001194}
1195
Colin Cross46c9b8b2017-06-22 16:51:17 -07001196func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001197 return j.exportAidlIncludeDirs
1198}
1199
Colin Cross46c9b8b2017-06-22 16:51:17 -07001200var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001201
Colin Cross46c9b8b2017-06-22 16:51:17 -07001202func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001203 return j.logtagsSrcs
1204}
1205
Colin Cross2fe66872015-03-30 17:20:39 -07001206//
1207// Java libraries (.jar file)
1208//
1209
Colin Crossf506d872017-07-19 15:53:04 -07001210type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001211 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001212}
1213
Colin Crossf506d872017-07-19 15:53:04 -07001214func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001215 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001216
Colin Cross59f1bb62017-09-27 17:59:10 -07001217 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001218 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1219 ctx.ModuleName()+".jar", j.outputFile)
1220 }
Colin Crossb7a63242015-04-16 14:09:14 -07001221}
1222
Colin Crossf506d872017-07-19 15:53:04 -07001223func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001224 j.deps(ctx)
1225}
1226
Colin Crossa60ead82017-10-02 18:10:21 -07001227func LibraryFactory(installable bool) func() android.Module {
1228 return func() android.Module {
1229 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001230
Colin Crossa60ead82017-10-02 18:10:21 -07001231 if !installable {
1232 module.properties.Installable = proptools.BoolPtr(false)
1233 }
Colin Cross2fe66872015-03-30 17:20:39 -07001234
Colin Crossa60ead82017-10-02 18:10:21 -07001235 module.AddProperties(
1236 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001237 &module.Module.deviceProperties,
1238 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001239
Colin Crossa60ead82017-10-02 18:10:21 -07001240 InitJavaModule(module, android.HostAndDeviceSupported)
1241 return module
1242 }
Colin Cross2fe66872015-03-30 17:20:39 -07001243}
1244
Colin Crossf506d872017-07-19 15:53:04 -07001245func LibraryHostFactory() android.Module {
1246 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001247
Colin Cross6af17aa2017-09-20 12:59:05 -07001248 module.AddProperties(
1249 &module.Module.properties,
1250 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001251
Colin Cross89536d42017-07-07 14:35:50 -07001252 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001253 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001254}
1255
1256//
Colin Cross05638fc2018-04-09 18:40:24 -07001257// Java Junit Tests
1258//
1259
1260type testProperties struct {
1261 // If true, add a static dependency on the platform junit library. Defaults to true.
1262 Junit *bool
1263
1264 // list of compatibility suites (for example "cts", "vts") that the module should be
1265 // installed into.
1266 Test_suites []string `android:"arch_variant"`
1267}
1268
1269type Test struct {
1270 Library
1271
1272 testProperties testProperties
1273}
1274
1275func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1276 j.deps(ctx)
Colin Cross38b40df2018-04-10 16:14:46 -07001277 if BoolDefault(j.testProperties.Junit, true) {
Colin Cross05638fc2018-04-09 18:40:24 -07001278 ctx.AddDependency(ctx.Module(), staticLibTag, "junit")
1279 }
1280}
1281
1282func TestFactory() android.Module {
1283 module := &Test{}
1284
1285 module.AddProperties(
1286 &module.Module.properties,
1287 &module.Module.deviceProperties,
1288 &module.Module.protoProperties,
1289 &module.testProperties)
1290
1291 InitJavaModule(module, android.HostAndDeviceSupported)
1292 android.InitDefaultableModule(module)
1293 return module
1294}
1295
1296func TestHostFactory() android.Module {
1297 module := &Test{}
1298
1299 module.AddProperties(
1300 &module.Module.properties,
1301 &module.Module.protoProperties,
1302 &module.testProperties)
1303
1304 InitJavaModule(module, android.HostSupported)
1305 android.InitDefaultableModule(module)
1306 return module
1307}
1308
1309//
Colin Cross2fe66872015-03-30 17:20:39 -07001310// Java Binaries (.jar file plus wrapper script)
1311//
1312
Colin Crossf506d872017-07-19 15:53:04 -07001313type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001314 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001315 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001316}
1317
Colin Crossf506d872017-07-19 15:53:04 -07001318type Binary struct {
1319 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001320
Colin Crossf506d872017-07-19 15:53:04 -07001321 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001322
Colin Cross6b4a32d2017-12-05 13:42:45 -08001323 isWrapperVariant bool
1324
Colin Crossc3315992017-12-08 19:12:36 -08001325 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001326 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001327}
1328
Alex Light24237172017-10-26 09:46:21 -07001329func (j *Binary) HostToolPath() android.OptionalPath {
1330 return android.OptionalPathForPath(j.binaryFile)
1331}
1332
Colin Crossf506d872017-07-19 15:53:04 -07001333func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001334 if ctx.Arch().ArchType == android.Common {
1335 // Compile the jar
1336 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001337 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001338 // Handle the binary wrapper
1339 j.isWrapperVariant = true
1340
Colin Cross366938f2017-12-11 16:29:02 -08001341 if j.binaryProperties.Wrapper != nil {
1342 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001343 } else {
1344 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1345 }
1346
1347 // Depend on the installed jar so that the wrapper doesn't get executed by
1348 // another build rule before the jar has been installed.
1349 jarFile := ctx.PrimaryModule().(*Binary).installFile
1350
1351 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1352 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001353 }
Colin Cross2fe66872015-03-30 17:20:39 -07001354}
1355
Colin Crossf506d872017-07-19 15:53:04 -07001356func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001357 if ctx.Arch().ArchType == android.Common {
1358 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001359 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001360 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001361 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001362}
1363
Colin Crossf506d872017-07-19 15:53:04 -07001364func BinaryFactory() android.Module {
1365 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001366
Colin Cross36242852017-06-23 15:06:31 -07001367 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001368 &module.Module.properties,
1369 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001370 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001371 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001372
Colin Cross6b4a32d2017-12-05 13:42:45 -08001373 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1374 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001375 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001376}
1377
Colin Crossf506d872017-07-19 15:53:04 -07001378func BinaryHostFactory() android.Module {
1379 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001380
Colin Cross36242852017-06-23 15:06:31 -07001381 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001382 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001383 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001384 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001385
Colin Cross6b4a32d2017-12-05 13:42:45 -08001386 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1387 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001388 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001389}
1390
1391//
1392// Java prebuilts
1393//
1394
Colin Cross74d73e22017-08-02 11:05:49 -07001395type ImportProperties struct {
1396 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001397
Nan Zhangea568a42017-11-08 21:20:04 -08001398 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001399
1400 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001401}
1402
1403type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001404 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001405 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001406
Colin Cross74d73e22017-08-02 11:05:49 -07001407 properties ImportProperties
1408
Colin Cross0a6e0072017-08-30 14:24:55 -07001409 classpathFiles android.Paths
1410 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001411}
1412
Colin Cross74d73e22017-08-02 11:05:49 -07001413func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001414 return &j.prebuilt
1415}
1416
Colin Cross74d73e22017-08-02 11:05:49 -07001417func (j *Import) PrebuiltSrcs() []string {
1418 return j.properties.Jars
1419}
1420
1421func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001422 return j.prebuilt.Name(j.ModuleBase.Name())
1423}
1424
Colin Cross74d73e22017-08-02 11:05:49 -07001425func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001426}
1427
Colin Cross74d73e22017-08-02 11:05:49 -07001428func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1429 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001430
Colin Crosse9a275b2017-10-16 17:09:48 -07001431 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001432 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001433 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001434}
1435
Colin Cross74d73e22017-08-02 11:05:49 -07001436var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001437
Nan Zhanged19fc32017-10-19 13:06:22 -07001438func (j *Import) HeaderJars() android.Paths {
1439 return j.classpathFiles
1440}
1441
1442func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001443 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001444}
1445
Colin Cross74d73e22017-08-02 11:05:49 -07001446func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001447 return nil
1448}
1449
Colin Cross74d73e22017-08-02 11:05:49 -07001450var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001451
Colin Cross74d73e22017-08-02 11:05:49 -07001452func ImportFactory() android.Module {
1453 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001454
Colin Cross74d73e22017-08-02 11:05:49 -07001455 module.AddProperties(&module.properties)
1456
1457 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001458 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1459 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001460}
1461
Colin Cross74d73e22017-08-02 11:05:49 -07001462func ImportFactoryHost() android.Module {
1463 module := &Import{}
1464
1465 module.AddProperties(&module.properties)
1466
1467 android.InitPrebuiltModule(module, &module.properties.Jars)
1468 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1469 return module
1470}
1471
Colin Cross89536d42017-07-07 14:35:50 -07001472//
1473// Defaults
1474//
1475type Defaults struct {
1476 android.ModuleBase
1477 android.DefaultsModuleBase
1478}
1479
1480func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1481}
1482
1483func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1484}
1485
1486func defaultsFactory() android.Module {
1487 return DefaultsFactory()
1488}
1489
1490func DefaultsFactory(props ...interface{}) android.Module {
1491 module := &Defaults{}
1492
1493 module.AddProperties(props...)
1494 module.AddProperties(
1495 &CompilerProperties{},
1496 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001497 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001498 )
1499
1500 android.InitDefaultsModule(module)
1501
1502 return module
1503}
Nan Zhangea568a42017-11-08 21:20:04 -08001504
1505var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001506var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001507var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001508var inList = android.InList