blob: 125fde1ecfc2f2da98fce700b1ff791315b530cf [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
22 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Cross89536d42017-07-07 14:35:50 -070035 android.RegisterModuleType("java_defaults", defaultsFactory)
36
Colin Crossa60ead82017-10-02 18:10:21 -070037 android.RegisterModuleType("java_library", LibraryFactory(true))
38 android.RegisterModuleType("java_library_static", LibraryFactory(false))
Colin Crossf506d872017-07-19 15:53:04 -070039 android.RegisterModuleType("java_library_host", LibraryHostFactory)
40 android.RegisterModuleType("java_binary", BinaryFactory)
41 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070042 android.RegisterModuleType("java_import", ImportFactory)
43 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070044
Colin Cross798bfce2016-10-12 14:28:16 -070045 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070046}
47
Colin Cross2fe66872015-03-30 17:20:39 -070048// TODO:
49// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070050// Renderscript
51// Post-jar passes:
52// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070053// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070054// DroidDoc
55// Findbugs
56
Colin Cross89536d42017-07-07 14:35:50 -070057type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070058 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
59 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070060 Srcs []string `android:"arch_variant"`
61
62 // list of source files that should not be used to build the Java module.
63 // This is most useful in the arch/multilib variants to remove non-common files
64 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070065
66 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070067 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
Colin Cross86a63ff2017-09-27 17:33:10 -070069 // list of directories that should be excluded from java_resource_dirs
70 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070071
Colin Cross0f37af02017-09-27 17:42:05 -070072 // list of files to use as Java resources
73 Java_resources []string `android:"arch_variant"`
74
75 // list of files that should be excluded from java_resources
76 Exclude_java_resources []string `android:"arch_variant"`
77
Colin Crossfa5eb232017-10-01 20:33:03 -070078 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070080 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070081
Colin Crossfa5eb232017-10-01 20:33:03 -070082 // don't build against the framework libraries (legacy-test, core-junit,
83 // ext, and framework for device targets)
84 No_framework_libs *bool
85
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000086 // Use renamed kotlin stdlib (com.android.kotlin.*). This allows kotlin usage without colliding
87 // with app-provided kotlin stdlib.
88 Renamed_kotlin_stdlib *bool
89
Colin Cross7d5136f2015-05-11 13:39:40 -070090 // list of module-specific flags that will be used for javac compiles
91 Javacflags []string `android:"arch_variant"`
92
Colin Cross7d5136f2015-05-11 13:39:40 -070093 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070094 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070095
96 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070097 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070098
99 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700100 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700101
Colin Cross540eff82017-06-22 17:01:52 -0700102 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700103 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700104
105 // If not blank, set the java version passed to javac as -source and -target
106 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700107
108 // If set to false, don't allow this module to be installed. Defaults to true.
109 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700110
Colin Cross0f37af02017-09-27 17:42:05 -0700111 // If set to true, include sources used to compile the module in to the final jar
112 Include_srcs *bool
113
Colin Cross32f676a2017-09-06 13:41:06 -0700114 // List of modules to use as annotation processors
115 Annotation_processors []string
116
117 // List of classes to pass to javac to use as annotation processors
118 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119
Nan Zhang61eaedb2017-11-02 13:28:15 -0700120 // The number of Java source entries each Javac instance can process
121 Javac_shard_size *int64
122
Nan Zhang5f8cb422018-02-06 10:34:32 -0800123 // Add host jdk tools.jar to bootclasspath
124 Use_tools_jar *bool
125
Colin Cross1369cdb2017-09-29 17:58:17 -0700126 Openjdk9 struct {
127 // List of source files that should only be used when passing -source 1.9
128 Srcs []string
129
130 // List of javac flags that should only be used when passing -source 1.9
131 Javacflags []string
132 }
Colin Crosscb933592017-11-22 13:49:43 -0800133
134 Jacoco struct {
135 // List of classes to include for instrumentation with jacoco to collect coverage
136 // information at runtime when building with coverage enabled. If unset defaults to all
137 // classes.
138 // Supports '*' as the last character of an entry in the list as a wildcard match.
139 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
140 // it matches classes in the package that have the class name as a prefix.
141 Include_filter []string
142
143 // List of classes to exclude from instrumentation with jacoco to collect coverage
144 // information at runtime when building with coverage enabled. Overrides classes selected
145 // by the include_filter property.
146 // Supports '*' as the last character of an entry in the list as a wildcard match.
147 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
148 // it matches classes in the package that have the class name as a prefix.
149 Exclude_filter []string
150 }
151
Andreas Gampef3e5b552018-01-22 21:27:21 -0800152 Errorprone struct {
153 // List of javac flags that should only be used when running errorprone.
154 Javacflags []string
155 }
156
Colin Cross0f2ee152017-12-14 15:22:43 -0800157 Proto struct {
158 // List of extra options that will be passed to the proto generator.
159 Output_params []string
160 }
161
Colin Crosscb933592017-11-22 13:49:43 -0800162 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700163}
164
Colin Cross89536d42017-07-07 14:35:50 -0700165type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700166 // list of module-specific flags that will be used for dex compiles
167 Dxflags []string `android:"arch_variant"`
168
Colin Cross7d5136f2015-05-11 13:39:40 -0700169 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800170 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700171
Colin Crossebe1a512017-11-14 13:12:14 -0800172 Aidl struct {
173 // Top level directories to pass to aidl tool
174 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700175
Colin Crossebe1a512017-11-14 13:12:14 -0800176 // Directories rooted at the Android.bp file to pass to aidl tool
177 Local_include_dirs []string
178
179 // directories that should be added as include directories for any aidl sources of modules
180 // that depend on this module, as well as to aidl for this module.
181 Export_include_dirs []string
182 }
Colin Cross92430102017-10-09 14:59:32 -0700183
184 // If true, export a copy of the module as a -hostdex module for host testing.
185 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700186
Colin Cross1bd87802017-12-05 15:31:19 -0800187 Dex_preopt struct {
188 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
189 // true.
190 Enabled *bool
191
192 // If true, generate an app image (.art file) for this module.
193 App_image *bool
194
195 // If true, use a checked-in profile to guide optimization. Defaults to false unless
196 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
197 // that matches the name of this module, in which case it is defaulted to true.
198 Profile_guided *bool
199
200 // If set, provides the path to profile relative to the Android.bp file. If not set,
201 // defaults to searching for a file that matches the name of this module in the default
202 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
203 Profile *string
204 }
Colin Crossa22116e2017-10-19 14:18:58 -0700205
Colin Cross66dbc0b2017-12-28 12:23:20 -0800206 Optimize struct {
207 // If false, disable all optimization. Defaults to true for apps, false for
208 // libraries and tests.
209 Enabled *bool
210
211 // If true, optimize for size by removing unused code. Defaults to true for apps,
212 // false for libraries and tests.
213 Shrink *bool
214
215 // If true, optimize bytecode. Defaults to false.
216 Optimize *bool
217
218 // If true, obfuscate bytecode. Defaults to false.
219 Obfuscate *bool
220
221 // If true, do not use the flag files generated by aapt that automatically keep
222 // classes referenced by the app manifest. Defaults to false.
223 No_aapt_flags *bool
224
225 // Flags to pass to proguard.
226 Proguard_flags []string
227
228 // Specifies the locations of files containing proguard flags.
229 Proguard_flags_files []string
230 }
231
Colin Cross1369cdb2017-09-29 17:58:17 -0700232 // When targeting 1.9, override the modules to use with --system
233 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700234}
235
Colin Cross46c9b8b2017-06-22 16:51:17 -0700236// Module contains the properties and members used by all java module types
237type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700238 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700239 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700240
Colin Cross89536d42017-07-07 14:35:50 -0700241 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700242 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700243 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700244
Nan Zhanged19fc32017-10-19 13:06:22 -0700245 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
246 headerJarFile android.Path
247
248 // full implementation jar file suitable for static dependency of another module compile
249 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700250
Colin Cross6ade34f2017-09-15 13:00:47 -0700251 // output file containing classes.dex
252 dexJarFile android.Path
253
Colin Crosscb933592017-11-22 13:49:43 -0800254 // output file containing uninstrumented classes that will be instrumented by jacoco
255 jacocoReportClassesFile android.Path
256
Colin Cross66dbc0b2017-12-28 12:23:20 -0800257 // output file containing mapping of obfuscated names
258 proguardDictionary android.Path
259
Colin Crossb7a63242015-04-16 14:09:14 -0700260 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700261 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700262
Colin Cross635c3b02016-05-18 15:37:25 -0700263 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700264
Colin Cross635c3b02016-05-18 15:37:25 -0700265 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700266
Colin Cross2fe66872015-03-30 17:20:39 -0700267 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700268 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800269
270 // list of .java files and srcjars that was passed to javac
271 compiledJavaSrcs android.Paths
272 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800273
274 // list of extra progurad flag files
275 extraProguardFlagFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700276}
277
Colin Cross54250902017-12-05 09:28:08 -0800278func (j *Module) Srcs() android.Paths {
279 return android.Paths{j.implementationJarFile}
280}
281
282var _ android.SourceFileProducer = (*Module)(nil)
283
Colin Crossf506d872017-07-19 15:53:04 -0700284type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700285 HeaderJars() android.Paths
286 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700287 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700288}
289
Nan Zhangb2b33de2018-02-23 11:18:47 -0800290type SrcDependency interface {
291 CompiledSrcs() android.Paths
292 CompiledSrcJars() android.Paths
293}
294
295func (j *Module) CompiledSrcs() android.Paths {
296 return j.compiledJavaSrcs
297}
298
299func (j *Module) CompiledSrcJars() android.Paths {
300 return j.compiledSrcJars
301}
302
303var _ SrcDependency = (*Module)(nil)
304
Colin Cross89536d42017-07-07 14:35:50 -0700305func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
306 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
307 android.InitDefaultableModule(module)
308}
309
Colin Crossbe1da472017-07-07 15:59:46 -0700310type dependencyTag struct {
311 blueprint.BaseDependencyTag
312 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700313}
314
Colin Crossbe1da472017-07-07 15:59:46 -0700315var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700316 staticLibTag = dependencyTag{name: "staticlib"}
317 libTag = dependencyTag{name: "javalib"}
318 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700319 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700320 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800321 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700322 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800323 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700324)
Colin Cross2fe66872015-03-30 17:20:39 -0700325
Colin Crossfc3674a2017-09-18 17:41:52 -0700326type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700327 useModule, useFiles, useDefaultLibs, invalidVersion bool
328
Colin Cross1369cdb2017-09-29 17:58:17 -0700329 module string
330 systemModules string
331
332 jar android.Path
333 aidl android.Path
334}
335
336func sdkStringToNumber(ctx android.BaseContext, v string) int {
337 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900338 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900339 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700340 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900341 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700342 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
343 return -1
344 } else {
345 return i
346 }
347 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700348}
349
Colin Cross3144dfc2018-01-03 15:06:47 -0800350func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
351 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
352}
353
354func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
355 return j.shouldInstrument(ctx) &&
356 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
357 ctx.Config().UnbundledBuild())
358}
359
Colin Crossfc3674a2017-09-18 17:41:52 -0700360func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700361 i := sdkStringToNumber(ctx, v)
362 if i == -1 {
363 // Invalid sdk version, error handled by sdkStringToNumber.
364 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700365 }
366
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900367 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
368 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
369 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
370 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
371 if ctx.DeviceSpecific() || ctx.SocSpecific() {
372 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
373 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
374 }
375 }
376 version := strings.TrimPrefix(v, "system_")
377 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
378 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
379 v, allowed_versions)
380 }
381 }
382
Colin Crossfc3674a2017-09-18 17:41:52 -0700383 toFile := func(v string) sdkDep {
Jiyong Park750e5572018-01-31 00:20:13 +0900384 isCore := strings.HasPrefix(v, "core_")
385 if isCore {
386 v = strings.TrimPrefix(v, "core_")
387 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700388 dir := filepath.Join("prebuilts/sdk", v)
389 jar := filepath.Join(dir, "android.jar")
Jiyong Park750e5572018-01-31 00:20:13 +0900390 if isCore {
391 jar = filepath.Join(dir, "core.jar")
392 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700393 aidl := filepath.Join(dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800394 jarPath := android.ExistentPathForSource(ctx, jar)
395 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700396
Colin Cross6510f912017-11-29 00:27:14 -0800397 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700398 return sdkDep{
399 invalidVersion: true,
400 module: "sdk_v" + v,
401 }
402 }
403
Colin Crossfc3674a2017-09-18 17:41:52 -0700404 if !jarPath.Valid() {
405 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
406 return sdkDep{}
407 }
Colin Cross47ff2522017-10-02 14:22:08 -0700408
Colin Crossfc3674a2017-09-18 17:41:52 -0700409 if !aidlPath.Valid() {
410 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
411 return sdkDep{}
412 }
Colin Cross47ff2522017-10-02 14:22:08 -0700413
Colin Crossfc3674a2017-09-18 17:41:52 -0700414 return sdkDep{
415 useFiles: true,
416 jar: jarPath.Path(),
417 aidl: aidlPath.Path(),
418 }
419 }
420
Colin Cross2ebc4762017-10-20 14:00:31 -0700421 //toModule := func(m string) sdkDep {
422 // return sdkDep{
423 // useModule: true,
424 // module: m,
425 // systemModules: m + "_system_modules",
426 // }
427 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700428
Colin Cross6510f912017-11-29 00:27:14 -0800429 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700430 return toFile(v)
431 }
432
433 switch v {
434 case "":
435 return sdkDep{
436 useDefaultLibs: true,
437 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700438 // TODO(ccross): re-enable these once we generate stubs, until then
439 // use the stubs in prebuilts/sdk/*current
440 //case "current":
441 // return toModule("android_stubs_current")
442 //case "system_current":
443 // return toModule("android_system_stubs_current")
444 //case "test_current":
445 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700446 default:
447 return toFile(v)
448 }
449}
450
Colin Crossbe1da472017-07-07 15:59:46 -0700451func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700452 if ctx.Device() {
453 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800454 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700455 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700456 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800457 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700458 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
459 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700460 if !proptools.Bool(j.properties.No_framework_libs) {
461 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
462 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700463 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800464 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700465 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
466 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700467 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800468 if Bool(j.deviceProperties.Optimize.Enabled) {
469 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
470 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
471 }
Colin Crossbe1da472017-07-07 15:59:46 -0700472 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700473 } else if j.deviceProperties.System_modules == nil {
474 ctx.PropertyErrorf("no_standard_libs",
475 "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 -0800476 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700477 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700478 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800479 if ctx.ModuleName() == "framework" {
480 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
481 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800482 if ctx.ModuleName() == "android_stubs_current" ||
483 ctx.ModuleName() == "android_system_stubs_current" ||
484 ctx.ModuleName() == "android_test_stubs_current" {
485 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
486 }
Colin Cross2fe66872015-03-30 17:20:39 -0700487 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700488
Colin Crossf506d872017-07-19 15:53:04 -0700489 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
490 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700491 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700492
493 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000494 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700495 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800496 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700497
498 if j.hasSrcExt(".proto") {
499 protoDeps(ctx, &j.protoProperties)
500 }
Colin Cross93e85952017-08-15 13:34:18 -0700501
502 if j.hasSrcExt(".kt") {
503 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
504 // Kotlin files
505 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
506 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800507
508 if j.shouldInstrumentStatic(ctx) {
509 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
510 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700511}
512
513func hasSrcExt(srcs []string, ext string) bool {
514 for _, src := range srcs {
515 if filepath.Ext(src) == ext {
516 return true
517 }
518 }
519
520 return false
521}
522
Nan Zhang61eaedb2017-11-02 13:28:15 -0700523func shardPaths(paths android.Paths, shardSize int) []android.Paths {
524 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
525 for len(paths) > shardSize {
526 ret = append(ret, paths[0:shardSize])
527 paths = paths[shardSize:]
528 }
529 if len(paths) > 0 {
530 ret = append(ret, paths)
531 }
532 return ret
533}
534
Colin Cross6af17aa2017-09-20 12:59:05 -0700535func (j *Module) hasSrcExt(ext string) bool {
536 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700537}
538
Colin Cross46c9b8b2017-06-22 16:51:17 -0700539func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700540 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700541
Colin Crossebe1a512017-11-14 13:12:14 -0800542 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
543 aidlIncludes = append(aidlIncludes,
544 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
545 aidlIncludes = append(aidlIncludes,
546 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700547
548 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700549 if aidlPreprocess.Valid() {
550 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700551 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700552 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700553 }
554
Colin Cross635c3b02016-05-18 15:37:25 -0700555 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800556 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700557 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800558 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700559 flags = append(flags, "-I"+src.String())
560 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700561
Colin Crossf03c82b2015-04-13 13:53:40 -0700562 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700563}
564
Colin Cross32f676a2017-09-06 13:41:06 -0700565type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800566 classpath classpath
567 bootClasspath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700568 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700569 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700570 staticJarResources android.Paths
571 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800572 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700573 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700574 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700575 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700576 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700577}
Colin Cross2fe66872015-03-30 17:20:39 -0700578
Colin Cross54250902017-12-05 09:28:08 -0800579func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
580 for _, f := range dep.Srcs() {
581 if f.Ext() != ".jar" {
582 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
583 ctx.OtherModuleName(dep.(blueprint.Module)))
584 }
585 }
586}
587
Jiyong Park750e5572018-01-31 00:20:13 +0900588func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
589 if strings.HasPrefix(String(from.deviceProperties.Sdk_version), "core_") {
590 if !strings.HasPrefix(String(to.deviceProperties.Sdk_version), "core_") {
591 ctx.ModuleErrorf("depends on other library %q using non-core Java APIs",
592 ctx.OtherModuleName(to))
593 }
594 }
595}
596
Colin Cross32f676a2017-09-06 13:41:06 -0700597func (j *Module) collectDeps(ctx android.ModuleContext) deps {
598 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700599
Colin Cross300f0382018-03-06 13:11:51 -0800600 if ctx.Device() {
601 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
602 if sdkDep.invalidVersion {
603 ctx.AddMissingDependencies([]string{sdkDep.module})
604 } else if sdkDep.useFiles {
605 // sdkDep.jar is actually equivalent to turbine header.jar.
606 deps.classpath = append(deps.classpath, sdkDep.jar)
607 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
608 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700609 }
610
Colin Crossd11fcda2017-10-23 17:59:01 -0700611 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700612 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700613 tag := ctx.OtherModuleDependencyTag(module)
614
Jiyong Park750e5572018-01-31 00:20:13 +0900615 if to, ok := module.(*Library); ok {
616 checkLinkType(ctx, j, to, tag.(dependencyTag))
617 }
Colin Cross54250902017-12-05 09:28:08 -0800618 switch dep := module.(type) {
619 case Dependency:
620 switch tag {
621 case bootClasspathTag:
622 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
623 case libTag:
624 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
625 case staticLibTag:
626 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
627 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
628 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
629 case frameworkResTag:
630 if ctx.ModuleName() == "framework" {
631 // framework.jar has a one-off dependency on the R.java and Manifest.java files
632 // generated by framework-res.apk
633 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
634 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800635 case frameworkApkTag:
636 if ctx.ModuleName() == "android_stubs_current" ||
637 ctx.ModuleName() == "android_system_stubs_current" ||
638 ctx.ModuleName() == "android_test_stubs_current" {
639 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
640 // resource files out of there for aapt.
641 //
642 // Normally the package rule runs aapt, which includes the resource,
643 // but we're not running that in our package rule so just copy in the
644 // resource files here.
645 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
646 }
Colin Cross54250902017-12-05 09:28:08 -0800647 case kotlinStdlibTag:
648 deps.kotlinStdlib = dep.HeaderJars()
649 default:
650 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
651 }
652
653 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
654 case android.SourceFileProducer:
655 switch tag {
656 case libTag:
657 checkProducesJars(ctx, dep)
658 deps.classpath = append(deps.classpath, dep.Srcs()...)
659 case staticLibTag:
660 checkProducesJars(ctx, dep)
661 deps.classpath = append(deps.classpath, dep.Srcs()...)
662 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
663 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
664 case android.DefaultsDepTag, android.SourceDepTag:
665 // Nothing to do
666 default:
667 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
668 }
669 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700670 switch tag {
671 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700672 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700673 case systemModulesTag:
674 if deps.systemModules != nil {
675 panic("Found two system module dependencies")
676 }
677 sm := module.(*SystemModules)
678 if sm.outputFile == nil {
679 panic("Missing directory for system module dependency")
680 }
681 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700682 default:
683 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700684 }
Colin Crossec7a0422017-07-07 14:47:12 -0700685 }
Colin Cross2fe66872015-03-30 17:20:39 -0700686 })
687
Colin Cross32f676a2017-09-06 13:41:06 -0700688 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700689}
690
Nan Zhanged19fc32017-10-19 13:06:22 -0700691func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700692
Colin Crossf03c82b2015-04-13 13:53:40 -0700693 var flags javaBuilderFlags
694
Nan Zhanged19fc32017-10-19 13:06:22 -0700695 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700696 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800697 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700698 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700699 }
Colin Cross6510f912017-11-29 00:27:14 -0800700 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700701 // Override the -g flag passed globally to remove local variable debug info to reduce
702 // disk and memory usage.
703 javacFlags = append(javacFlags, "-g:source,lines")
704 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700705 if len(javacFlags) > 0 {
706 // optimization.
707 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
708 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700709 }
Colin Cross64162712017-08-08 13:17:59 -0700710
Andreas Gampef3e5b552018-01-22 21:27:21 -0800711 if len(j.properties.Errorprone.Javacflags) > 0 {
712 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
713 }
714
Nan Zhanged19fc32017-10-19 13:06:22 -0700715 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800716 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700717 if j.properties.Java_version != nil {
718 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700719 } else if ctx.Device() && sdk <= 23 {
720 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800721 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700722 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900723 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700724 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
725 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700726 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700727 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700728 }
729
Nan Zhanged19fc32017-10-19 13:06:22 -0700730 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800731 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
732 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800733
734 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
735 !Bool(j.properties.No_standard_libs) &&
736 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
737 // Give host-side tools a version of OpenJDK's standard libraries
738 // close to what they're targeting. As of Dec 2017, AOSP is only
739 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
740 //
741 // When building with OpenJDK 8, the following should have no
742 // effect since those jars would be available by default.
743 //
744 // When building with OpenJDK 9 but targeting a version < 1.8,
745 // putting them on the bootclasspath means that:
746 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
747 // b) references to existing APIs are not reinterpreted in an
748 // OpenJDK 9-specific way, eg. calls to subclasses of
749 // java.nio.Buffer as in http://b/70862583
750 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
751 flags.bootClasspath = append(flags.bootClasspath,
752 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
753 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800754 if Bool(j.properties.Use_tools_jar) {
755 flags.bootClasspath = append(flags.bootClasspath,
756 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
757 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800758 }
759
Nan Zhanged19fc32017-10-19 13:06:22 -0700760 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700761 if deps.systemModules != nil {
762 flags.systemModules = append(flags.systemModules, deps.systemModules)
763 }
764
Nan Zhanged19fc32017-10-19 13:06:22 -0700765 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700766 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700767 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700768 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700769 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
770 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700771 }
772
Nan Zhanged19fc32017-10-19 13:06:22 -0700773 return flags
774}
Colin Crossc0b06f12015-04-08 13:03:43 -0700775
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800776func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700777
Colin Crossebe1a512017-11-14 13:12:14 -0800778 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700779
780 deps := j.collectDeps(ctx)
781 flags := j.collectBuilderFlags(ctx, deps)
782
Colin Cross6510f912017-11-29 00:27:14 -0800783 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700784 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
785 }
786 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700787 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800788 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700789 }
790
Colin Crossaf050172017-11-15 23:01:59 -0800791 srcFiles = j.genSources(ctx, srcFiles, flags)
792
793 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700794 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800795 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700796
Colin Cross0a6e0072017-08-30 14:24:55 -0700797 var jars android.Paths
798
Colin Cross1ee23172017-10-18 14:44:18 -0700799 jarName := ctx.ModuleName() + ".jar"
800
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000801 javaSrcFiles := srcFiles.FilterByExt(".java")
802 var uniqueSrcFiles android.Paths
803 set := make(map[string]bool)
804 for _, v := range javaSrcFiles {
805 if _, found := set[v.String()]; !found {
806 set[v.String()] = true
807 uniqueSrcFiles = append(uniqueSrcFiles, v)
808 }
809 }
810
Colin Cross93e85952017-08-15 13:34:18 -0700811 if srcFiles.HasExt(".kt") {
812 // If there are kotlin files, compile them first but pass all the kotlin and java files
813 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
814 // won't emit any classes for them.
815
816 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000817
Colin Cross93e85952017-08-15 13:34:18 -0700818 if ctx.Device() {
819 flags.kotlincFlags += " -no-jdk"
820 }
821
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000822 var kotlinSrcFiles android.Paths
823 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
824 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
825
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000826 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700827 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
828 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
829
Colin Cross1ee23172017-10-18 14:44:18 -0700830 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000831 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700832 if ctx.Failed() {
833 return
834 }
835
836 // Make javac rule depend on the kotlinc rule
837 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000838
Colin Cross93e85952017-08-15 13:34:18 -0700839 // Jar kotlin classes into the final jar after javac
840 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000841
842 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
843 // (it's expected to be on device bootclasspath)
844 if !proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
845 jars = append(jars, deps.kotlinStdlib...)
846 }
Colin Cross93e85952017-08-15 13:34:18 -0700847 }
848
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800849 // Store the list of .java files that was passed to javac
850 j.compiledJavaSrcs = uniqueSrcFiles
851 j.compiledSrcJars = srcJars
852
Nan Zhang61eaedb2017-11-02 13:28:15 -0700853 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800854 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700855 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
856 enable_sharding = true
857 if len(j.properties.Annotation_processors) != 0 ||
858 len(j.properties.Annotation_processor_classes) != 0 {
859 ctx.PropertyErrorf("javac_shard_size",
860 "%q cannot be set when annotation processors are enabled.",
861 j.properties.Javac_shard_size)
862 }
863 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700864 // If sdk jar is java module, then directly return classesJar as header.jar
865 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
866 j.Name() != "android_test_stubs_current" {
867 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
868 if ctx.Failed() {
869 return
870 }
871 }
872 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700873 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700874 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800875 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700876 // If error-prone is enabled, add an additional rule to compile the java files into
877 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700878 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700879 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
880 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700881 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700882 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700883 extraJarDeps = append(extraJarDeps, errorprone)
884 }
885
Nan Zhang61eaedb2017-11-02 13:28:15 -0700886 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800887 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700888 shardSize := int(*(j.properties.Javac_shard_size))
889 var shardSrcs []android.Paths
890 if len(uniqueSrcFiles) > 0 {
891 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
892 for idx, shardSrc := range shardSrcs {
893 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
894 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
895 jars = append(jars, classes)
896 }
897 }
898 if len(srcJars) > 0 {
899 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
900 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
901 jars = append(jars, classes)
902 }
903 } else {
904 classes := android.PathForModuleOut(ctx, "javac", jarName)
905 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
906 jars = append(jars, classes)
907 }
Colin Crossd6891432017-09-27 17:39:56 -0700908 if ctx.Failed() {
909 return
910 }
Colin Cross2fe66872015-03-30 17:20:39 -0700911 }
912
Colin Cross0f37af02017-09-27 17:42:05 -0700913 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
914 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
915
916 var resArgs []string
917 var resDeps android.Paths
918
919 resArgs = append(resArgs, dirArgs...)
920 resDeps = append(resDeps, dirDeps...)
921
922 resArgs = append(resArgs, fileArgs...)
923 resDeps = append(resDeps, fileDeps...)
924
925 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700926 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700927 resArgs = append(resArgs, srcArgs...)
928 resDeps = append(resDeps, srcDeps...)
929 }
Colin Cross40a36712017-09-27 17:41:35 -0700930
931 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700932 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700933 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700934 if ctx.Failed() {
935 return
936 }
Colin Cross20978302015-04-10 17:05:07 -0700937
Colin Cross0a6e0072017-08-30 14:24:55 -0700938 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700939 }
940
Colin Cross6ade34f2017-09-15 13:00:47 -0700941 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700942 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800943 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700944
Colin Cross366938f2017-12-11 16:29:02 -0800945 var manifest android.OptionalPath
946 if j.properties.Manifest != nil {
947 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
948 }
Colin Cross635acc92017-09-12 22:50:46 -0700949
Colin Cross0a6e0072017-08-30 14:24:55 -0700950 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700951 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700952 var outputFile android.Path
953
954 if len(jars) == 1 && !manifest.Valid() {
955 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -0800956 // TODO(ccross): this leaves any module-info.class files, but those should only come from
957 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
958 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -0700959 outputFile = jars[0]
960 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700961 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700962 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700963 outputFile = combinedJar
964 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700965
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000966 // Use renamed kotlin standard library?
967 if srcFiles.HasExt(".kt") && proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
968 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
969 TransformJarJar(ctx, jarjarFile, outputFile,
970 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
971 outputFile = jarjarFile
972 if ctx.Failed() {
973 return
974 }
975 }
976
Colin Cross0a6e0072017-08-30 14:24:55 -0700977 if j.properties.Jarjar_rules != nil {
978 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700979 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700980 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700981 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
982 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700983 if ctx.Failed() {
984 return
985 }
986 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700987 j.implementationJarFile = outputFile
988 if j.headerJarFile == nil {
989 j.headerJarFile = j.implementationJarFile
990 }
Colin Cross2fe66872015-03-30 17:20:39 -0700991
Colin Cross6510f912017-11-29 00:27:14 -0800992 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -0800993 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
994 j.properties.Instrument = true
995 }
996 }
997
Colin Cross3144dfc2018-01-03 15:06:47 -0800998 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -0800999 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1000 }
1001
1002 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001003 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001004 if ctx.Failed() {
1005 return
1006 }
Colin Cross2fe66872015-03-30 17:20:39 -07001007 }
Colin Crossb7a63242015-04-16 14:09:14 -07001008 ctx.CheckbuildFile(outputFile)
1009 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001010}
1011
Colin Cross8eadbf02017-10-24 17:46:00 -07001012func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001013 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1014
1015 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001016 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001017 // Compile java sources into turbine.jar.
1018 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1019 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1020 if ctx.Failed() {
1021 return nil
1022 }
1023 jars = append(jars, turbineJar)
1024 }
1025
1026 // Combine any static header libraries into classes-header.jar. If there is only
1027 // one input jar this step will be skipped.
1028 var headerJar android.Path
1029 jars = append(jars, deps.staticHeaderJars...)
1030
Colin Cross5c6ecc12017-10-23 18:12:27 -07001031 // we cannot skip the combine step for now if there is only one jar
1032 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1033 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1034 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1035 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001036
1037 if j.properties.Jarjar_rules != nil {
1038 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1039 // Transform classes.jar into classes-jarjar.jar
1040 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1041 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1042 headerJar = jarjarFile
1043 if ctx.Failed() {
1044 return nil
1045 }
1046 }
1047
1048 return headerJar
1049}
1050
Colin Crosscb933592017-11-22 13:49:43 -08001051func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1052 classesJar android.Path, jarName string) android.Path {
1053
Colin Cross7a3139e2017-12-19 13:57:50 -08001054 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001055
Colin Cross84c38822018-01-03 15:59:46 -08001056 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001057 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1058
1059 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1060
1061 j.jacocoReportClassesFile = jacocoReportClassesFile
1062
1063 return instrumentedJar
1064}
1065
Colin Crosscb933592017-11-22 13:49:43 -08001066// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1067// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1068func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1069 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001070 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001071 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001072 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001073 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001074 }
1075}
1076
Colin Cross59f1bb62017-09-27 17:59:10 -07001077func (j *Module) installable() bool {
1078 return j.properties.Installable == nil || *j.properties.Installable
1079}
1080
Colin Crossf506d872017-07-19 15:53:04 -07001081var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001082
Nan Zhanged19fc32017-10-19 13:06:22 -07001083func (j *Module) HeaderJars() android.Paths {
1084 return android.Paths{j.headerJarFile}
1085}
1086
1087func (j *Module) ImplementationJars() android.Paths {
1088 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001089}
1090
Colin Cross46c9b8b2017-06-22 16:51:17 -07001091func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001092 return j.exportAidlIncludeDirs
1093}
1094
Colin Cross46c9b8b2017-06-22 16:51:17 -07001095var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001096
Colin Cross46c9b8b2017-06-22 16:51:17 -07001097func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001098 return j.logtagsSrcs
1099}
1100
Colin Cross2fe66872015-03-30 17:20:39 -07001101//
1102// Java libraries (.jar file)
1103//
1104
Colin Crossf506d872017-07-19 15:53:04 -07001105type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001106 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001107}
1108
Colin Crossf506d872017-07-19 15:53:04 -07001109func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001110 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001111
Colin Cross59f1bb62017-09-27 17:59:10 -07001112 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001113 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1114 ctx.ModuleName()+".jar", j.outputFile)
1115 }
Colin Crossb7a63242015-04-16 14:09:14 -07001116}
1117
Colin Crossf506d872017-07-19 15:53:04 -07001118func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001119 j.deps(ctx)
1120}
1121
Colin Crossa60ead82017-10-02 18:10:21 -07001122func LibraryFactory(installable bool) func() android.Module {
1123 return func() android.Module {
1124 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001125
Colin Crossa60ead82017-10-02 18:10:21 -07001126 if !installable {
1127 module.properties.Installable = proptools.BoolPtr(false)
1128 }
Colin Cross2fe66872015-03-30 17:20:39 -07001129
Colin Crossa60ead82017-10-02 18:10:21 -07001130 module.AddProperties(
1131 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001132 &module.Module.deviceProperties,
1133 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001134
Colin Crossa60ead82017-10-02 18:10:21 -07001135 InitJavaModule(module, android.HostAndDeviceSupported)
1136 return module
1137 }
Colin Cross2fe66872015-03-30 17:20:39 -07001138}
1139
Colin Crossf506d872017-07-19 15:53:04 -07001140func LibraryHostFactory() android.Module {
1141 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001142
Colin Cross6af17aa2017-09-20 12:59:05 -07001143 module.AddProperties(
1144 &module.Module.properties,
1145 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001146
Colin Cross89536d42017-07-07 14:35:50 -07001147 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001148 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001149}
1150
1151//
1152// Java Binaries (.jar file plus wrapper script)
1153//
1154
Colin Crossf506d872017-07-19 15:53:04 -07001155type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001156 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001157 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001158}
1159
Colin Crossf506d872017-07-19 15:53:04 -07001160type Binary struct {
1161 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001162
Colin Crossf506d872017-07-19 15:53:04 -07001163 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001164
Colin Cross6b4a32d2017-12-05 13:42:45 -08001165 isWrapperVariant bool
1166
Colin Crossc3315992017-12-08 19:12:36 -08001167 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001168 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001169}
1170
Alex Light24237172017-10-26 09:46:21 -07001171func (j *Binary) HostToolPath() android.OptionalPath {
1172 return android.OptionalPathForPath(j.binaryFile)
1173}
1174
Colin Crossf506d872017-07-19 15:53:04 -07001175func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001176 if ctx.Arch().ArchType == android.Common {
1177 // Compile the jar
1178 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001179 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001180 // Handle the binary wrapper
1181 j.isWrapperVariant = true
1182
Colin Cross366938f2017-12-11 16:29:02 -08001183 if j.binaryProperties.Wrapper != nil {
1184 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001185 } else {
1186 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1187 }
1188
1189 // Depend on the installed jar so that the wrapper doesn't get executed by
1190 // another build rule before the jar has been installed.
1191 jarFile := ctx.PrimaryModule().(*Binary).installFile
1192
1193 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1194 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001195 }
Colin Cross2fe66872015-03-30 17:20:39 -07001196}
1197
Colin Crossf506d872017-07-19 15:53:04 -07001198func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001199 if ctx.Arch().ArchType == android.Common {
1200 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001201 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001202 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001203 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001204}
1205
Colin Crossf506d872017-07-19 15:53:04 -07001206func BinaryFactory() android.Module {
1207 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001208
Colin Cross36242852017-06-23 15:06:31 -07001209 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001210 &module.Module.properties,
1211 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001212 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001213 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001214
Colin Cross6b4a32d2017-12-05 13:42:45 -08001215 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1216 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001217 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001218}
1219
Colin Crossf506d872017-07-19 15:53:04 -07001220func BinaryHostFactory() android.Module {
1221 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001222
Colin Cross36242852017-06-23 15:06:31 -07001223 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001224 &module.Module.properties,
1225 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001226 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001227 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001228
Colin Cross6b4a32d2017-12-05 13:42:45 -08001229 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1230 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001231 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001232}
1233
1234//
1235// Java prebuilts
1236//
1237
Colin Cross74d73e22017-08-02 11:05:49 -07001238type ImportProperties struct {
1239 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001240
Nan Zhangea568a42017-11-08 21:20:04 -08001241 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001242
1243 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001244}
1245
1246type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001247 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001248 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001249
Colin Cross74d73e22017-08-02 11:05:49 -07001250 properties ImportProperties
1251
Colin Cross0a6e0072017-08-30 14:24:55 -07001252 classpathFiles android.Paths
1253 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001254}
1255
Colin Cross74d73e22017-08-02 11:05:49 -07001256func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001257 return &j.prebuilt
1258}
1259
Colin Cross74d73e22017-08-02 11:05:49 -07001260func (j *Import) PrebuiltSrcs() []string {
1261 return j.properties.Jars
1262}
1263
1264func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001265 return j.prebuilt.Name(j.ModuleBase.Name())
1266}
1267
Colin Cross74d73e22017-08-02 11:05:49 -07001268func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001269}
1270
Colin Cross74d73e22017-08-02 11:05:49 -07001271func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1272 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001273
Colin Crosse9a275b2017-10-16 17:09:48 -07001274 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001275 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001276 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001277}
1278
Colin Cross74d73e22017-08-02 11:05:49 -07001279var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001280
Nan Zhanged19fc32017-10-19 13:06:22 -07001281func (j *Import) HeaderJars() android.Paths {
1282 return j.classpathFiles
1283}
1284
1285func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001286 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001287}
1288
Colin Cross74d73e22017-08-02 11:05:49 -07001289func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001290 return nil
1291}
1292
Colin Cross74d73e22017-08-02 11:05:49 -07001293var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001294
Colin Cross74d73e22017-08-02 11:05:49 -07001295func ImportFactory() android.Module {
1296 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001297
Colin Cross74d73e22017-08-02 11:05:49 -07001298 module.AddProperties(&module.properties)
1299
1300 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001301 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1302 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001303}
1304
Colin Cross74d73e22017-08-02 11:05:49 -07001305func ImportFactoryHost() android.Module {
1306 module := &Import{}
1307
1308 module.AddProperties(&module.properties)
1309
1310 android.InitPrebuiltModule(module, &module.properties.Jars)
1311 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1312 return module
1313}
1314
Colin Cross89536d42017-07-07 14:35:50 -07001315//
1316// Defaults
1317//
1318type Defaults struct {
1319 android.ModuleBase
1320 android.DefaultsModuleBase
1321}
1322
1323func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1324}
1325
1326func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1327}
1328
1329func defaultsFactory() android.Module {
1330 return DefaultsFactory()
1331}
1332
1333func DefaultsFactory(props ...interface{}) android.Module {
1334 module := &Defaults{}
1335
1336 module.AddProperties(props...)
1337 module.AddProperties(
1338 &CompilerProperties{},
1339 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001340 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001341 )
1342
1343 android.InitDefaultsModule(module)
1344
1345 return module
1346}
Nan Zhangea568a42017-11-08 21:20:04 -08001347
1348var Bool = proptools.Bool
1349var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001350var inList = android.InList