blob: 0e54e3c6120c60f934abc6fa65fbd499997d5cab [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
Colin Cross7d5136f2015-05-11 13:39:40 -070086 // list of module-specific flags that will be used for javac compiles
87 Javacflags []string `android:"arch_variant"`
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070090 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070091
92 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070096 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070097
Colin Cross540eff82017-06-22 17:01:52 -070098 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -070099 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700100
101 // If not blank, set the java version passed to javac as -source and -target
102 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700103
104 // If set to false, don't allow this module to be installed. Defaults to true.
105 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700106
Colin Cross0f37af02017-09-27 17:42:05 -0700107 // If set to true, include sources used to compile the module in to the final jar
108 Include_srcs *bool
109
Colin Cross32f676a2017-09-06 13:41:06 -0700110 // List of modules to use as annotation processors
111 Annotation_processors []string
112
113 // List of classes to pass to javac to use as annotation processors
114 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700115
Nan Zhang61eaedb2017-11-02 13:28:15 -0700116 // The number of Java source entries each Javac instance can process
117 Javac_shard_size *int64
118
Colin Cross1369cdb2017-09-29 17:58:17 -0700119 Openjdk9 struct {
120 // List of source files that should only be used when passing -source 1.9
121 Srcs []string
122
123 // List of javac flags that should only be used when passing -source 1.9
124 Javacflags []string
125 }
Colin Crosscb933592017-11-22 13:49:43 -0800126
127 Jacoco struct {
128 // List of classes to include for instrumentation with jacoco to collect coverage
129 // information at runtime when building with coverage enabled. If unset defaults to all
130 // classes.
131 // Supports '*' as the last character of an entry in the list as a wildcard match.
132 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
133 // it matches classes in the package that have the class name as a prefix.
134 Include_filter []string
135
136 // List of classes to exclude from instrumentation with jacoco to collect coverage
137 // information at runtime when building with coverage enabled. Overrides classes selected
138 // by the include_filter property.
139 // Supports '*' as the last character of an entry in the list as a wildcard match.
140 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
141 // it matches classes in the package that have the class name as a prefix.
142 Exclude_filter []string
143 }
144
145 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700146}
147
Colin Cross89536d42017-07-07 14:35:50 -0700148type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700149 // list of module-specific flags that will be used for dex compiles
150 Dxflags []string `android:"arch_variant"`
151
Colin Cross7d5136f2015-05-11 13:39:40 -0700152 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800153 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700154
Colin Crossebe1a512017-11-14 13:12:14 -0800155 Aidl struct {
156 // Top level directories to pass to aidl tool
157 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700158
Colin Crossebe1a512017-11-14 13:12:14 -0800159 // Directories rooted at the Android.bp file to pass to aidl tool
160 Local_include_dirs []string
161
162 // directories that should be added as include directories for any aidl sources of modules
163 // that depend on this module, as well as to aidl for this module.
164 Export_include_dirs []string
165 }
Colin Cross92430102017-10-09 14:59:32 -0700166
167 // If true, export a copy of the module as a -hostdex module for host testing.
168 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700169
Colin Crossa22116e2017-10-19 14:18:58 -0700170 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
171 // true.
172 Dex_preopt *bool
173
Colin Cross1369cdb2017-09-29 17:58:17 -0700174 // When targeting 1.9, override the modules to use with --system
175 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700176}
177
Colin Cross46c9b8b2017-06-22 16:51:17 -0700178// Module contains the properties and members used by all java module types
179type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700180 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700181 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700182
Colin Cross89536d42017-07-07 14:35:50 -0700183 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700184 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700185 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700186
Nan Zhanged19fc32017-10-19 13:06:22 -0700187 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
188 headerJarFile android.Path
189
190 // full implementation jar file suitable for static dependency of another module compile
191 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700192
Colin Cross6ade34f2017-09-15 13:00:47 -0700193 // output file containing classes.dex
194 dexJarFile android.Path
195
Colin Crosscb933592017-11-22 13:49:43 -0800196 // output file containing uninstrumented classes that will be instrumented by jacoco
197 jacocoReportClassesFile android.Path
198
Colin Crossb7a63242015-04-16 14:09:14 -0700199 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700200 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700201
Colin Cross635c3b02016-05-18 15:37:25 -0700202 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700203
Colin Cross635c3b02016-05-18 15:37:25 -0700204 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700205
Colin Cross2fe66872015-03-30 17:20:39 -0700206 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700207 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800208
209 // list of .java files and srcjars that was passed to javac
210 compiledJavaSrcs android.Paths
211 compiledSrcJars android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700212}
213
Colin Cross54250902017-12-05 09:28:08 -0800214func (j *Module) Srcs() android.Paths {
215 return android.Paths{j.implementationJarFile}
216}
217
218var _ android.SourceFileProducer = (*Module)(nil)
219
Colin Crossf506d872017-07-19 15:53:04 -0700220type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700221 HeaderJars() android.Paths
222 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700223 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700224}
225
Colin Cross89536d42017-07-07 14:35:50 -0700226func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
227 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
228 android.InitDefaultableModule(module)
229}
230
Colin Crossbe1da472017-07-07 15:59:46 -0700231type dependencyTag struct {
232 blueprint.BaseDependencyTag
233 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700234}
235
Colin Crossbe1da472017-07-07 15:59:46 -0700236var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700237 staticLibTag = dependencyTag{name: "staticlib"}
238 libTag = dependencyTag{name: "javalib"}
239 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700240 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700241 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross93e85952017-08-15 13:34:18 -0700242 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700243)
Colin Cross2fe66872015-03-30 17:20:39 -0700244
Colin Crossfc3674a2017-09-18 17:41:52 -0700245type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700246 useModule, useFiles, useDefaultLibs, invalidVersion bool
247
Colin Cross1369cdb2017-09-29 17:58:17 -0700248 module string
249 systemModules string
250
251 jar android.Path
252 aidl android.Path
253}
254
255func sdkStringToNumber(ctx android.BaseContext, v string) int {
256 switch v {
257 case "", "current", "system_current", "test_current":
258 return 10000
259 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900260 if i, err := strconv.Atoi(android.GetNumericSdkVersion(v)); err != nil {
Colin Cross1369cdb2017-09-29 17:58:17 -0700261 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
262 return -1
263 } else {
264 return i
265 }
266 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700267}
268
269func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700270 i := sdkStringToNumber(ctx, v)
271 if i == -1 {
272 // Invalid sdk version, error handled by sdkStringToNumber.
273 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700274 }
275
276 toFile := func(v string) sdkDep {
277 dir := filepath.Join("prebuilts/sdk", v)
278 jar := filepath.Join(dir, "android.jar")
279 aidl := filepath.Join(dir, "framework.aidl")
280 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
281 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700282
Colin Cross6510f912017-11-29 00:27:14 -0800283 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Sundong Ahn0926fae2017-10-17 16:34:51 +0900284 if strings.Contains(v, "system_") {
285 return sdkDep{
286 invalidVersion: true,
287 module: "vsdk_v" + strings.Replace(v, "system_", "", 1),
288 }
289 }
Colin Cross47ff2522017-10-02 14:22:08 -0700290 return sdkDep{
291 invalidVersion: true,
292 module: "sdk_v" + v,
293 }
294 }
295
Colin Crossfc3674a2017-09-18 17:41:52 -0700296 if !jarPath.Valid() {
297 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
298 return sdkDep{}
299 }
Colin Cross47ff2522017-10-02 14:22:08 -0700300
Colin Crossfc3674a2017-09-18 17:41:52 -0700301 if !aidlPath.Valid() {
302 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
303 return sdkDep{}
304 }
Colin Cross47ff2522017-10-02 14:22:08 -0700305
Colin Crossfc3674a2017-09-18 17:41:52 -0700306 return sdkDep{
307 useFiles: true,
308 jar: jarPath.Path(),
309 aidl: aidlPath.Path(),
310 }
311 }
312
Colin Cross2ebc4762017-10-20 14:00:31 -0700313 //toModule := func(m string) sdkDep {
314 // return sdkDep{
315 // useModule: true,
316 // module: m,
317 // systemModules: m + "_system_modules",
318 // }
319 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700320
Colin Cross6510f912017-11-29 00:27:14 -0800321 if ctx.Config().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700322 return toFile(v)
323 }
324
325 switch v {
326 case "":
327 return sdkDep{
328 useDefaultLibs: true,
329 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700330 // TODO(ccross): re-enable these once we generate stubs, until then
331 // use the stubs in prebuilts/sdk/*current
332 //case "current":
333 // return toModule("android_stubs_current")
334 //case "system_current":
335 // return toModule("android_system_stubs_current")
336 //case "test_current":
337 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700338 default:
339 return toFile(v)
340 }
341}
342
Colin Crossbe1da472017-07-07 15:59:46 -0700343func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700344 if ctx.Device() {
345 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800346 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700347 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700348 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross6510f912017-11-29 00:27:14 -0800349 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700350 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
351 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700352 if !proptools.Bool(j.properties.No_framework_libs) {
353 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
354 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700355 } else if sdkDep.useModule {
Colin Cross6510f912017-11-29 00:27:14 -0800356 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700357 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
358 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700359 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700360 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700361 } else if j.deviceProperties.System_modules == nil {
362 ctx.PropertyErrorf("no_standard_libs",
363 "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 -0800364 } else if *j.deviceProperties.System_modules != "none" && ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700365 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700366 }
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800367 if ctx.ModuleName() == "framework" {
368 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
369 }
Colin Cross2fe66872015-03-30 17:20:39 -0700370 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700371
Colin Crossf506d872017-07-19 15:53:04 -0700372 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
373 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700374 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700375
376 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700377 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross6af17aa2017-09-20 12:59:05 -0700378
379 if j.hasSrcExt(".proto") {
380 protoDeps(ctx, &j.protoProperties)
381 }
Colin Cross93e85952017-08-15 13:34:18 -0700382
383 if j.hasSrcExt(".kt") {
384 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
385 // Kotlin files
386 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
387 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700388}
389
390func hasSrcExt(srcs []string, ext string) bool {
391 for _, src := range srcs {
392 if filepath.Ext(src) == ext {
393 return true
394 }
395 }
396
397 return false
398}
399
Nan Zhang61eaedb2017-11-02 13:28:15 -0700400func shardPaths(paths android.Paths, shardSize int) []android.Paths {
401 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
402 for len(paths) > shardSize {
403 ret = append(ret, paths[0:shardSize])
404 paths = paths[shardSize:]
405 }
406 if len(paths) > 0 {
407 ret = append(ret, paths)
408 }
409 return ret
410}
411
Colin Cross6af17aa2017-09-20 12:59:05 -0700412func (j *Module) hasSrcExt(ext string) bool {
413 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700414}
415
Colin Cross46c9b8b2017-06-22 16:51:17 -0700416func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700417 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700418
Colin Crossebe1a512017-11-14 13:12:14 -0800419 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
420 aidlIncludes = append(aidlIncludes,
421 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
422 aidlIncludes = append(aidlIncludes,
423 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700424
425 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700426 if aidlPreprocess.Valid() {
427 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700428 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700429 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700430 }
431
Colin Cross635c3b02016-05-18 15:37:25 -0700432 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800433 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700434 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crosse243c232017-10-21 15:16:37 -0700435 if src := android.ExistentPathForSource(ctx, "", ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700436 flags = append(flags, "-I"+src.String())
437 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700438
Colin Crossf03c82b2015-04-13 13:53:40 -0700439 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700440}
441
Colin Cross32f676a2017-09-06 13:41:06 -0700442type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700443 classpath android.Paths
444 bootClasspath android.Paths
445 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700446 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700447 staticJarResources android.Paths
448 aidlIncludeDirs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700449 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700450 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700451 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700452 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700453}
Colin Cross2fe66872015-03-30 17:20:39 -0700454
Colin Cross54250902017-12-05 09:28:08 -0800455func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
456 for _, f := range dep.Srcs() {
457 if f.Ext() != ".jar" {
458 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
459 ctx.OtherModuleName(dep.(blueprint.Module)))
460 }
461 }
462}
463
Colin Cross32f676a2017-09-06 13:41:06 -0700464func (j *Module) collectDeps(ctx android.ModuleContext) deps {
465 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700466
Nan Zhangea568a42017-11-08 21:20:04 -0800467 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross47ff2522017-10-02 14:22:08 -0700468 if sdkDep.invalidVersion {
469 ctx.AddMissingDependencies([]string{sdkDep.module})
470 } else if sdkDep.useFiles {
Nan Zhanged19fc32017-10-19 13:06:22 -0700471 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Crossfc3674a2017-09-18 17:41:52 -0700472 deps.classpath = append(deps.classpath, sdkDep.jar)
473 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
474 }
475
Colin Crossd11fcda2017-10-23 17:59:01 -0700476 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700477 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700478 tag := ctx.OtherModuleDependencyTag(module)
479
Colin Cross54250902017-12-05 09:28:08 -0800480 switch dep := module.(type) {
481 case Dependency:
482 switch tag {
483 case bootClasspathTag:
484 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
485 case libTag:
486 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
487 case staticLibTag:
488 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
489 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
490 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
491 case frameworkResTag:
492 if ctx.ModuleName() == "framework" {
493 // framework.jar has a one-off dependency on the R.java and Manifest.java files
494 // generated by framework-res.apk
495 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
496 }
497 case kotlinStdlibTag:
498 deps.kotlinStdlib = dep.HeaderJars()
499 default:
500 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
501 }
502
503 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
504 case android.SourceFileProducer:
505 switch tag {
506 case libTag:
507 checkProducesJars(ctx, dep)
508 deps.classpath = append(deps.classpath, dep.Srcs()...)
509 case staticLibTag:
510 checkProducesJars(ctx, dep)
511 deps.classpath = append(deps.classpath, dep.Srcs()...)
512 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
513 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
514 case android.DefaultsDepTag, android.SourceDepTag:
515 // Nothing to do
516 default:
517 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
518 }
519 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700520 switch tag {
521 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700522 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700523 case systemModulesTag:
524 if deps.systemModules != nil {
525 panic("Found two system module dependencies")
526 }
527 sm := module.(*SystemModules)
528 if sm.outputFile == nil {
529 panic("Missing directory for system module dependency")
530 }
531 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700532 default:
533 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700534 }
Colin Crossec7a0422017-07-07 14:47:12 -0700535 }
Colin Cross2fe66872015-03-30 17:20:39 -0700536 })
537
Colin Cross32f676a2017-09-06 13:41:06 -0700538 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700539}
540
Nan Zhanged19fc32017-10-19 13:06:22 -0700541func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700542
Colin Crossf03c82b2015-04-13 13:53:40 -0700543 var flags javaBuilderFlags
544
Nan Zhanged19fc32017-10-19 13:06:22 -0700545 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700546 javacFlags := j.properties.Javacflags
Colin Cross6510f912017-11-29 00:27:14 -0800547 if ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700548 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700549 }
Colin Cross6510f912017-11-29 00:27:14 -0800550 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700551 // Override the -g flag passed globally to remove local variable debug info to reduce
552 // disk and memory usage.
553 javacFlags = append(javacFlags, "-g:source,lines")
554 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700555 if len(javacFlags) > 0 {
556 // optimization.
557 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
558 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700559 }
Colin Cross64162712017-08-08 13:17:59 -0700560
Nan Zhanged19fc32017-10-19 13:06:22 -0700561 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800562 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700563 if j.properties.Java_version != nil {
564 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700565 } else if ctx.Device() && sdk <= 23 {
566 flags.javaVersion = "1.7"
Colin Cross6510f912017-11-29 00:27:14 -0800567 } else if ctx.Device() && sdk <= 26 || !ctx.Config().TargetOpenJDK9() {
Colin Cross1369cdb2017-09-29 17:58:17 -0700568 flags.javaVersion = "1.8"
Nan Zhangea568a42017-11-08 21:20:04 -0800569 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == 10000 {
Colin Cross2ebc4762017-10-20 14:00:31 -0700570 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
571 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700572 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700573 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700574 }
575
Nan Zhanged19fc32017-10-19 13:06:22 -0700576 // classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700577 flags.bootClasspath.AddPaths(deps.bootClasspath)
578 flags.classpath.AddPaths(deps.classpath)
Nan Zhanged19fc32017-10-19 13:06:22 -0700579 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700580 if deps.systemModules != nil {
581 flags.systemModules = append(flags.systemModules, deps.systemModules)
582 }
583
Nan Zhanged19fc32017-10-19 13:06:22 -0700584 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700585 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700586 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700587 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700588 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
589 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700590 }
591
Nan Zhanged19fc32017-10-19 13:06:22 -0700592 return flags
593}
Colin Crossc0b06f12015-04-08 13:03:43 -0700594
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800595func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700596
Colin Crossebe1a512017-11-14 13:12:14 -0800597 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700598
599 deps := j.collectDeps(ctx)
600 flags := j.collectBuilderFlags(ctx, deps)
601
Colin Cross6510f912017-11-29 00:27:14 -0800602 if ctx.Config().TargetOpenJDK9() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700603 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
604 }
605 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700606 if hasSrcExt(srcFiles.Strings(), ".proto") {
607 flags = protoFlags(ctx, &j.protoProperties, flags)
608 }
609
Colin Crossaf050172017-11-15 23:01:59 -0800610 srcFiles = j.genSources(ctx, srcFiles, flags)
611
612 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700613 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800614 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700615
Colin Cross0a6e0072017-08-30 14:24:55 -0700616 var jars android.Paths
617
Colin Cross1ee23172017-10-18 14:44:18 -0700618 jarName := ctx.ModuleName() + ".jar"
619
Colin Cross93e85952017-08-15 13:34:18 -0700620 if srcFiles.HasExt(".kt") {
621 // If there are kotlin files, compile them first but pass all the kotlin and java files
622 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
623 // won't emit any classes for them.
624
625 flags.kotlincFlags = "-no-stdlib"
626 if ctx.Device() {
627 flags.kotlincFlags += " -no-jdk"
628 }
629
630 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
631 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
632
Colin Cross1ee23172017-10-18 14:44:18 -0700633 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross93e85952017-08-15 13:34:18 -0700634 TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags)
635 if ctx.Failed() {
636 return
637 }
638
639 // Make javac rule depend on the kotlinc rule
640 flags.classpath = append(flags.classpath, kotlinJar)
641 // Jar kotlin classes into the final jar after javac
642 jars = append(jars, kotlinJar)
643 jars = append(jars, deps.kotlinStdlib...)
644 }
645
Nan Zhanged19fc32017-10-19 13:06:22 -0700646 javaSrcFiles := srcFiles.FilterByExt(".java")
647 var uniqueSrcFiles android.Paths
648 set := make(map[string]bool)
649 for _, v := range javaSrcFiles {
650 if _, found := set[v.String()]; !found {
651 set[v.String()] = true
652 uniqueSrcFiles = append(uniqueSrcFiles, v)
653 }
654 }
655
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800656 // Store the list of .java files that was passed to javac
657 j.compiledJavaSrcs = uniqueSrcFiles
658 j.compiledSrcJars = srcJars
659
Nan Zhang61eaedb2017-11-02 13:28:15 -0700660 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -0800661 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700662 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
663 enable_sharding = true
664 if len(j.properties.Annotation_processors) != 0 ||
665 len(j.properties.Annotation_processor_classes) != 0 {
666 ctx.PropertyErrorf("javac_shard_size",
667 "%q cannot be set when annotation processors are enabled.",
668 j.properties.Javac_shard_size)
669 }
670 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700671 // If sdk jar is java module, then directly return classesJar as header.jar
672 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
673 j.Name() != "android_test_stubs_current" {
674 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
675 if ctx.Failed() {
676 return
677 }
678 }
679 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700680 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700681 var extraJarDeps android.Paths
Colin Cross6510f912017-11-29 00:27:14 -0800682 if ctx.Config().IsEnvTrue("RUN_ERROR_PRONE") {
Colin Crossc6bbef32017-08-14 14:16:06 -0700683 // If error-prone is enabled, add an additional rule to compile the java files into
684 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700685 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700686 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
687 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700688 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700689 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700690 extraJarDeps = append(extraJarDeps, errorprone)
691 }
692
Nan Zhang61eaedb2017-11-02 13:28:15 -0700693 if enable_sharding {
694 flags.classpath.AddPaths([]android.Path{j.headerJarFile})
695 shardSize := int(*(j.properties.Javac_shard_size))
696 var shardSrcs []android.Paths
697 if len(uniqueSrcFiles) > 0 {
698 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
699 for idx, shardSrc := range shardSrcs {
700 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
701 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
702 jars = append(jars, classes)
703 }
704 }
705 if len(srcJars) > 0 {
706 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
707 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
708 jars = append(jars, classes)
709 }
710 } else {
711 classes := android.PathForModuleOut(ctx, "javac", jarName)
712 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
713 jars = append(jars, classes)
714 }
Colin Crossd6891432017-09-27 17:39:56 -0700715 if ctx.Failed() {
716 return
717 }
Colin Cross2fe66872015-03-30 17:20:39 -0700718 }
719
Colin Cross0f37af02017-09-27 17:42:05 -0700720 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
721 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
722
723 var resArgs []string
724 var resDeps android.Paths
725
726 resArgs = append(resArgs, dirArgs...)
727 resDeps = append(resDeps, dirDeps...)
728
729 resArgs = append(resArgs, fileArgs...)
730 resDeps = append(resDeps, fileDeps...)
731
732 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700733 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700734 resArgs = append(resArgs, srcArgs...)
735 resDeps = append(resDeps, srcDeps...)
736 }
Colin Cross40a36712017-09-27 17:41:35 -0700737
738 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700739 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700740 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700741 if ctx.Failed() {
742 return
743 }
Colin Cross20978302015-04-10 17:05:07 -0700744
Colin Cross0a6e0072017-08-30 14:24:55 -0700745 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700746 }
747
Colin Cross6ade34f2017-09-15 13:00:47 -0700748 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700749 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700750
Colin Cross635acc92017-09-12 22:50:46 -0700751 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
752
Colin Cross0a6e0072017-08-30 14:24:55 -0700753 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700754 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700755 var outputFile android.Path
756
757 if len(jars) == 1 && !manifest.Valid() {
758 // Optimization: skip the combine step if there is nothing to do
759 outputFile = jars[0]
760 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700761 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700762 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700763 outputFile = combinedJar
764 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700765
766 if j.properties.Jarjar_rules != nil {
767 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700768 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700769 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700770 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
771 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700772 if ctx.Failed() {
773 return
774 }
775 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700776 j.implementationJarFile = outputFile
777 if j.headerJarFile == nil {
778 j.headerJarFile = j.implementationJarFile
779 }
Colin Cross2fe66872015-03-30 17:20:39 -0700780
Colin Crossc157a8d2017-10-03 17:17:07 -0700781 if ctx.Device() && j.installable() {
Colin Crosscb933592017-11-22 13:49:43 -0800782 outputFile = j.desugar(ctx, flags, outputFile, jarName)
783 }
784
Colin Cross6510f912017-11-29 00:27:14 -0800785 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -0800786 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
787 j.properties.Instrument = true
788 }
789 }
790
Colin Cross6510f912017-11-29 00:27:14 -0800791 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") && j.properties.Instrument {
Colin Crosscb933592017-11-22 13:49:43 -0800792 outputFile = j.instrument(ctx, flags, outputFile, jarName)
793 }
794
795 if ctx.Device() && j.installable() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700796 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -0700797 if ctx.Failed() {
798 return
799 }
Colin Cross2fe66872015-03-30 17:20:39 -0700800 }
Colin Crossb7a63242015-04-16 14:09:14 -0700801 ctx.CheckbuildFile(outputFile)
802 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700803}
804
Colin Cross8eadbf02017-10-24 17:46:00 -0700805func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -0700806 deps deps, flags javaBuilderFlags, jarName string) android.Path {
807
808 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -0700809 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700810 // Compile java sources into turbine.jar.
811 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
812 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
813 if ctx.Failed() {
814 return nil
815 }
816 jars = append(jars, turbineJar)
817 }
818
819 // Combine any static header libraries into classes-header.jar. If there is only
820 // one input jar this step will be skipped.
821 var headerJar android.Path
822 jars = append(jars, deps.staticHeaderJars...)
823
Colin Cross5c6ecc12017-10-23 18:12:27 -0700824 // we cannot skip the combine step for now if there is only one jar
825 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
826 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
827 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
828 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -0700829
830 if j.properties.Jarjar_rules != nil {
831 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
832 // Transform classes.jar into classes-jarjar.jar
833 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
834 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
835 headerJar = jarjarFile
836 if ctx.Failed() {
837 return nil
838 }
839 }
840
841 return headerJar
842}
843
Colin Crosscb933592017-11-22 13:49:43 -0800844func (j *Module) desugar(ctx android.ModuleContext, flags javaBuilderFlags,
845 classesJar android.Path, jarName string) android.Path {
846
847 desugarFlags := []string{
848 "--min_sdk_version " + j.minSdkVersionNumber(ctx),
849 "--desugar_try_with_resources_if_needed=false",
850 "--allow_empty_bootclasspath",
851 }
852
853 if inList("--core-library", j.deviceProperties.Dxflags) {
854 desugarFlags = append(desugarFlags, "--core_library")
855 }
856
857 flags.desugarFlags = strings.Join(desugarFlags, " ")
858
859 desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
860 TransformDesugar(ctx, desugarJar, classesJar, flags)
861
862 return desugarJar
863}
864
865func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
866 classesJar android.Path, jarName string) android.Path {
867
868 specs := j.jacocoStripSpecs(ctx)
869
870 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco", "jacoco-report-classes.jar")
871 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
872
873 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
874
875 j.jacocoReportClassesFile = jacocoReportClassesFile
876
877 return instrumentedJar
878}
879
Nan Zhanged19fc32017-10-19 13:06:22 -0700880func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
881 classesJar android.Path, jarName string) android.Path {
882
883 dxFlags := j.deviceProperties.Dxflags
884 if false /* emma enabled */ {
885 // If you instrument class files that have local variable debug information in
886 // them emma does not correctly maintain the local variable table.
887 // This will cause an error when you try to convert the class files for Android.
888 // The workaround here is to build different dex file here based on emma switch
889 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
890 // option to remove local variable information
891 dxFlags = append(dxFlags, "--no-locals")
892 }
893
Colin Cross6510f912017-11-29 00:27:14 -0800894 if ctx.Config().Getenv("NO_OPTIMIZE_DX") != "" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700895 dxFlags = append(dxFlags, "--no-optimize")
896 }
897
Colin Cross6510f912017-11-29 00:27:14 -0800898 if ctx.Config().Getenv("GENERATE_DEX_DEBUG") != "" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700899 dxFlags = append(dxFlags,
900 "--debug",
901 "--verbose",
902 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
903 "--dump-width=1000")
904 }
905
Colin Crosscb933592017-11-22 13:49:43 -0800906 dxFlags = append(dxFlags, "--min-sdk-version="+j.minSdkVersionNumber(ctx))
Nan Zhanged19fc32017-10-19 13:06:22 -0700907
908 flags.dxFlags = strings.Join(dxFlags, " ")
909
Nan Zhanged19fc32017-10-19 13:06:22 -0700910 // Compile classes.jar into classes.dex and then javalib.jar
911 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
Colin Crosscb933592017-11-22 13:49:43 -0800912 TransformClassesJarToDexJar(ctx, javalibJar, classesJar, flags)
Nan Zhanged19fc32017-10-19 13:06:22 -0700913
914 j.dexJarFile = javalibJar
915 return javalibJar
916}
917
Colin Crosscb933592017-11-22 13:49:43 -0800918// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
919// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
920func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
921 switch String(j.deviceProperties.Sdk_version) {
922 case "", "current", "test_current", "system_current":
Colin Cross6510f912017-11-29 00:27:14 -0800923 return strconv.Itoa(ctx.Config().DefaultAppTargetSdkInt())
Colin Crosscb933592017-11-22 13:49:43 -0800924 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900925 return android.GetNumericSdkVersion(String(j.deviceProperties.Sdk_version))
Colin Crosscb933592017-11-22 13:49:43 -0800926 }
927}
928
Colin Cross59f1bb62017-09-27 17:59:10 -0700929func (j *Module) installable() bool {
930 return j.properties.Installable == nil || *j.properties.Installable
931}
932
Colin Crossf506d872017-07-19 15:53:04 -0700933var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700934
Nan Zhanged19fc32017-10-19 13:06:22 -0700935func (j *Module) HeaderJars() android.Paths {
936 return android.Paths{j.headerJarFile}
937}
938
939func (j *Module) ImplementationJars() android.Paths {
940 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700941}
942
Colin Cross46c9b8b2017-06-22 16:51:17 -0700943func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700944 return j.exportAidlIncludeDirs
945}
946
Colin Cross46c9b8b2017-06-22 16:51:17 -0700947var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700948
Colin Cross46c9b8b2017-06-22 16:51:17 -0700949func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700950 return j.logtagsSrcs
951}
952
Colin Cross2fe66872015-03-30 17:20:39 -0700953//
954// Java libraries (.jar file)
955//
956
Colin Crossf506d872017-07-19 15:53:04 -0700957type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700958 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700959}
960
Colin Crossf506d872017-07-19 15:53:04 -0700961func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700962 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700963
Colin Cross59f1bb62017-09-27 17:59:10 -0700964 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700965 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
966 ctx.ModuleName()+".jar", j.outputFile)
967 }
Colin Crossb7a63242015-04-16 14:09:14 -0700968}
969
Colin Crossf506d872017-07-19 15:53:04 -0700970func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700971 j.deps(ctx)
972}
973
Colin Crossa60ead82017-10-02 18:10:21 -0700974func LibraryFactory(installable bool) func() android.Module {
975 return func() android.Module {
976 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700977
Colin Crossa60ead82017-10-02 18:10:21 -0700978 if !installable {
979 module.properties.Installable = proptools.BoolPtr(false)
980 }
Colin Cross2fe66872015-03-30 17:20:39 -0700981
Colin Crossa60ead82017-10-02 18:10:21 -0700982 module.AddProperties(
983 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700984 &module.Module.deviceProperties,
985 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700986
Colin Crossa60ead82017-10-02 18:10:21 -0700987 InitJavaModule(module, android.HostAndDeviceSupported)
988 return module
989 }
Colin Cross2fe66872015-03-30 17:20:39 -0700990}
991
Colin Crossf506d872017-07-19 15:53:04 -0700992func LibraryHostFactory() android.Module {
993 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700994
Colin Cross6af17aa2017-09-20 12:59:05 -0700995 module.AddProperties(
996 &module.Module.properties,
997 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700998
Colin Cross89536d42017-07-07 14:35:50 -0700999 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001000 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001001}
1002
1003//
1004// Java Binaries (.jar file plus wrapper script)
1005//
1006
Colin Crossf506d872017-07-19 15:53:04 -07001007type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001008 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001009 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001010}
1011
Colin Crossf506d872017-07-19 15:53:04 -07001012type Binary struct {
1013 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001014
Colin Crossf506d872017-07-19 15:53:04 -07001015 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001016
Colin Cross6b4a32d2017-12-05 13:42:45 -08001017 isWrapperVariant bool
1018
Nan Zhang3c807db2017-11-03 14:53:31 -07001019 wrapperFile android.SourcePath
Colin Cross10a03492017-08-10 17:09:43 -07001020 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001021}
1022
Alex Light24237172017-10-26 09:46:21 -07001023func (j *Binary) HostToolPath() android.OptionalPath {
1024 return android.OptionalPathForPath(j.binaryFile)
1025}
1026
Colin Crossf506d872017-07-19 15:53:04 -07001027func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001028 if ctx.Arch().ArchType == android.Common {
1029 // Compile the jar
1030 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001031 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001032 // Handle the binary wrapper
1033 j.isWrapperVariant = true
1034
1035 if String(j.binaryProperties.Wrapper) != "" {
1036 j.wrapperFile = android.PathForModuleSrc(ctx, String(j.binaryProperties.Wrapper)).SourcePath
1037 } else {
1038 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1039 }
1040
1041 // Depend on the installed jar so that the wrapper doesn't get executed by
1042 // another build rule before the jar has been installed.
1043 jarFile := ctx.PrimaryModule().(*Binary).installFile
1044
1045 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1046 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001047 }
Colin Cross2fe66872015-03-30 17:20:39 -07001048}
1049
Colin Crossf506d872017-07-19 15:53:04 -07001050func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001051 if ctx.Arch().ArchType == android.Common {
1052 j.deps(ctx)
1053 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001054}
1055
Colin Crossf506d872017-07-19 15:53:04 -07001056func BinaryFactory() android.Module {
1057 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001058
Colin Cross36242852017-06-23 15:06:31 -07001059 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001060 &module.Module.properties,
1061 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001062 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001063 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001064
Colin Cross6b4a32d2017-12-05 13:42:45 -08001065 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1066 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001067 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001068}
1069
Colin Crossf506d872017-07-19 15:53:04 -07001070func BinaryHostFactory() android.Module {
1071 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001072
Colin Cross36242852017-06-23 15:06:31 -07001073 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001074 &module.Module.properties,
1075 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001076 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001077 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001078
Colin Cross6b4a32d2017-12-05 13:42:45 -08001079 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1080 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001081 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001082}
1083
1084//
1085// Java prebuilts
1086//
1087
Colin Cross74d73e22017-08-02 11:05:49 -07001088type ImportProperties struct {
1089 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001090
Nan Zhangea568a42017-11-08 21:20:04 -08001091 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001092
1093 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001094}
1095
1096type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001097 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001098 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001099
Colin Cross74d73e22017-08-02 11:05:49 -07001100 properties ImportProperties
1101
Colin Cross0a6e0072017-08-30 14:24:55 -07001102 classpathFiles android.Paths
1103 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001104}
1105
Colin Cross74d73e22017-08-02 11:05:49 -07001106func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001107 return &j.prebuilt
1108}
1109
Colin Cross74d73e22017-08-02 11:05:49 -07001110func (j *Import) PrebuiltSrcs() []string {
1111 return j.properties.Jars
1112}
1113
1114func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001115 return j.prebuilt.Name(j.ModuleBase.Name())
1116}
1117
Colin Cross74d73e22017-08-02 11:05:49 -07001118func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001119}
1120
Colin Cross74d73e22017-08-02 11:05:49 -07001121func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1122 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001123
Colin Crosse9a275b2017-10-16 17:09:48 -07001124 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001125 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001126 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001127}
1128
Colin Cross74d73e22017-08-02 11:05:49 -07001129var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001130
Nan Zhanged19fc32017-10-19 13:06:22 -07001131func (j *Import) HeaderJars() android.Paths {
1132 return j.classpathFiles
1133}
1134
1135func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001136 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001137}
1138
Colin Cross74d73e22017-08-02 11:05:49 -07001139func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001140 return nil
1141}
1142
Colin Cross74d73e22017-08-02 11:05:49 -07001143var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001144
Colin Cross74d73e22017-08-02 11:05:49 -07001145func ImportFactory() android.Module {
1146 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001147
Colin Cross74d73e22017-08-02 11:05:49 -07001148 module.AddProperties(&module.properties)
1149
1150 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001151 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1152 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001153}
1154
Colin Cross74d73e22017-08-02 11:05:49 -07001155func ImportFactoryHost() android.Module {
1156 module := &Import{}
1157
1158 module.AddProperties(&module.properties)
1159
1160 android.InitPrebuiltModule(module, &module.properties.Jars)
1161 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1162 return module
1163}
1164
Colin Cross2fe66872015-03-30 17:20:39 -07001165func inList(s string, l []string) bool {
1166 for _, e := range l {
1167 if e == s {
1168 return true
1169 }
1170 }
1171 return false
1172}
Colin Cross89536d42017-07-07 14:35:50 -07001173
1174//
1175// Defaults
1176//
1177type Defaults struct {
1178 android.ModuleBase
1179 android.DefaultsModuleBase
1180}
1181
1182func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1183}
1184
1185func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1186}
1187
1188func defaultsFactory() android.Module {
1189 return DefaultsFactory()
1190}
1191
1192func DefaultsFactory(props ...interface{}) android.Module {
1193 module := &Defaults{}
1194
1195 module.AddProperties(props...)
1196 module.AddProperties(
1197 &CompilerProperties{},
1198 &CompilerDeviceProperties{},
1199 )
1200
1201 android.InitDefaultsModule(module)
1202
1203 return module
1204}
Nan Zhangea568a42017-11-08 21:20:04 -08001205
1206var Bool = proptools.Bool
1207var String = proptools.String