blob: 3feb8382835b5abd013f40428d575258cba5509c [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 Crossfc3674a2017-09-18 17:41:52 -070022 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070023 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070030 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross89536d42017-07-07 14:35:50 -070034 android.RegisterModuleType("java_defaults", defaultsFactory)
35
Colin Crossa60ead82017-10-02 18:10:21 -070036 android.RegisterModuleType("java_library", LibraryFactory(true))
37 android.RegisterModuleType("java_library_static", LibraryFactory(false))
Colin Crossf506d872017-07-19 15:53:04 -070038 android.RegisterModuleType("java_library_host", LibraryHostFactory)
39 android.RegisterModuleType("java_binary", BinaryFactory)
40 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070041 android.RegisterModuleType("java_import", ImportFactory)
42 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070045}
46
Colin Cross2fe66872015-03-30 17:20:39 -070047// TODO:
48// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070049// Renderscript
50// Post-jar passes:
51// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070052// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070053// DroidDoc
54// Findbugs
55
Colin Cross89536d42017-07-07 14:35:50 -070056type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070057 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
58 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070059 Srcs []string `android:"arch_variant"`
60
61 // list of source files that should not be used to build the Java module.
62 // This is most useful in the arch/multilib variants to remove non-common files
63 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070064
65 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070066 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070067
Colin Cross86a63ff2017-09-27 17:33:10 -070068 // list of directories that should be excluded from java_resource_dirs
69 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070070
Colin Cross0f37af02017-09-27 17:42:05 -070071 // list of files to use as Java resources
72 Java_resources []string `android:"arch_variant"`
73
74 // list of files that should be excluded from java_resources
75 Exclude_java_resources []string `android:"arch_variant"`
76
Colin Crossfa5eb232017-10-01 20:33:03 -070077 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070078 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070079 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070080
Colin Crossfa5eb232017-10-01 20:33:03 -070081 // don't build against the framework libraries (legacy-test, core-junit,
82 // ext, and framework for device targets)
83 No_framework_libs *bool
84
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +000085 // Use renamed kotlin stdlib (com.android.kotlin.*). This allows kotlin usage without colliding
86 // with app-provided kotlin stdlib.
87 Renamed_kotlin_stdlib *bool
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of module-specific flags that will be used for javac compiles
90 Javacflags []string `android:"arch_variant"`
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
Colin Cross540eff82017-06-22 17:01:52 -0700101 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700102 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700103
104 // If not blank, set the java version passed to javac as -source and -target
105 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700106
107 // If set to false, don't allow this module to be installed. Defaults to true.
108 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700109
Colin Cross0f37af02017-09-27 17:42:05 -0700110 // If set to true, include sources used to compile the module in to the final jar
111 Include_srcs *bool
112
Colin Cross32f676a2017-09-06 13:41:06 -0700113 // List of modules to use as annotation processors
114 Annotation_processors []string
115
116 // List of classes to pass to javac to use as annotation processors
117 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700118
Nan Zhang61eaedb2017-11-02 13:28:15 -0700119 // The number of Java source entries each Javac instance can process
120 Javac_shard_size *int64
121
Nan Zhang5f8cb422018-02-06 10:34:32 -0800122 // Add host jdk tools.jar to bootclasspath
123 Use_tools_jar *bool
124
Colin Cross1369cdb2017-09-29 17:58:17 -0700125 Openjdk9 struct {
126 // List of source files that should only be used when passing -source 1.9
127 Srcs []string
128
129 // List of javac flags that should only be used when passing -source 1.9
130 Javacflags []string
131 }
Colin Crosscb933592017-11-22 13:49:43 -0800132
133 Jacoco struct {
134 // List of classes to include for instrumentation with jacoco to collect coverage
135 // information at runtime when building with coverage enabled. If unset defaults to all
136 // classes.
137 // Supports '*' as the last character of an entry in the list as a wildcard match.
138 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
139 // it matches classes in the package that have the class name as a prefix.
140 Include_filter []string
141
142 // List of classes to exclude from instrumentation with jacoco to collect coverage
143 // information at runtime when building with coverage enabled. Overrides classes selected
144 // by the include_filter property.
145 // Supports '*' as the last character of an entry in the list as a wildcard match.
146 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
147 // it matches classes in the package that have the class name as a prefix.
148 Exclude_filter []string
149 }
150
Andreas Gampef3e5b552018-01-22 21:27:21 -0800151 Errorprone struct {
152 // List of javac flags that should only be used when running errorprone.
153 Javacflags []string
154 }
155
Colin Cross0f2ee152017-12-14 15:22:43 -0800156 Proto struct {
157 // List of extra options that will be passed to the proto generator.
158 Output_params []string
159 }
160
Colin Crosscb933592017-11-22 13:49:43 -0800161 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700162}
163
Colin Cross89536d42017-07-07 14:35:50 -0700164type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700165 // list of module-specific flags that will be used for dex compiles
166 Dxflags []string `android:"arch_variant"`
167
Colin Cross7d5136f2015-05-11 13:39:40 -0700168 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800169 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700170
Colin Crossebe1a512017-11-14 13:12:14 -0800171 Aidl struct {
172 // Top level directories to pass to aidl tool
173 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700174
Colin Crossebe1a512017-11-14 13:12:14 -0800175 // Directories rooted at the Android.bp file to pass to aidl tool
176 Local_include_dirs []string
177
178 // directories that should be added as include directories for any aidl sources of modules
179 // that depend on this module, as well as to aidl for this module.
180 Export_include_dirs []string
181 }
Colin Cross92430102017-10-09 14:59:32 -0700182
183 // If true, export a copy of the module as a -hostdex module for host testing.
184 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700185
Colin Cross1bd87802017-12-05 15:31:19 -0800186 Dex_preopt struct {
187 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
188 // true.
189 Enabled *bool
190
191 // If true, generate an app image (.art file) for this module.
192 App_image *bool
193
194 // If true, use a checked-in profile to guide optimization. Defaults to false unless
195 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
196 // that matches the name of this module, in which case it is defaulted to true.
197 Profile_guided *bool
198
199 // If set, provides the path to profile relative to the Android.bp file. If not set,
200 // defaults to searching for a file that matches the name of this module in the default
201 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
202 Profile *string
203 }
Colin Crossa22116e2017-10-19 14:18:58 -0700204
Colin Cross66dbc0b2017-12-28 12:23:20 -0800205 Optimize struct {
206 // If false, disable all optimization. Defaults to true for apps, false for
207 // libraries and tests.
208 Enabled *bool
209
210 // If true, optimize for size by removing unused code. Defaults to true for apps,
211 // false for libraries and tests.
212 Shrink *bool
213
214 // If true, optimize bytecode. Defaults to false.
215 Optimize *bool
216
217 // If true, obfuscate bytecode. Defaults to false.
218 Obfuscate *bool
219
220 // If true, do not use the flag files generated by aapt that automatically keep
221 // classes referenced by the app manifest. Defaults to false.
222 No_aapt_flags *bool
223
224 // Flags to pass to proguard.
225 Proguard_flags []string
226
227 // Specifies the locations of files containing proguard flags.
228 Proguard_flags_files []string
229 }
230
Colin Cross1369cdb2017-09-29 17:58:17 -0700231 // When targeting 1.9, override the modules to use with --system
232 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700233}
234
Colin Cross46c9b8b2017-06-22 16:51:17 -0700235// Module contains the properties and members used by all java module types
236type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700237 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700238 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700239
Colin Cross89536d42017-07-07 14:35:50 -0700240 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700241 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700242 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700243
Nan Zhanged19fc32017-10-19 13:06:22 -0700244 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
245 headerJarFile android.Path
246
247 // full implementation jar file suitable for static dependency of another module compile
248 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700249
Colin Cross6ade34f2017-09-15 13:00:47 -0700250 // output file containing classes.dex
251 dexJarFile android.Path
252
Colin Crosscb933592017-11-22 13:49:43 -0800253 // output file containing uninstrumented classes that will be instrumented by jacoco
254 jacocoReportClassesFile android.Path
255
Colin Cross66dbc0b2017-12-28 12:23:20 -0800256 // output file containing mapping of obfuscated names
257 proguardDictionary android.Path
258
Colin Crossb7a63242015-04-16 14:09:14 -0700259 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700260 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700261
Colin Cross635c3b02016-05-18 15:37:25 -0700262 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700263
Colin Cross635c3b02016-05-18 15:37:25 -0700264 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700265
Colin Cross2fe66872015-03-30 17:20:39 -0700266 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700267 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800268
269 // list of .java files and srcjars that was passed to javac
270 compiledJavaSrcs android.Paths
271 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800272
273 // list of extra progurad flag files
274 extraProguardFlagFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700275}
276
Colin Cross54250902017-12-05 09:28:08 -0800277func (j *Module) Srcs() android.Paths {
278 return android.Paths{j.implementationJarFile}
279}
280
281var _ android.SourceFileProducer = (*Module)(nil)
282
Colin Crossf506d872017-07-19 15:53:04 -0700283type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 HeaderJars() android.Paths
285 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700286 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700287}
288
Nan Zhangb2b33de2018-02-23 11:18:47 -0800289type SrcDependency interface {
290 CompiledSrcs() android.Paths
291 CompiledSrcJars() android.Paths
292}
293
294func (j *Module) CompiledSrcs() android.Paths {
295 return j.compiledJavaSrcs
296}
297
298func (j *Module) CompiledSrcJars() android.Paths {
299 return j.compiledSrcJars
300}
301
302var _ SrcDependency = (*Module)(nil)
303
Colin Cross89536d42017-07-07 14:35:50 -0700304func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
305 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
306 android.InitDefaultableModule(module)
307}
308
Colin Crossbe1da472017-07-07 15:59:46 -0700309type dependencyTag struct {
310 blueprint.BaseDependencyTag
311 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700312}
313
Colin Crossbe1da472017-07-07 15:59:46 -0700314var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700315 staticLibTag = dependencyTag{name: "staticlib"}
316 libTag = dependencyTag{name: "javalib"}
317 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700318 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700319 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800320 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700321 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800322 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700323)
Colin Cross2fe66872015-03-30 17:20:39 -0700324
Colin Crossfc3674a2017-09-18 17:41:52 -0700325type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700326 useModule, useFiles, useDefaultLibs, invalidVersion bool
327
Colin Cross1369cdb2017-09-29 17:58:17 -0700328 module string
329 systemModules string
330
331 jar android.Path
332 aidl android.Path
333}
334
335func sdkStringToNumber(ctx android.BaseContext, v string) int {
336 switch v {
Jiyong Park750e5572018-01-31 00:20:13 +0900337 case "", "current", "system_current", "test_current", "core_current":
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900338 return android.FutureApiLevel
Colin Cross1369cdb2017-09-29 17:58:17 -0700339 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900340 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700341 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
342 return -1
343 } else {
344 return i
345 }
346 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700347}
348
Colin Cross3144dfc2018-01-03 15:06:47 -0800349func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
350 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
351}
352
353func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
354 return j.shouldInstrument(ctx) &&
355 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
356 ctx.Config().UnbundledBuild())
357}
358
Colin Crossfc3674a2017-09-18 17:41:52 -0700359func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700360 i := sdkStringToNumber(ctx, v)
361 if i == -1 {
362 // Invalid sdk version, error handled by sdkStringToNumber.
363 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700364 }
365
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900366 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
367 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
368 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
369 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
370 if ctx.DeviceSpecific() || ctx.SocSpecific() {
371 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
372 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
373 }
374 }
375 version := strings.TrimPrefix(v, "system_")
376 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
377 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
378 v, allowed_versions)
379 }
380 }
381
Colin Crossfc3674a2017-09-18 17:41:52 -0700382 toFile := func(v string) sdkDep {
Jiyong Park750e5572018-01-31 00:20:13 +0900383 isCore := strings.HasPrefix(v, "core_")
384 if isCore {
385 v = strings.TrimPrefix(v, "core_")
386 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700387 dir := filepath.Join("prebuilts/sdk", v)
388 jar := filepath.Join(dir, "android.jar")
Jiyong Park750e5572018-01-31 00:20:13 +0900389 if isCore {
390 jar = filepath.Join(dir, "core.jar")
391 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700392 aidl := filepath.Join(dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800393 jarPath := android.ExistentPathForSource(ctx, jar)
394 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700395
Colin Cross6510f912017-11-29 00:27:14 -0800396 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700397 return sdkDep{
398 invalidVersion: true,
399 module: "sdk_v" + v,
400 }
401 }
402
Colin Crossfc3674a2017-09-18 17:41:52 -0700403 if !jarPath.Valid() {
404 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
405 return sdkDep{}
406 }
Colin Cross47ff2522017-10-02 14:22:08 -0700407
Colin Crossfc3674a2017-09-18 17:41:52 -0700408 if !aidlPath.Valid() {
409 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
410 return sdkDep{}
411 }
Colin Cross47ff2522017-10-02 14:22:08 -0700412
Colin Crossfc3674a2017-09-18 17:41:52 -0700413 return sdkDep{
414 useFiles: true,
415 jar: jarPath.Path(),
416 aidl: aidlPath.Path(),
417 }
418 }
419
Colin Cross2ebc4762017-10-20 14:00:31 -0700420 //toModule := func(m string) sdkDep {
421 // return sdkDep{
422 // useModule: true,
423 // module: m,
424 // systemModules: m + "_system_modules",
425 // }
426 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700427
Colin Cross6510f912017-11-29 00:27:14 -0800428 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700429 return toFile(v)
430 }
431
432 switch v {
433 case "":
434 return sdkDep{
435 useDefaultLibs: true,
436 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700437 // TODO(ccross): re-enable these once we generate stubs, until then
438 // use the stubs in prebuilts/sdk/*current
439 //case "current":
440 // return toModule("android_stubs_current")
441 //case "system_current":
442 // return toModule("android_system_stubs_current")
443 //case "test_current":
444 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700445 default:
446 return toFile(v)
447 }
448}
449
Colin Crossbe1da472017-07-07 15:59:46 -0700450func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700451 if ctx.Device() {
452 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800453 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700454 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700455 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800456 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700457 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
458 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700459 if !proptools.Bool(j.properties.No_framework_libs) {
460 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
461 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700462 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800463 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700464 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
465 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700466 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800467 if Bool(j.deviceProperties.Optimize.Enabled) {
468 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultBootclasspathLibraries...)
469 ctx.AddDependency(ctx.Module(), proguardRaiseTag, config.DefaultLibraries...)
470 }
Colin Crossbe1da472017-07-07 15:59:46 -0700471 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700472 } else if j.deviceProperties.System_modules == nil {
473 ctx.PropertyErrorf("no_standard_libs",
474 "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 -0800475 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700476 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700477 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800478 if ctx.ModuleName() == "framework" {
479 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
480 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800481 if ctx.ModuleName() == "android_stubs_current" ||
482 ctx.ModuleName() == "android_system_stubs_current" ||
483 ctx.ModuleName() == "android_test_stubs_current" {
484 ctx.AddDependency(ctx.Module(), frameworkApkTag, "framework-res")
485 }
Colin Cross2fe66872015-03-30 17:20:39 -0700486 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700487
Colin Crossf506d872017-07-19 15:53:04 -0700488 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
489 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700490 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700491
492 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000493 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700494 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800495 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700496
497 if j.hasSrcExt(".proto") {
498 protoDeps(ctx, &j.protoProperties)
499 }
Colin Cross93e85952017-08-15 13:34:18 -0700500
501 if j.hasSrcExt(".kt") {
502 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
503 // Kotlin files
504 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
505 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800506
507 if j.shouldInstrumentStatic(ctx) {
508 ctx.AddDependency(ctx.Module(), staticLibTag, "jacocoagent")
509 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700510}
511
512func hasSrcExt(srcs []string, ext string) bool {
513 for _, src := range srcs {
514 if filepath.Ext(src) == ext {
515 return true
516 }
517 }
518
519 return false
520}
521
Nan Zhang61eaedb2017-11-02 13:28:15 -0700522func shardPaths(paths android.Paths, shardSize int) []android.Paths {
523 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
524 for len(paths) > shardSize {
525 ret = append(ret, paths[0:shardSize])
526 paths = paths[shardSize:]
527 }
528 if len(paths) > 0 {
529 ret = append(ret, paths)
530 }
531 return ret
532}
533
Colin Cross6af17aa2017-09-20 12:59:05 -0700534func (j *Module) hasSrcExt(ext string) bool {
535 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700536}
537
Colin Cross46c9b8b2017-06-22 16:51:17 -0700538func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700539 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700540
Colin Crossebe1a512017-11-14 13:12:14 -0800541 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
542 aidlIncludes = append(aidlIncludes,
543 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
544 aidlIncludes = append(aidlIncludes,
545 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700546
547 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700548 if aidlPreprocess.Valid() {
549 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700550 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700551 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700552 }
553
Colin Cross635c3b02016-05-18 15:37:25 -0700554 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800555 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700556 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800557 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700558 flags = append(flags, "-I"+src.String())
559 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700560
Colin Crossf03c82b2015-04-13 13:53:40 -0700561 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700562}
563
Colin Cross32f676a2017-09-06 13:41:06 -0700564type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800565 classpath classpath
566 bootClasspath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700567 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700568 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700569 staticJarResources android.Paths
570 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800571 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700572 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700573 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700574 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700575 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700576}
Colin Cross2fe66872015-03-30 17:20:39 -0700577
Colin Cross54250902017-12-05 09:28:08 -0800578func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
579 for _, f := range dep.Srcs() {
580 if f.Ext() != ".jar" {
581 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
582 ctx.OtherModuleName(dep.(blueprint.Module)))
583 }
584 }
585}
586
Jiyong Park750e5572018-01-31 00:20:13 +0900587func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
588 if strings.HasPrefix(String(from.deviceProperties.Sdk_version), "core_") {
589 if !strings.HasPrefix(String(to.deviceProperties.Sdk_version), "core_") {
590 ctx.ModuleErrorf("depends on other library %q using non-core Java APIs",
591 ctx.OtherModuleName(to))
592 }
593 }
594}
595
Colin Cross32f676a2017-09-06 13:41:06 -0700596func (j *Module) collectDeps(ctx android.ModuleContext) deps {
597 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700598
Colin Cross300f0382018-03-06 13:11:51 -0800599 if ctx.Device() {
600 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
601 if sdkDep.invalidVersion {
602 ctx.AddMissingDependencies([]string{sdkDep.module})
603 } else if sdkDep.useFiles {
604 // sdkDep.jar is actually equivalent to turbine header.jar.
605 deps.classpath = append(deps.classpath, sdkDep.jar)
606 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
607 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700608 }
609
Colin Crossd11fcda2017-10-23 17:59:01 -0700610 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700611 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700612 tag := ctx.OtherModuleDependencyTag(module)
613
Jiyong Park750e5572018-01-31 00:20:13 +0900614 if to, ok := module.(*Library); ok {
615 checkLinkType(ctx, j, to, tag.(dependencyTag))
616 }
Colin Cross54250902017-12-05 09:28:08 -0800617 switch dep := module.(type) {
618 case Dependency:
619 switch tag {
620 case bootClasspathTag:
621 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
622 case libTag:
623 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
624 case staticLibTag:
625 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
626 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
627 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
628 case frameworkResTag:
629 if ctx.ModuleName() == "framework" {
630 // framework.jar has a one-off dependency on the R.java and Manifest.java files
631 // generated by framework-res.apk
632 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
633 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800634 case frameworkApkTag:
635 if ctx.ModuleName() == "android_stubs_current" ||
636 ctx.ModuleName() == "android_system_stubs_current" ||
637 ctx.ModuleName() == "android_test_stubs_current" {
638 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
639 // resource files out of there for aapt.
640 //
641 // Normally the package rule runs aapt, which includes the resource,
642 // but we're not running that in our package rule so just copy in the
643 // resource files here.
644 deps.staticJarResources = append(deps.staticJarResources, dep.(*AndroidApp).exportPackage)
645 }
Colin Cross54250902017-12-05 09:28:08 -0800646 case kotlinStdlibTag:
647 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800648 }
649
650 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
651 case android.SourceFileProducer:
652 switch tag {
653 case libTag:
654 checkProducesJars(ctx, dep)
655 deps.classpath = append(deps.classpath, dep.Srcs()...)
656 case staticLibTag:
657 checkProducesJars(ctx, dep)
658 deps.classpath = append(deps.classpath, dep.Srcs()...)
659 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
660 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
661 case android.DefaultsDepTag, android.SourceDepTag:
662 // Nothing to do
663 default:
664 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
665 }
666 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700667 switch tag {
668 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700669 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700670 case systemModulesTag:
671 if deps.systemModules != nil {
672 panic("Found two system module dependencies")
673 }
674 sm := module.(*SystemModules)
675 if sm.outputFile == nil {
676 panic("Missing directory for system module dependency")
677 }
678 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700679 default:
680 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700681 }
Colin Crossec7a0422017-07-07 14:47:12 -0700682 }
Colin Cross2fe66872015-03-30 17:20:39 -0700683 })
684
Colin Cross32f676a2017-09-06 13:41:06 -0700685 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700686}
687
Nan Zhanged19fc32017-10-19 13:06:22 -0700688func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700689
Colin Crossf03c82b2015-04-13 13:53:40 -0700690 var flags javaBuilderFlags
691
Nan Zhanged19fc32017-10-19 13:06:22 -0700692 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700693 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800694 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700695 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700696 }
Colin Cross6510f912017-11-29 00:27:14 -0800697 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700698 // Override the -g flag passed globally to remove local variable debug info to reduce
699 // disk and memory usage.
700 javacFlags = append(javacFlags, "-g:source,lines")
701 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700702 if len(javacFlags) > 0 {
703 // optimization.
704 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
705 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700706 }
Colin Cross64162712017-08-08 13:17:59 -0700707
Andreas Gampef3e5b552018-01-22 21:27:21 -0800708 if len(j.properties.Errorprone.Javacflags) > 0 {
709 flags.errorProneExtraJavacFlags = strings.Join(j.properties.Errorprone.Javacflags, " ")
710 }
711
Nan Zhanged19fc32017-10-19 13:06:22 -0700712 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800713 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700714 if j.properties.Java_version != nil {
715 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700716 } else if ctx.Device() && sdk <= 23 {
717 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800718 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700719 flags.javaVersion = "1.8"
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900720 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == android.FutureApiLevel {
Colin Cross2ebc4762017-10-20 14:00:31 -0700721 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
722 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700723 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700724 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700725 }
726
Nan Zhanged19fc32017-10-19 13:06:22 -0700727 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800728 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
729 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800730
731 if len(flags.bootClasspath) == 0 && ctx.Host() && !ctx.Config().TargetOpenJDK9() &&
732 !Bool(j.properties.No_standard_libs) &&
733 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
734 // Give host-side tools a version of OpenJDK's standard libraries
735 // close to what they're targeting. As of Dec 2017, AOSP is only
736 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
737 //
738 // When building with OpenJDK 8, the following should have no
739 // effect since those jars would be available by default.
740 //
741 // When building with OpenJDK 9 but targeting a version < 1.8,
742 // putting them on the bootclasspath means that:
743 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
744 // b) references to existing APIs are not reinterpreted in an
745 // OpenJDK 9-specific way, eg. calls to subclasses of
746 // java.nio.Buffer as in http://b/70862583
747 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
748 flags.bootClasspath = append(flags.bootClasspath,
749 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
750 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800751 if Bool(j.properties.Use_tools_jar) {
752 flags.bootClasspath = append(flags.bootClasspath,
753 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
754 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800755 }
756
Nan Zhanged19fc32017-10-19 13:06:22 -0700757 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700758 if deps.systemModules != nil {
759 flags.systemModules = append(flags.systemModules, deps.systemModules)
760 }
761
Nan Zhanged19fc32017-10-19 13:06:22 -0700762 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700763 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700764 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700765 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700766 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
767 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700768 }
769
Nan Zhanged19fc32017-10-19 13:06:22 -0700770 return flags
771}
Colin Crossc0b06f12015-04-08 13:03:43 -0700772
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800773func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700774
Colin Crossebe1a512017-11-14 13:12:14 -0800775 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700776
777 deps := j.collectDeps(ctx)
778 flags := j.collectBuilderFlags(ctx, deps)
779
Colin Cross6510f912017-11-29 00:27:14 -0800780 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700781 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
782 }
783 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700784 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800785 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700786 }
787
Colin Crossaf050172017-11-15 23:01:59 -0800788 srcFiles = j.genSources(ctx, srcFiles, flags)
789
790 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700791 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800792 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700793
Colin Cross0a6e0072017-08-30 14:24:55 -0700794 var jars android.Paths
795
Colin Cross1ee23172017-10-18 14:44:18 -0700796 jarName := ctx.ModuleName() + ".jar"
797
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000798 javaSrcFiles := srcFiles.FilterByExt(".java")
799 var uniqueSrcFiles android.Paths
800 set := make(map[string]bool)
801 for _, v := range javaSrcFiles {
802 if _, found := set[v.String()]; !found {
803 set[v.String()] = true
804 uniqueSrcFiles = append(uniqueSrcFiles, v)
805 }
806 }
807
Colin Cross93e85952017-08-15 13:34:18 -0700808 if srcFiles.HasExt(".kt") {
809 // If there are kotlin files, compile them first but pass all the kotlin and java files
810 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
811 // won't emit any classes for them.
812
813 flags.kotlincFlags = "-no-stdlib"
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000814
Colin Cross93e85952017-08-15 13:34:18 -0700815 if ctx.Device() {
816 flags.kotlincFlags += " -no-jdk"
817 }
818
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000819 var kotlinSrcFiles android.Paths
820 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
821 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
822
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +0000823 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -0700824 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
825 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
826
Colin Cross1ee23172017-10-18 14:44:18 -0700827 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000828 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -0700829 if ctx.Failed() {
830 return
831 }
832
833 // Make javac rule depend on the kotlinc rule
834 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000835
Colin Cross93e85952017-08-15 13:34:18 -0700836 // Jar kotlin classes into the final jar after javac
837 jars = append(jars, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000838
839 // Don't add kotlin-stdlib if using (on-device) renamed stdlib
840 // (it's expected to be on device bootclasspath)
841 if !proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
842 jars = append(jars, deps.kotlinStdlib...)
843 }
Colin Cross93e85952017-08-15 13:34:18 -0700844 }
845
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800846 // Store the list of .java files that was passed to javac
847 j.compiledJavaSrcs = uniqueSrcFiles
848 j.compiledSrcJars = srcJars
849
Nan Zhang61eaedb2017-11-02 13:28:15 -0700850 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800851 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700852 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
853 enable_sharding = true
854 if len(j.properties.Annotation_processors) != 0 ||
855 len(j.properties.Annotation_processor_classes) != 0 {
856 ctx.PropertyErrorf("javac_shard_size",
857 "%q cannot be set when annotation processors are enabled.",
858 j.properties.Javac_shard_size)
859 }
860 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700861 // If sdk jar is java module, then directly return classesJar as header.jar
862 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
863 j.Name() != "android_test_stubs_current" {
864 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
865 if ctx.Failed() {
866 return
867 }
868 }
869 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700870 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700871 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800872 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700873 // If error-prone is enabled, add an additional rule to compile the java files into
874 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700875 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700876 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
877 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700878 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700879 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700880 extraJarDeps = append(extraJarDeps, errorprone)
881 }
882
Nan Zhang61eaedb2017-11-02 13:28:15 -0700883 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -0800884 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700885 shardSize := int(*(j.properties.Javac_shard_size))
886 var shardSrcs []android.Paths
887 if len(uniqueSrcFiles) > 0 {
888 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
889 for idx, shardSrc := range shardSrcs {
890 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
891 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
892 jars = append(jars, classes)
893 }
894 }
895 if len(srcJars) > 0 {
896 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
897 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
898 jars = append(jars, classes)
899 }
900 } else {
901 classes := android.PathForModuleOut(ctx, "javac", jarName)
902 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
903 jars = append(jars, classes)
904 }
Colin Crossd6891432017-09-27 17:39:56 -0700905 if ctx.Failed() {
906 return
907 }
Colin Cross2fe66872015-03-30 17:20:39 -0700908 }
909
Colin Cross0f37af02017-09-27 17:42:05 -0700910 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
911 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
912
913 var resArgs []string
914 var resDeps android.Paths
915
916 resArgs = append(resArgs, dirArgs...)
917 resDeps = append(resDeps, dirDeps...)
918
919 resArgs = append(resArgs, fileArgs...)
920 resDeps = append(resDeps, fileDeps...)
921
922 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700923 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700924 resArgs = append(resArgs, srcArgs...)
925 resDeps = append(resDeps, srcDeps...)
926 }
Colin Cross40a36712017-09-27 17:41:35 -0700927
928 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700929 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700930 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700931 if ctx.Failed() {
932 return
933 }
Colin Cross20978302015-04-10 17:05:07 -0700934
Colin Cross0a6e0072017-08-30 14:24:55 -0700935 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700936 }
937
Colin Cross6ade34f2017-09-15 13:00:47 -0700938 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700939 jars = append(jars, deps.staticJars...)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800940 jars = append(jars, deps.staticJarResources...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700941
Colin Cross366938f2017-12-11 16:29:02 -0800942 var manifest android.OptionalPath
943 if j.properties.Manifest != nil {
944 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
945 }
Colin Cross635acc92017-09-12 22:50:46 -0700946
Colin Cross0a6e0072017-08-30 14:24:55 -0700947 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700948 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700949 var outputFile android.Path
950
951 if len(jars) == 1 && !manifest.Valid() {
952 // Optimization: skip the combine step if there is nothing to do
Colin Cross7b60cdd2017-12-21 13:52:58 -0800953 // TODO(ccross): this leaves any module-info.class files, but those should only come from
954 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
955 // any if len(jars) == 1.
Colin Crosse9a275b2017-10-16 17:09:48 -0700956 outputFile = jars[0]
957 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700958 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700959 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700960 outputFile = combinedJar
961 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700962
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000963 // Use renamed kotlin standard library?
964 if srcFiles.HasExt(".kt") && proptools.Bool(j.properties.Renamed_kotlin_stdlib) {
965 jarjarFile := android.PathForModuleOut(ctx, "kotlin-renamed", jarName)
966 TransformJarJar(ctx, jarjarFile, outputFile,
967 android.PathForSource(ctx, "external/kotlinc/jarjar-rules.txt"))
968 outputFile = jarjarFile
969 if ctx.Failed() {
970 return
971 }
972 }
973
Colin Cross0a6e0072017-08-30 14:24:55 -0700974 if j.properties.Jarjar_rules != nil {
975 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700976 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700977 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700978 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
979 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700980 if ctx.Failed() {
981 return
982 }
983 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700984 j.implementationJarFile = outputFile
985 if j.headerJarFile == nil {
986 j.headerJarFile = j.implementationJarFile
987 }
Colin Cross2fe66872015-03-30 17:20:39 -0700988
Colin Cross6510f912017-11-29 00:27:14 -0800989 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -0800990 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
991 j.properties.Instrument = true
992 }
993 }
994
Colin Cross3144dfc2018-01-03 15:06:47 -0800995 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -0800996 outputFile = j.instrument(ctx, flags, outputFile, jarName)
997 }
998
999 if ctx.Device() && j.installable() {
Colin Crossf0056cb2017-12-22 15:56:08 -08001000 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001001 if ctx.Failed() {
1002 return
1003 }
Colin Cross2fe66872015-03-30 17:20:39 -07001004 }
Colin Crossb7a63242015-04-16 14:09:14 -07001005 ctx.CheckbuildFile(outputFile)
1006 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001007}
1008
Colin Cross8eadbf02017-10-24 17:46:00 -07001009func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -07001010 deps deps, flags javaBuilderFlags, jarName string) android.Path {
1011
1012 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001013 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001014 // Compile java sources into turbine.jar.
1015 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1016 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1017 if ctx.Failed() {
1018 return nil
1019 }
1020 jars = append(jars, turbineJar)
1021 }
1022
1023 // Combine any static header libraries into classes-header.jar. If there is only
1024 // one input jar this step will be skipped.
1025 var headerJar android.Path
1026 jars = append(jars, deps.staticHeaderJars...)
1027
Colin Cross5c6ecc12017-10-23 18:12:27 -07001028 // we cannot skip the combine step for now if there is only one jar
1029 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1030 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
1031 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
1032 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001033
1034 if j.properties.Jarjar_rules != nil {
1035 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1036 // Transform classes.jar into classes-jarjar.jar
1037 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1038 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1039 headerJar = jarjarFile
1040 if ctx.Failed() {
1041 return nil
1042 }
1043 }
1044
1045 return headerJar
1046}
1047
Colin Crosscb933592017-11-22 13:49:43 -08001048func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
1049 classesJar android.Path, jarName string) android.Path {
1050
Colin Cross7a3139e2017-12-19 13:57:50 -08001051 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001052
Colin Cross84c38822018-01-03 15:59:46 -08001053 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001054 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1055
1056 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1057
1058 j.jacocoReportClassesFile = jacocoReportClassesFile
1059
1060 return instrumentedJar
1061}
1062
Colin Crosscb933592017-11-22 13:49:43 -08001063// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
1064// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
1065func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
1066 switch String(j.deviceProperties.Sdk_version) {
Jiyong Park750e5572018-01-31 00:20:13 +09001067 case "", "current", "test_current", "system_current", "core_current":
Colin Cross6510f912017-11-29 00:27:14 -08001068 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -08001069 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +09001070 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -08001071 }
1072}
1073
Colin Cross59f1bb62017-09-27 17:59:10 -07001074func (j *Module) installable() bool {
1075 return j.properties.Installable == nil || *j.properties.Installable
1076}
1077
Colin Crossf506d872017-07-19 15:53:04 -07001078var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001079
Nan Zhanged19fc32017-10-19 13:06:22 -07001080func (j *Module) HeaderJars() android.Paths {
1081 return android.Paths{j.headerJarFile}
1082}
1083
1084func (j *Module) ImplementationJars() android.Paths {
1085 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001086}
1087
Colin Cross46c9b8b2017-06-22 16:51:17 -07001088func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001089 return j.exportAidlIncludeDirs
1090}
1091
Colin Cross46c9b8b2017-06-22 16:51:17 -07001092var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001093
Colin Cross46c9b8b2017-06-22 16:51:17 -07001094func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001095 return j.logtagsSrcs
1096}
1097
Colin Cross2fe66872015-03-30 17:20:39 -07001098//
1099// Java libraries (.jar file)
1100//
1101
Colin Crossf506d872017-07-19 15:53:04 -07001102type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001103 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001104}
1105
Colin Crossf506d872017-07-19 15:53:04 -07001106func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001107 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001108
Colin Cross59f1bb62017-09-27 17:59:10 -07001109 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001110 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1111 ctx.ModuleName()+".jar", j.outputFile)
1112 }
Colin Crossb7a63242015-04-16 14:09:14 -07001113}
1114
Colin Crossf506d872017-07-19 15:53:04 -07001115func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001116 j.deps(ctx)
1117}
1118
Colin Crossa60ead82017-10-02 18:10:21 -07001119func LibraryFactory(installable bool) func() android.Module {
1120 return func() android.Module {
1121 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001122
Colin Crossa60ead82017-10-02 18:10:21 -07001123 if !installable {
1124 module.properties.Installable = proptools.BoolPtr(false)
1125 }
Colin Cross2fe66872015-03-30 17:20:39 -07001126
Colin Crossa60ead82017-10-02 18:10:21 -07001127 module.AddProperties(
1128 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001129 &module.Module.deviceProperties,
1130 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001131
Colin Crossa60ead82017-10-02 18:10:21 -07001132 InitJavaModule(module, android.HostAndDeviceSupported)
1133 return module
1134 }
Colin Cross2fe66872015-03-30 17:20:39 -07001135}
1136
Colin Crossf506d872017-07-19 15:53:04 -07001137func LibraryHostFactory() android.Module {
1138 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001139
Colin Cross6af17aa2017-09-20 12:59:05 -07001140 module.AddProperties(
1141 &module.Module.properties,
1142 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001143
Colin Cross89536d42017-07-07 14:35:50 -07001144 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001145 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001146}
1147
1148//
1149// Java Binaries (.jar file plus wrapper script)
1150//
1151
Colin Crossf506d872017-07-19 15:53:04 -07001152type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001153 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001154 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001155}
1156
Colin Crossf506d872017-07-19 15:53:04 -07001157type Binary struct {
1158 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001159
Colin Crossf506d872017-07-19 15:53:04 -07001160 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001161
Colin Cross6b4a32d2017-12-05 13:42:45 -08001162 isWrapperVariant bool
1163
Colin Crossc3315992017-12-08 19:12:36 -08001164 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001165 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001166}
1167
Alex Light24237172017-10-26 09:46:21 -07001168func (j *Binary) HostToolPath() android.OptionalPath {
1169 return android.OptionalPathForPath(j.binaryFile)
1170}
1171
Colin Crossf506d872017-07-19 15:53:04 -07001172func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001173 if ctx.Arch().ArchType == android.Common {
1174 // Compile the jar
1175 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001176 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001177 // Handle the binary wrapper
1178 j.isWrapperVariant = true
1179
Colin Cross366938f2017-12-11 16:29:02 -08001180 if j.binaryProperties.Wrapper != nil {
1181 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001182 } else {
1183 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1184 }
1185
1186 // Depend on the installed jar so that the wrapper doesn't get executed by
1187 // another build rule before the jar has been installed.
1188 jarFile := ctx.PrimaryModule().(*Binary).installFile
1189
1190 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1191 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001192 }
Colin Cross2fe66872015-03-30 17:20:39 -07001193}
1194
Colin Crossf506d872017-07-19 15:53:04 -07001195func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001196 if ctx.Arch().ArchType == android.Common {
1197 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001198 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001199 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001200 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001201}
1202
Colin Crossf506d872017-07-19 15:53:04 -07001203func BinaryFactory() android.Module {
1204 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001205
Colin Cross36242852017-06-23 15:06:31 -07001206 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001207 &module.Module.properties,
1208 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001209 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001210 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001211
Colin Cross6b4a32d2017-12-05 13:42:45 -08001212 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1213 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001214 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001215}
1216
Colin Crossf506d872017-07-19 15:53:04 -07001217func BinaryHostFactory() android.Module {
1218 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001219
Colin Cross36242852017-06-23 15:06:31 -07001220 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001221 &module.Module.properties,
1222 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001223 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001224 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001225
Colin Cross6b4a32d2017-12-05 13:42:45 -08001226 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1227 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001228 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001229}
1230
1231//
1232// Java prebuilts
1233//
1234
Colin Cross74d73e22017-08-02 11:05:49 -07001235type ImportProperties struct {
1236 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001237
Nan Zhangea568a42017-11-08 21:20:04 -08001238 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001239
1240 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001241}
1242
1243type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001244 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001245 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001246
Colin Cross74d73e22017-08-02 11:05:49 -07001247 properties ImportProperties
1248
Colin Cross0a6e0072017-08-30 14:24:55 -07001249 classpathFiles android.Paths
1250 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001251}
1252
Colin Cross74d73e22017-08-02 11:05:49 -07001253func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001254 return &j.prebuilt
1255}
1256
Colin Cross74d73e22017-08-02 11:05:49 -07001257func (j *Import) PrebuiltSrcs() []string {
1258 return j.properties.Jars
1259}
1260
1261func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001262 return j.prebuilt.Name(j.ModuleBase.Name())
1263}
1264
Colin Cross74d73e22017-08-02 11:05:49 -07001265func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001266}
1267
Colin Cross74d73e22017-08-02 11:05:49 -07001268func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1269 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001270
Colin Crosse9a275b2017-10-16 17:09:48 -07001271 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001272 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001273 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001274}
1275
Colin Cross74d73e22017-08-02 11:05:49 -07001276var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001277
Nan Zhanged19fc32017-10-19 13:06:22 -07001278func (j *Import) HeaderJars() android.Paths {
1279 return j.classpathFiles
1280}
1281
1282func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001283 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001284}
1285
Colin Cross74d73e22017-08-02 11:05:49 -07001286func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001287 return nil
1288}
1289
Colin Cross74d73e22017-08-02 11:05:49 -07001290var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001291
Colin Cross74d73e22017-08-02 11:05:49 -07001292func ImportFactory() android.Module {
1293 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001294
Colin Cross74d73e22017-08-02 11:05:49 -07001295 module.AddProperties(&module.properties)
1296
1297 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001298 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1299 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001300}
1301
Colin Cross74d73e22017-08-02 11:05:49 -07001302func ImportFactoryHost() android.Module {
1303 module := &Import{}
1304
1305 module.AddProperties(&module.properties)
1306
1307 android.InitPrebuiltModule(module, &module.properties.Jars)
1308 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1309 return module
1310}
1311
Colin Cross89536d42017-07-07 14:35:50 -07001312//
1313// Defaults
1314//
1315type Defaults struct {
1316 android.ModuleBase
1317 android.DefaultsModuleBase
1318}
1319
1320func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1321}
1322
1323func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1324}
1325
1326func defaultsFactory() android.Module {
1327 return DefaultsFactory()
1328}
1329
1330func DefaultsFactory(props ...interface{}) android.Module {
1331 module := &Defaults{}
1332
1333 module.AddProperties(props...)
1334 module.AddProperties(
1335 &CompilerProperties{},
1336 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001337 &android.ProtoProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001338 )
1339
1340 android.InitDefaultsModule(module)
1341
1342 return module
1343}
Nan Zhangea568a42017-11-08 21:20:04 -08001344
1345var Bool = proptools.Bool
1346var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001347var inList = android.InList