blob: 417cf741872656955c08ac4667e0607f64eb2c0b [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 Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070045
Colin Cross798bfce2016-10-12 14:28:16 -070046 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070047}
48
Colin Cross2fe66872015-03-30 17:20:39 -070049// TODO:
50// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070051// Renderscript
52// Post-jar passes:
53// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070054// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070055// DroidDoc
56// Findbugs
57
Colin Cross89536d42017-07-07 14:35:50 -070058type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070059 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
60 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070061 Srcs []string `android:"arch_variant"`
62
63 // list of source files that should not be used to build the Java module.
64 // This is most useful in the arch/multilib variants to remove non-common files
65 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070066
67 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070068 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070069
Colin Cross86a63ff2017-09-27 17:33:10 -070070 // list of directories that should be excluded from java_resource_dirs
71 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070072
Colin Cross0f37af02017-09-27 17:42:05 -070073 // list of files to use as Java resources
74 Java_resources []string `android:"arch_variant"`
75
76 // list of files that should be excluded from java_resources
77 Exclude_java_resources []string `android:"arch_variant"`
78
Colin Crossfa5eb232017-10-01 20:33:03 -070079 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070081 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070082
Colin Crossfa5eb232017-10-01 20:33:03 -070083 // don't build against the framework libraries (legacy-test, core-junit,
84 // ext, and framework for device targets)
85 No_framework_libs *bool
86
Colin Cross7d5136f2015-05-11 13:39:40 -070087 // list of module-specific flags that will be used for javac compiles
88 Javacflags []string `android:"arch_variant"`
89
Colin Cross7d5136f2015-05-11 13:39:40 -070090 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070091 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070092
93 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070094 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070095
96 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070097 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070098
Colin Cross540eff82017-06-22 17:01:52 -070099 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700100 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700101
102 // If not blank, set the java version passed to javac as -source and -target
103 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700104
105 // If set to false, don't allow this module to be installed. Defaults to true.
106 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700107
Colin Cross0f37af02017-09-27 17:42:05 -0700108 // If set to true, include sources used to compile the module in to the final jar
109 Include_srcs *bool
110
Colin Cross32f676a2017-09-06 13:41:06 -0700111 // List of modules to use as annotation processors
112 Annotation_processors []string
113
114 // List of classes to pass to javac to use as annotation processors
115 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700116
Nan Zhang61eaedb2017-11-02 13:28:15 -0700117 // The number of Java source entries each Javac instance can process
118 Javac_shard_size *int64
119
Colin Cross1369cdb2017-09-29 17:58:17 -0700120 Openjdk9 struct {
121 // List of source files that should only be used when passing -source 1.9
122 Srcs []string
123
124 // List of javac flags that should only be used when passing -source 1.9
125 Javacflags []string
126 }
Colin Crosscb933592017-11-22 13:49:43 -0800127
128 Jacoco struct {
129 // List of classes to include for instrumentation with jacoco to collect coverage
130 // information at runtime when building with coverage enabled. If unset defaults to all
131 // classes.
132 // Supports '*' as the last character of an entry in the list as a wildcard match.
133 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
134 // it matches classes in the package that have the class name as a prefix.
135 Include_filter []string
136
137 // List of classes to exclude from instrumentation with jacoco to collect coverage
138 // information at runtime when building with coverage enabled. Overrides classes selected
139 // by the include_filter property.
140 // Supports '*' as the last character of an entry in the list as a wildcard match.
141 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
142 // it matches classes in the package that have the class name as a prefix.
143 Exclude_filter []string
144 }
145
146 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700147}
148
Colin Cross89536d42017-07-07 14:35:50 -0700149type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700150 // list of module-specific flags that will be used for dex compiles
151 Dxflags []string `android:"arch_variant"`
152
Colin Cross7d5136f2015-05-11 13:39:40 -0700153 // if not blank, set to the version of the sdk to compile against
Nan Zhangea568a42017-11-08 21:20:04 -0800154 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700155
Colin Crossebe1a512017-11-14 13:12:14 -0800156 Aidl struct {
157 // Top level directories to pass to aidl tool
158 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700159
Colin Crossebe1a512017-11-14 13:12:14 -0800160 // Directories rooted at the Android.bp file to pass to aidl tool
161 Local_include_dirs []string
162
163 // directories that should be added as include directories for any aidl sources of modules
164 // that depend on this module, as well as to aidl for this module.
165 Export_include_dirs []string
166 }
Colin Cross92430102017-10-09 14:59:32 -0700167
168 // If true, export a copy of the module as a -hostdex module for host testing.
169 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700170
Colin Crossa22116e2017-10-19 14:18:58 -0700171 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
172 // true.
173 Dex_preopt *bool
174
Colin Cross1369cdb2017-09-29 17:58:17 -0700175 // When targeting 1.9, override the modules to use with --system
176 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700177}
178
Colin Cross46c9b8b2017-06-22 16:51:17 -0700179// Module contains the properties and members used by all java module types
180type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700181 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700182 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700183
Colin Cross89536d42017-07-07 14:35:50 -0700184 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700185 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700186 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700187
Nan Zhanged19fc32017-10-19 13:06:22 -0700188 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
189 headerJarFile android.Path
190
191 // full implementation jar file suitable for static dependency of another module compile
192 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700193
Colin Cross6ade34f2017-09-15 13:00:47 -0700194 // output file containing classes.dex
195 dexJarFile android.Path
196
Colin Crosscb933592017-11-22 13:49:43 -0800197 // output file containing uninstrumented classes that will be instrumented by jacoco
198 jacocoReportClassesFile android.Path
199
Colin Crossb7a63242015-04-16 14:09:14 -0700200 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700201 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700202
Colin Cross635c3b02016-05-18 15:37:25 -0700203 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700204
Colin Cross635c3b02016-05-18 15:37:25 -0700205 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700206
Colin Cross59149b62017-10-16 18:07:29 -0700207 // jars containing source files that should be included in the javac command line,
Colin Crossb7a63242015-04-16 14:09:14 -0700208 // for example R.java generated by aapt for android apps
Colin Cross59149b62017-10-16 18:07:29 -0700209 ExtraSrcJars android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700210
Colin Cross2fe66872015-03-30 17:20:39 -0700211 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700212 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700213}
214
Colin Crossf506d872017-07-19 15:53:04 -0700215type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700216 HeaderJars() android.Paths
217 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700218 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700219}
220
Colin Cross89536d42017-07-07 14:35:50 -0700221func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
222 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
223 android.InitDefaultableModule(module)
224}
225
Colin Crossbe1da472017-07-07 15:59:46 -0700226type dependencyTag struct {
227 blueprint.BaseDependencyTag
228 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700229}
230
Colin Crossbe1da472017-07-07 15:59:46 -0700231var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700232 staticLibTag = dependencyTag{name: "staticlib"}
233 libTag = dependencyTag{name: "javalib"}
234 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700235 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700236 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross93e85952017-08-15 13:34:18 -0700237 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700238)
Colin Cross2fe66872015-03-30 17:20:39 -0700239
Colin Crossfc3674a2017-09-18 17:41:52 -0700240type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700241 useModule, useFiles, useDefaultLibs, invalidVersion bool
242
Colin Cross1369cdb2017-09-29 17:58:17 -0700243 module string
244 systemModules string
245
246 jar android.Path
247 aidl android.Path
248}
249
250func sdkStringToNumber(ctx android.BaseContext, v string) int {
251 switch v {
252 case "", "current", "system_current", "test_current":
253 return 10000
254 default:
255 if i, err := strconv.Atoi(v); err != nil {
256 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
257 return -1
258 } else {
259 return i
260 }
261 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700262}
263
264func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700265 i := sdkStringToNumber(ctx, v)
266 if i == -1 {
267 // Invalid sdk version, error handled by sdkStringToNumber.
268 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700269 }
270
271 toFile := func(v string) sdkDep {
272 dir := filepath.Join("prebuilts/sdk", v)
273 jar := filepath.Join(dir, "android.jar")
274 aidl := filepath.Join(dir, "framework.aidl")
275 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
276 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700277
278 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() {
279 return sdkDep{
280 invalidVersion: true,
281 module: "sdk_v" + v,
282 }
283 }
284
Colin Crossfc3674a2017-09-18 17:41:52 -0700285 if !jarPath.Valid() {
286 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
287 return sdkDep{}
288 }
Colin Cross47ff2522017-10-02 14:22:08 -0700289
Colin Crossfc3674a2017-09-18 17:41:52 -0700290 if !aidlPath.Valid() {
291 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
292 return sdkDep{}
293 }
Colin Cross47ff2522017-10-02 14:22:08 -0700294
Colin Crossfc3674a2017-09-18 17:41:52 -0700295 return sdkDep{
296 useFiles: true,
297 jar: jarPath.Path(),
298 aidl: aidlPath.Path(),
299 }
300 }
301
Colin Cross2ebc4762017-10-20 14:00:31 -0700302 //toModule := func(m string) sdkDep {
303 // return sdkDep{
304 // useModule: true,
305 // module: m,
306 // systemModules: m + "_system_modules",
307 // }
308 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700309
Colin Cross8b9d37b2017-09-22 15:30:06 -0700310 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700311 return toFile(v)
312 }
313
314 switch v {
315 case "":
316 return sdkDep{
317 useDefaultLibs: true,
318 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700319 // TODO(ccross): re-enable these once we generate stubs, until then
320 // use the stubs in prebuilts/sdk/*current
321 //case "current":
322 // return toModule("android_stubs_current")
323 //case "system_current":
324 // return toModule("android_system_stubs_current")
325 //case "test_current":
326 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700327 default:
328 return toFile(v)
329 }
330}
331
Colin Crossbe1da472017-07-07 15:59:46 -0700332func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700333 if ctx.Device() {
334 if !proptools.Bool(j.properties.No_standard_libs) {
Nan Zhangea568a42017-11-08 21:20:04 -0800335 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Crossfc3674a2017-09-18 17:41:52 -0700336 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700337 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700338 if ctx.AConfig().TargetOpenJDK9() {
339 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
340 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700341 if !proptools.Bool(j.properties.No_framework_libs) {
342 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
343 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700344 } else if sdkDep.useModule {
345 if ctx.AConfig().TargetOpenJDK9() {
346 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
347 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700348 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700349 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700350 } else if j.deviceProperties.System_modules == nil {
351 ctx.PropertyErrorf("no_standard_libs",
352 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
353 } else if *j.deviceProperties.System_modules != "none" && ctx.AConfig().TargetOpenJDK9() {
354 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700355 }
356 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700357
Colin Crossf506d872017-07-19 15:53:04 -0700358 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
359 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700360 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700361
362 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700363 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross6af17aa2017-09-20 12:59:05 -0700364
365 if j.hasSrcExt(".proto") {
366 protoDeps(ctx, &j.protoProperties)
367 }
Colin Cross93e85952017-08-15 13:34:18 -0700368
369 if j.hasSrcExt(".kt") {
370 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
371 // Kotlin files
372 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
373 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700374}
375
376func hasSrcExt(srcs []string, ext string) bool {
377 for _, src := range srcs {
378 if filepath.Ext(src) == ext {
379 return true
380 }
381 }
382
383 return false
384}
385
Nan Zhang61eaedb2017-11-02 13:28:15 -0700386func shardPaths(paths android.Paths, shardSize int) []android.Paths {
387 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
388 for len(paths) > shardSize {
389 ret = append(ret, paths[0:shardSize])
390 paths = paths[shardSize:]
391 }
392 if len(paths) > 0 {
393 ret = append(ret, paths)
394 }
395 return ret
396}
397
Colin Cross6af17aa2017-09-20 12:59:05 -0700398func (j *Module) hasSrcExt(ext string) bool {
399 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700400}
401
Colin Cross46c9b8b2017-06-22 16:51:17 -0700402func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700403 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700404
Colin Crossebe1a512017-11-14 13:12:14 -0800405 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
406 aidlIncludes = append(aidlIncludes,
407 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
408 aidlIncludes = append(aidlIncludes,
409 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700410
411 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700412 if aidlPreprocess.Valid() {
413 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700414 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700415 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700416 }
417
Colin Cross635c3b02016-05-18 15:37:25 -0700418 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800419 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700420 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crosse243c232017-10-21 15:16:37 -0700421 if src := android.ExistentPathForSource(ctx, "", ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700422 flags = append(flags, "-I"+src.String())
423 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700424
Colin Crossf03c82b2015-04-13 13:53:40 -0700425 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700426}
427
Colin Cross32f676a2017-09-06 13:41:06 -0700428type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700429 classpath android.Paths
430 bootClasspath android.Paths
431 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700432 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700433 staticJarResources android.Paths
434 aidlIncludeDirs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700435 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700436 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700437 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700438 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700439}
Colin Cross2fe66872015-03-30 17:20:39 -0700440
Colin Cross32f676a2017-09-06 13:41:06 -0700441func (j *Module) collectDeps(ctx android.ModuleContext) deps {
442 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700443
Nan Zhangea568a42017-11-08 21:20:04 -0800444 sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross47ff2522017-10-02 14:22:08 -0700445 if sdkDep.invalidVersion {
446 ctx.AddMissingDependencies([]string{sdkDep.module})
447 } else if sdkDep.useFiles {
Nan Zhanged19fc32017-10-19 13:06:22 -0700448 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Crossfc3674a2017-09-18 17:41:52 -0700449 deps.classpath = append(deps.classpath, sdkDep.jar)
450 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
451 }
452
Colin Crossd11fcda2017-10-23 17:59:01 -0700453 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700454 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700455 tag := ctx.OtherModuleDependencyTag(module)
456
Colin Crossf506d872017-07-19 15:53:04 -0700457 dep, _ := module.(Dependency)
458 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700459 switch tag {
460 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700461 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700462 case systemModulesTag:
463 if deps.systemModules != nil {
464 panic("Found two system module dependencies")
465 }
466 sm := module.(*SystemModules)
467 if sm.outputFile == nil {
468 panic("Missing directory for system module dependency")
469 }
470 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700471 default:
472 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700473 }
Colin Crossec7a0422017-07-07 14:47:12 -0700474 return
475 }
476
Colin Crossbe1da472017-07-07 15:59:46 -0700477 switch tag {
478 case bootClasspathTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700479 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Crossf506d872017-07-19 15:53:04 -0700480 case libTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700481 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Colin Crossf506d872017-07-19 15:53:04 -0700482 case staticLibTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700483 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
484 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
485 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700486 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700487 if ctx.ModuleName() == "framework" {
488 // framework.jar has a one-off dependency on the R.java and Manifest.java files
489 // generated by framework-res.apk
Colin Cross59149b62017-10-16 18:07:29 -0700490 // TODO(ccross): aapt java files should go in a src jar
Colin Crossec7a0422017-07-07 14:47:12 -0700491 }
Colin Cross93e85952017-08-15 13:34:18 -0700492 case kotlinStdlibTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700493 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossbe1da472017-07-07 15:59:46 -0700494 default:
495 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700496 }
Colin Crossbe1da472017-07-07 15:59:46 -0700497
Colin Cross32f676a2017-09-06 13:41:06 -0700498 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700499 })
500
Colin Cross32f676a2017-09-06 13:41:06 -0700501 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700502}
503
Nan Zhanged19fc32017-10-19 13:06:22 -0700504func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700505
Colin Crossf03c82b2015-04-13 13:53:40 -0700506 var flags javaBuilderFlags
507
Nan Zhanged19fc32017-10-19 13:06:22 -0700508 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700509 javacFlags := j.properties.Javacflags
Colin Cross1369cdb2017-09-29 17:58:17 -0700510 if ctx.AConfig().TargetOpenJDK9() {
511 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700512 }
Colin Cross126a25c2017-10-31 13:55:34 -0700513 if ctx.AConfig().MinimizeJavaDebugInfo() {
514 // Override the -g flag passed globally to remove local variable debug info to reduce
515 // disk and memory usage.
516 javacFlags = append(javacFlags, "-g:source,lines")
517 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700518 if len(javacFlags) > 0 {
519 // optimization.
520 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
521 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700522 }
Colin Cross64162712017-08-08 13:17:59 -0700523
Nan Zhanged19fc32017-10-19 13:06:22 -0700524 // javaVersion flag.
Nan Zhangea568a42017-11-08 21:20:04 -0800525 sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
Colin Cross64162712017-08-08 13:17:59 -0700526 if j.properties.Java_version != nil {
527 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700528 } else if ctx.Device() && sdk <= 23 {
529 flags.javaVersion = "1.7"
530 } else if ctx.Device() && sdk <= 26 || !ctx.AConfig().TargetOpenJDK9() {
531 flags.javaVersion = "1.8"
Nan Zhangea568a42017-11-08 21:20:04 -0800532 } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == 10000 {
Colin Cross2ebc4762017-10-20 14:00:31 -0700533 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
534 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700535 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700536 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700537 }
538
Nan Zhanged19fc32017-10-19 13:06:22 -0700539 // classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700540 flags.bootClasspath.AddPaths(deps.bootClasspath)
541 flags.classpath.AddPaths(deps.classpath)
Nan Zhanged19fc32017-10-19 13:06:22 -0700542 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700543 if deps.systemModules != nil {
544 flags.systemModules = append(flags.systemModules, deps.systemModules)
545 }
546
Nan Zhanged19fc32017-10-19 13:06:22 -0700547 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700548 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700549 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700550 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700551 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
552 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700553 }
554
Nan Zhanged19fc32017-10-19 13:06:22 -0700555 return flags
556}
Colin Crossc0b06f12015-04-08 13:03:43 -0700557
Nan Zhanged19fc32017-10-19 13:06:22 -0700558func (j *Module) compile(ctx android.ModuleContext) {
559
Colin Crossebe1a512017-11-14 13:12:14 -0800560 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700561
562 deps := j.collectDeps(ctx)
563 flags := j.collectBuilderFlags(ctx, deps)
564
565 if ctx.AConfig().TargetOpenJDK9() {
566 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
567 }
568 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700569 if hasSrcExt(srcFiles.Strings(), ".proto") {
570 flags = protoFlags(ctx, &j.protoProperties, flags)
571 }
572
Colin Crossaf050172017-11-15 23:01:59 -0800573 srcFiles = j.genSources(ctx, srcFiles, flags)
574
575 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700576 srcJars = append(srcJars, deps.srcJars...)
Colin Cross59149b62017-10-16 18:07:29 -0700577 srcJars = append(srcJars, j.ExtraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700578
Colin Cross0a6e0072017-08-30 14:24:55 -0700579 var jars android.Paths
580
Colin Cross1ee23172017-10-18 14:44:18 -0700581 jarName := ctx.ModuleName() + ".jar"
582
Colin Cross93e85952017-08-15 13:34:18 -0700583 if srcFiles.HasExt(".kt") {
584 // If there are kotlin files, compile them first but pass all the kotlin and java files
585 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
586 // won't emit any classes for them.
587
588 flags.kotlincFlags = "-no-stdlib"
589 if ctx.Device() {
590 flags.kotlincFlags += " -no-jdk"
591 }
592
593 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
594 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
595
Colin Cross1ee23172017-10-18 14:44:18 -0700596 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross93e85952017-08-15 13:34:18 -0700597 TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags)
598 if ctx.Failed() {
599 return
600 }
601
602 // Make javac rule depend on the kotlinc rule
603 flags.classpath = append(flags.classpath, kotlinJar)
604 // Jar kotlin classes into the final jar after javac
605 jars = append(jars, kotlinJar)
606 jars = append(jars, deps.kotlinStdlib...)
607 }
608
Nan Zhanged19fc32017-10-19 13:06:22 -0700609 javaSrcFiles := srcFiles.FilterByExt(".java")
610 var uniqueSrcFiles android.Paths
611 set := make(map[string]bool)
612 for _, v := range javaSrcFiles {
613 if _, found := set[v.String()]; !found {
614 set[v.String()] = true
615 uniqueSrcFiles = append(uniqueSrcFiles, v)
616 }
617 }
618
Nan Zhang61eaedb2017-11-02 13:28:15 -0700619 enable_sharding := false
Nan Zhanged19fc32017-10-19 13:06:22 -0700620 if ctx.Device() && !ctx.AConfig().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700621 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
622 enable_sharding = true
623 if len(j.properties.Annotation_processors) != 0 ||
624 len(j.properties.Annotation_processor_classes) != 0 {
625 ctx.PropertyErrorf("javac_shard_size",
626 "%q cannot be set when annotation processors are enabled.",
627 j.properties.Javac_shard_size)
628 }
629 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700630 // If sdk jar is java module, then directly return classesJar as header.jar
631 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
632 j.Name() != "android_test_stubs_current" {
633 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
634 if ctx.Failed() {
635 return
636 }
637 }
638 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700639 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700640 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700641 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
642 // If error-prone is enabled, add an additional rule to compile the java files into
643 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700644 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700645 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
646 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700647 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700648 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700649 extraJarDeps = append(extraJarDeps, errorprone)
650 }
651
Nan Zhang61eaedb2017-11-02 13:28:15 -0700652 if enable_sharding {
653 flags.classpath.AddPaths([]android.Path{j.headerJarFile})
654 shardSize := int(*(j.properties.Javac_shard_size))
655 var shardSrcs []android.Paths
656 if len(uniqueSrcFiles) > 0 {
657 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
658 for idx, shardSrc := range shardSrcs {
659 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
660 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
661 jars = append(jars, classes)
662 }
663 }
664 if len(srcJars) > 0 {
665 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
666 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
667 jars = append(jars, classes)
668 }
669 } else {
670 classes := android.PathForModuleOut(ctx, "javac", jarName)
671 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
672 jars = append(jars, classes)
673 }
Colin Crossd6891432017-09-27 17:39:56 -0700674 if ctx.Failed() {
675 return
676 }
Colin Cross2fe66872015-03-30 17:20:39 -0700677 }
678
Colin Cross0f37af02017-09-27 17:42:05 -0700679 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
680 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
681
682 var resArgs []string
683 var resDeps android.Paths
684
685 resArgs = append(resArgs, dirArgs...)
686 resDeps = append(resDeps, dirDeps...)
687
688 resArgs = append(resArgs, fileArgs...)
689 resDeps = append(resDeps, fileDeps...)
690
691 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700692 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700693 resArgs = append(resArgs, srcArgs...)
694 resDeps = append(resDeps, srcDeps...)
695 }
Colin Cross40a36712017-09-27 17:41:35 -0700696
697 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700698 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700699 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700700 if ctx.Failed() {
701 return
702 }
Colin Cross20978302015-04-10 17:05:07 -0700703
Colin Cross0a6e0072017-08-30 14:24:55 -0700704 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700705 }
706
Colin Cross6ade34f2017-09-15 13:00:47 -0700707 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700708 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700709
Colin Cross635acc92017-09-12 22:50:46 -0700710 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
711
Colin Cross0a6e0072017-08-30 14:24:55 -0700712 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700713 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700714 var outputFile android.Path
715
716 if len(jars) == 1 && !manifest.Valid() {
717 // Optimization: skip the combine step if there is nothing to do
718 outputFile = jars[0]
719 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700720 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700721 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700722 outputFile = combinedJar
723 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700724
725 if j.properties.Jarjar_rules != nil {
726 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700727 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700728 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700729 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
730 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700731 if ctx.Failed() {
732 return
733 }
734 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700735 j.implementationJarFile = outputFile
736 if j.headerJarFile == nil {
737 j.headerJarFile = j.implementationJarFile
738 }
Colin Cross2fe66872015-03-30 17:20:39 -0700739
Colin Crossc157a8d2017-10-03 17:17:07 -0700740 if ctx.Device() && j.installable() {
Colin Crosscb933592017-11-22 13:49:43 -0800741 outputFile = j.desugar(ctx, flags, outputFile, jarName)
742 }
743
744 if ctx.AConfig().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
745 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
746 j.properties.Instrument = true
747 }
748 }
749
750 if ctx.AConfig().IsEnvTrue("EMMA_INSTRUMENT") && j.properties.Instrument {
751 outputFile = j.instrument(ctx, flags, outputFile, jarName)
752 }
753
754 if ctx.Device() && j.installable() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700755 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -0700756 if ctx.Failed() {
757 return
758 }
Colin Cross2fe66872015-03-30 17:20:39 -0700759 }
Colin Crossb7a63242015-04-16 14:09:14 -0700760 ctx.CheckbuildFile(outputFile)
761 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700762}
763
Colin Cross8eadbf02017-10-24 17:46:00 -0700764func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -0700765 deps deps, flags javaBuilderFlags, jarName string) android.Path {
766
767 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -0700768 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700769 // Compile java sources into turbine.jar.
770 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
771 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
772 if ctx.Failed() {
773 return nil
774 }
775 jars = append(jars, turbineJar)
776 }
777
778 // Combine any static header libraries into classes-header.jar. If there is only
779 // one input jar this step will be skipped.
780 var headerJar android.Path
781 jars = append(jars, deps.staticHeaderJars...)
782
Colin Cross5c6ecc12017-10-23 18:12:27 -0700783 // we cannot skip the combine step for now if there is only one jar
784 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
785 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
786 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
787 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -0700788
789 if j.properties.Jarjar_rules != nil {
790 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
791 // Transform classes.jar into classes-jarjar.jar
792 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
793 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
794 headerJar = jarjarFile
795 if ctx.Failed() {
796 return nil
797 }
798 }
799
800 return headerJar
801}
802
Colin Crosscb933592017-11-22 13:49:43 -0800803func (j *Module) desugar(ctx android.ModuleContext, flags javaBuilderFlags,
804 classesJar android.Path, jarName string) android.Path {
805
806 desugarFlags := []string{
807 "--min_sdk_version " + j.minSdkVersionNumber(ctx),
808 "--desugar_try_with_resources_if_needed=false",
809 "--allow_empty_bootclasspath",
810 }
811
812 if inList("--core-library", j.deviceProperties.Dxflags) {
813 desugarFlags = append(desugarFlags, "--core_library")
814 }
815
816 flags.desugarFlags = strings.Join(desugarFlags, " ")
817
818 desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
819 TransformDesugar(ctx, desugarJar, classesJar, flags)
820
821 return desugarJar
822}
823
824func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
825 classesJar android.Path, jarName string) android.Path {
826
827 specs := j.jacocoStripSpecs(ctx)
828
829 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco", "jacoco-report-classes.jar")
830 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
831
832 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
833
834 j.jacocoReportClassesFile = jacocoReportClassesFile
835
836 return instrumentedJar
837}
838
Nan Zhanged19fc32017-10-19 13:06:22 -0700839func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
840 classesJar android.Path, jarName string) android.Path {
841
842 dxFlags := j.deviceProperties.Dxflags
843 if false /* emma enabled */ {
844 // If you instrument class files that have local variable debug information in
845 // them emma does not correctly maintain the local variable table.
846 // This will cause an error when you try to convert the class files for Android.
847 // The workaround here is to build different dex file here based on emma switch
848 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
849 // option to remove local variable information
850 dxFlags = append(dxFlags, "--no-locals")
851 }
852
853 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
854 dxFlags = append(dxFlags, "--no-optimize")
855 }
856
857 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
858 dxFlags = append(dxFlags,
859 "--debug",
860 "--verbose",
861 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
862 "--dump-width=1000")
863 }
864
Colin Crosscb933592017-11-22 13:49:43 -0800865 dxFlags = append(dxFlags, "--min-sdk-version="+j.minSdkVersionNumber(ctx))
Nan Zhanged19fc32017-10-19 13:06:22 -0700866
867 flags.dxFlags = strings.Join(dxFlags, " ")
868
Nan Zhanged19fc32017-10-19 13:06:22 -0700869 // Compile classes.jar into classes.dex and then javalib.jar
870 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
Colin Crosscb933592017-11-22 13:49:43 -0800871 TransformClassesJarToDexJar(ctx, javalibJar, classesJar, flags)
Nan Zhanged19fc32017-10-19 13:06:22 -0700872
873 j.dexJarFile = javalibJar
874 return javalibJar
875}
876
Colin Crosscb933592017-11-22 13:49:43 -0800877// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
878// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
879func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
880 switch String(j.deviceProperties.Sdk_version) {
881 case "", "current", "test_current", "system_current":
882 return strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
883 default:
884 return String(j.deviceProperties.Sdk_version)
885 }
886}
887
Colin Cross59f1bb62017-09-27 17:59:10 -0700888func (j *Module) installable() bool {
889 return j.properties.Installable == nil || *j.properties.Installable
890}
891
Colin Crossf506d872017-07-19 15:53:04 -0700892var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700893
Nan Zhanged19fc32017-10-19 13:06:22 -0700894func (j *Module) HeaderJars() android.Paths {
895 return android.Paths{j.headerJarFile}
896}
897
898func (j *Module) ImplementationJars() android.Paths {
899 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700900}
901
Colin Cross46c9b8b2017-06-22 16:51:17 -0700902func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700903 return j.exportAidlIncludeDirs
904}
905
Colin Cross46c9b8b2017-06-22 16:51:17 -0700906var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700907
Colin Cross46c9b8b2017-06-22 16:51:17 -0700908func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700909 return j.logtagsSrcs
910}
911
Colin Cross2fe66872015-03-30 17:20:39 -0700912//
913// Java libraries (.jar file)
914//
915
Colin Crossf506d872017-07-19 15:53:04 -0700916type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700917 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700918}
919
Colin Crossf506d872017-07-19 15:53:04 -0700920func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700921 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700922
Colin Cross59f1bb62017-09-27 17:59:10 -0700923 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700924 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
925 ctx.ModuleName()+".jar", j.outputFile)
926 }
Colin Crossb7a63242015-04-16 14:09:14 -0700927}
928
Colin Crossf506d872017-07-19 15:53:04 -0700929func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700930 j.deps(ctx)
931}
932
Colin Crossa60ead82017-10-02 18:10:21 -0700933func LibraryFactory(installable bool) func() android.Module {
934 return func() android.Module {
935 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700936
Colin Crossa60ead82017-10-02 18:10:21 -0700937 if !installable {
938 module.properties.Installable = proptools.BoolPtr(false)
939 }
Colin Cross2fe66872015-03-30 17:20:39 -0700940
Colin Crossa60ead82017-10-02 18:10:21 -0700941 module.AddProperties(
942 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700943 &module.Module.deviceProperties,
944 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700945
Colin Crossa60ead82017-10-02 18:10:21 -0700946 InitJavaModule(module, android.HostAndDeviceSupported)
947 return module
948 }
Colin Cross2fe66872015-03-30 17:20:39 -0700949}
950
Colin Crossf506d872017-07-19 15:53:04 -0700951func LibraryHostFactory() android.Module {
952 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700953
Colin Cross6af17aa2017-09-20 12:59:05 -0700954 module.AddProperties(
955 &module.Module.properties,
956 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700957
Colin Cross89536d42017-07-07 14:35:50 -0700958 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700959 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700960}
961
962//
963// Java Binaries (.jar file plus wrapper script)
964//
965
Colin Crossf506d872017-07-19 15:53:04 -0700966type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700967 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -0800968 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700969}
970
Colin Crossf506d872017-07-19 15:53:04 -0700971type Binary struct {
972 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700973
Colin Crossf506d872017-07-19 15:53:04 -0700974 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700975
Nan Zhang3c807db2017-11-03 14:53:31 -0700976 wrapperFile android.SourcePath
Colin Cross10a03492017-08-10 17:09:43 -0700977 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700978}
979
Alex Light24237172017-10-26 09:46:21 -0700980func (j *Binary) HostToolPath() android.OptionalPath {
981 return android.OptionalPathForPath(j.binaryFile)
982}
983
Colin Crossf506d872017-07-19 15:53:04 -0700984func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
985 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700986
987 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
988 // another build rule before the jar has been installed.
Nan Zhangea568a42017-11-08 21:20:04 -0800989 if String(j.binaryProperties.Wrapper) != "" {
990 j.wrapperFile = android.PathForModuleSrc(ctx, String(j.binaryProperties.Wrapper)).SourcePath
Nan Zhang3c807db2017-11-03 14:53:31 -0700991 } else {
992 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
993 }
Colin Cross5c517922017-08-31 12:29:17 -0700994 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
995 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700996}
997
Colin Crossf506d872017-07-19 15:53:04 -0700998func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700999 j.deps(ctx)
1000}
1001
Colin Crossf506d872017-07-19 15:53:04 -07001002func BinaryFactory() android.Module {
1003 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001004
Colin Cross36242852017-06-23 15:06:31 -07001005 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001006 &module.Module.properties,
1007 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001008 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001009 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001010
Colin Cross89536d42017-07-07 14:35:50 -07001011 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001012 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001013}
1014
Colin Crossf506d872017-07-19 15:53:04 -07001015func BinaryHostFactory() android.Module {
1016 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001017
Colin Cross36242852017-06-23 15:06:31 -07001018 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001019 &module.Module.properties,
1020 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001021 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001022 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001023
Colin Cross89536d42017-07-07 14:35:50 -07001024 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001025 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001026}
1027
1028//
1029// Java prebuilts
1030//
1031
Colin Cross74d73e22017-08-02 11:05:49 -07001032type ImportProperties struct {
1033 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001034
Nan Zhangea568a42017-11-08 21:20:04 -08001035 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001036
1037 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001038}
1039
1040type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001041 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001042 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001043
Colin Cross74d73e22017-08-02 11:05:49 -07001044 properties ImportProperties
1045
Colin Cross0a6e0072017-08-30 14:24:55 -07001046 classpathFiles android.Paths
1047 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -07001048}
1049
Colin Cross74d73e22017-08-02 11:05:49 -07001050func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001051 return &j.prebuilt
1052}
1053
Colin Cross74d73e22017-08-02 11:05:49 -07001054func (j *Import) PrebuiltSrcs() []string {
1055 return j.properties.Jars
1056}
1057
1058func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001059 return j.prebuilt.Name(j.ModuleBase.Name())
1060}
1061
Colin Cross74d73e22017-08-02 11:05:49 -07001062func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -07001063}
1064
Colin Cross74d73e22017-08-02 11:05:49 -07001065func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1066 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001067
Colin Crosse9a275b2017-10-16 17:09:48 -07001068 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001069 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001070 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -07001071}
1072
Colin Cross74d73e22017-08-02 11:05:49 -07001073var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001074
Nan Zhanged19fc32017-10-19 13:06:22 -07001075func (j *Import) HeaderJars() android.Paths {
1076 return j.classpathFiles
1077}
1078
1079func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -07001080 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -07001081}
1082
Colin Cross74d73e22017-08-02 11:05:49 -07001083func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001084 return nil
1085}
1086
Colin Cross74d73e22017-08-02 11:05:49 -07001087var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001088
Colin Cross74d73e22017-08-02 11:05:49 -07001089func ImportFactory() android.Module {
1090 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001091
Colin Cross74d73e22017-08-02 11:05:49 -07001092 module.AddProperties(&module.properties)
1093
1094 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -07001095 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
1096 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001097}
1098
Colin Cross74d73e22017-08-02 11:05:49 -07001099func ImportFactoryHost() android.Module {
1100 module := &Import{}
1101
1102 module.AddProperties(&module.properties)
1103
1104 android.InitPrebuiltModule(module, &module.properties.Jars)
1105 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
1106 return module
1107}
1108
Colin Cross2fe66872015-03-30 17:20:39 -07001109func inList(s string, l []string) bool {
1110 for _, e := range l {
1111 if e == s {
1112 return true
1113 }
1114 }
1115 return false
1116}
Colin Cross89536d42017-07-07 14:35:50 -07001117
1118//
1119// Defaults
1120//
1121type Defaults struct {
1122 android.ModuleBase
1123 android.DefaultsModuleBase
1124}
1125
1126func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1127}
1128
1129func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1130}
1131
1132func defaultsFactory() android.Module {
1133 return DefaultsFactory()
1134}
1135
1136func DefaultsFactory(props ...interface{}) android.Module {
1137 module := &Defaults{}
1138
1139 module.AddProperties(props...)
1140 module.AddProperties(
1141 &CompilerProperties{},
1142 &CompilerDeviceProperties{},
1143 )
1144
1145 android.InitDefaultsModule(module)
1146
1147 return module
1148}
Nan Zhangea568a42017-11-08 21:20:04 -08001149
1150var Bool = proptools.Bool
1151var String = proptools.String