blob: f5035e7df1fa8da007ce66e4be88778ff2ccbd4f [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 Crossba211132017-06-22 15:36:39 -070054// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070055// Jarjar
56// Dex
57// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070058// DroidDoc
59// Findbugs
60
Colin Cross89536d42017-07-07 14:35:50 -070061type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070062 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
63 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070064 Srcs []string `android:"arch_variant"`
65
66 // list of source files that should not be used to build the Java module.
67 // This is most useful in the arch/multilib variants to remove non-common files
68 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070069
70 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070071 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070072
Colin Cross86a63ff2017-09-27 17:33:10 -070073 // list of directories that should be excluded from java_resource_dirs
74 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070075
Colin Cross0f37af02017-09-27 17:42:05 -070076 // list of files to use as Java resources
77 Java_resources []string `android:"arch_variant"`
78
79 // list of files that should be excluded from java_resources
80 Exclude_java_resources []string `android:"arch_variant"`
81
Colin Crossfa5eb232017-10-01 20:33:03 -070082 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070083 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070084 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070085
Colin Crossfa5eb232017-10-01 20:33:03 -070086 // don't build against the framework libraries (legacy-test, core-junit,
87 // ext, and framework for device targets)
88 No_framework_libs *bool
89
Colin Cross7d5136f2015-05-11 13:39:40 -070090 // list of module-specific flags that will be used for javac compiles
91 Javacflags []string `android:"arch_variant"`
92
Colin Cross7d5136f2015-05-11 13:39:40 -070093 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070094 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070095
96 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070097 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070098
99 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700100 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700101
Colin Cross540eff82017-06-22 17:01:52 -0700102 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700103 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700104
105 // If not blank, set the java version passed to javac as -source and -target
106 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700107
108 // If set to false, don't allow this module to be installed. Defaults to true.
109 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700110
Colin Cross0f37af02017-09-27 17:42:05 -0700111 // If set to true, include sources used to compile the module in to the final jar
112 Include_srcs *bool
113
Colin Cross32f676a2017-09-06 13:41:06 -0700114 // List of modules to use as annotation processors
115 Annotation_processors []string
116
117 // List of classes to pass to javac to use as annotation processors
118 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119
120 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 Cross540eff82017-06-22 17:01:52 -0700127}
128
Colin Cross89536d42017-07-07 14:35:50 -0700129type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700130 // list of module-specific flags that will be used for dex compiles
131 Dxflags []string `android:"arch_variant"`
132
Colin Cross7d5136f2015-05-11 13:39:40 -0700133 // if not blank, set to the version of the sdk to compile against
134 Sdk_version string
135
Colin Cross7d5136f2015-05-11 13:39:40 -0700136 // directories to pass to aidl tool
137 Aidl_includes []string
138
139 // directories that should be added as include directories
140 // for any aidl sources of modules that depend on this module
141 Export_aidl_include_dirs []string
Colin Cross92430102017-10-09 14:59:32 -0700142
143 // If true, export a copy of the module as a -hostdex module for host testing.
144 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700145
Colin Crossa22116e2017-10-19 14:18:58 -0700146 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
147 // true.
148 Dex_preopt *bool
149
Colin Cross1369cdb2017-09-29 17:58:17 -0700150 // When targeting 1.9, override the modules to use with --system
151 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700152}
153
Colin Cross46c9b8b2017-06-22 16:51:17 -0700154// Module contains the properties and members used by all java module types
155type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700156 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700157 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700158
Colin Cross89536d42017-07-07 14:35:50 -0700159 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700160 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700161 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700162
Nan Zhanged19fc32017-10-19 13:06:22 -0700163 // header jar file suitable for inserting into the bootclasspath/classpath of another compile
164 headerJarFile android.Path
165
166 // full implementation jar file suitable for static dependency of another module compile
167 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700168
Colin Cross6ade34f2017-09-15 13:00:47 -0700169 // output file containing classes.dex
170 dexJarFile android.Path
171
Colin Crossb7a63242015-04-16 14:09:14 -0700172 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700173 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700174
Colin Cross635c3b02016-05-18 15:37:25 -0700175 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700176
Colin Cross635c3b02016-05-18 15:37:25 -0700177 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700178
Colin Cross59149b62017-10-16 18:07:29 -0700179 // jars containing source files that should be included in the javac command line,
Colin Crossb7a63242015-04-16 14:09:14 -0700180 // for example R.java generated by aapt for android apps
Colin Cross59149b62017-10-16 18:07:29 -0700181 ExtraSrcJars android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700182
Colin Cross2fe66872015-03-30 17:20:39 -0700183 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700184 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700185}
186
Colin Crossf506d872017-07-19 15:53:04 -0700187type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700188 HeaderJars() android.Paths
189 ImplementationJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700190 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700191}
192
Colin Cross89536d42017-07-07 14:35:50 -0700193func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
194 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
195 android.InitDefaultableModule(module)
196}
197
Colin Crossbe1da472017-07-07 15:59:46 -0700198type dependencyTag struct {
199 blueprint.BaseDependencyTag
200 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700201}
202
Colin Crossbe1da472017-07-07 15:59:46 -0700203var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700204 staticLibTag = dependencyTag{name: "staticlib"}
205 libTag = dependencyTag{name: "javalib"}
206 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700207 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700208 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross93e85952017-08-15 13:34:18 -0700209 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700210)
Colin Cross2fe66872015-03-30 17:20:39 -0700211
Colin Crossfc3674a2017-09-18 17:41:52 -0700212type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700213 useModule, useFiles, useDefaultLibs, invalidVersion bool
214
Colin Cross1369cdb2017-09-29 17:58:17 -0700215 module string
216 systemModules string
217
218 jar android.Path
219 aidl android.Path
220}
221
222func sdkStringToNumber(ctx android.BaseContext, v string) int {
223 switch v {
224 case "", "current", "system_current", "test_current":
225 return 10000
226 default:
227 if i, err := strconv.Atoi(v); err != nil {
228 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
229 return -1
230 } else {
231 return i
232 }
233 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700234}
235
236func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
Colin Cross1369cdb2017-09-29 17:58:17 -0700237 i := sdkStringToNumber(ctx, v)
238 if i == -1 {
239 // Invalid sdk version, error handled by sdkStringToNumber.
240 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700241 }
242
243 toFile := func(v string) sdkDep {
244 dir := filepath.Join("prebuilts/sdk", v)
245 jar := filepath.Join(dir, "android.jar")
246 aidl := filepath.Join(dir, "framework.aidl")
247 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
248 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700249
250 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() {
251 return sdkDep{
252 invalidVersion: true,
253 module: "sdk_v" + v,
254 }
255 }
256
Colin Crossfc3674a2017-09-18 17:41:52 -0700257 if !jarPath.Valid() {
258 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
259 return sdkDep{}
260 }
Colin Cross47ff2522017-10-02 14:22:08 -0700261
Colin Crossfc3674a2017-09-18 17:41:52 -0700262 if !aidlPath.Valid() {
263 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
264 return sdkDep{}
265 }
Colin Cross47ff2522017-10-02 14:22:08 -0700266
Colin Crossfc3674a2017-09-18 17:41:52 -0700267 return sdkDep{
268 useFiles: true,
269 jar: jarPath.Path(),
270 aidl: aidlPath.Path(),
271 }
272 }
273
Colin Cross2ebc4762017-10-20 14:00:31 -0700274 //toModule := func(m string) sdkDep {
275 // return sdkDep{
276 // useModule: true,
277 // module: m,
278 // systemModules: m + "_system_modules",
279 // }
280 //}
Colin Crossfc3674a2017-09-18 17:41:52 -0700281
Colin Cross8b9d37b2017-09-22 15:30:06 -0700282 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700283 return toFile(v)
284 }
285
286 switch v {
287 case "":
288 return sdkDep{
289 useDefaultLibs: true,
290 }
Colin Cross2ebc4762017-10-20 14:00:31 -0700291 // TODO(ccross): re-enable these once we generate stubs, until then
292 // use the stubs in prebuilts/sdk/*current
293 //case "current":
294 // return toModule("android_stubs_current")
295 //case "system_current":
296 // return toModule("android_system_stubs_current")
297 //case "test_current":
298 // return toModule("android_test_stubs_current")
Colin Crossfc3674a2017-09-18 17:41:52 -0700299 default:
300 return toFile(v)
301 }
302}
303
Colin Crossbe1da472017-07-07 15:59:46 -0700304func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700305 if ctx.Device() {
306 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossfc3674a2017-09-18 17:41:52 -0700307 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
308 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700309 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700310 if ctx.AConfig().TargetOpenJDK9() {
311 ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
312 }
Colin Crossfa5eb232017-10-01 20:33:03 -0700313 if !proptools.Bool(j.properties.No_framework_libs) {
314 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
315 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700316 } else if sdkDep.useModule {
317 if ctx.AConfig().TargetOpenJDK9() {
318 ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
319 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700320 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700321 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700322 } else if j.deviceProperties.System_modules == nil {
323 ctx.PropertyErrorf("no_standard_libs",
324 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
325 } else if *j.deviceProperties.System_modules != "none" && ctx.AConfig().TargetOpenJDK9() {
326 ctx.AddDependency(ctx.Module(), systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700327 }
328 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700329
Colin Crossf506d872017-07-19 15:53:04 -0700330 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
331 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700332 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700333
334 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700335 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross6af17aa2017-09-20 12:59:05 -0700336
337 if j.hasSrcExt(".proto") {
338 protoDeps(ctx, &j.protoProperties)
339 }
Colin Cross93e85952017-08-15 13:34:18 -0700340
341 if j.hasSrcExt(".kt") {
342 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
343 // Kotlin files
344 ctx.AddDependency(ctx.Module(), kotlinStdlibTag, "kotlin-stdlib")
345 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700346}
347
348func hasSrcExt(srcs []string, ext string) bool {
349 for _, src := range srcs {
350 if filepath.Ext(src) == ext {
351 return true
352 }
353 }
354
355 return false
356}
357
358func (j *Module) hasSrcExt(ext string) bool {
359 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700360}
361
Colin Cross46c9b8b2017-06-22 16:51:17 -0700362func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700363 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700364
Colin Cross540eff82017-06-22 17:01:52 -0700365 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700366
367 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700368 if aidlPreprocess.Valid() {
369 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700370 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700371 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700372 }
373
Colin Cross635c3b02016-05-18 15:37:25 -0700374 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
375 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
376 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crosse243c232017-10-21 15:16:37 -0700377 if src := android.ExistentPathForSource(ctx, "", ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700378 flags = append(flags, "-I"+src.String())
379 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700380
Colin Crossf03c82b2015-04-13 13:53:40 -0700381 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700382}
383
Colin Cross32f676a2017-09-06 13:41:06 -0700384type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700385 classpath android.Paths
386 bootClasspath android.Paths
387 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700388 staticHeaderJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700389 staticJarResources android.Paths
390 aidlIncludeDirs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700391 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700392 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700393 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700394 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700395}
Colin Cross2fe66872015-03-30 17:20:39 -0700396
Colin Cross32f676a2017-09-06 13:41:06 -0700397func (j *Module) collectDeps(ctx android.ModuleContext) deps {
398 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700399
400 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
Colin Cross47ff2522017-10-02 14:22:08 -0700401 if sdkDep.invalidVersion {
402 ctx.AddMissingDependencies([]string{sdkDep.module})
403 } else if sdkDep.useFiles {
Nan Zhanged19fc32017-10-19 13:06:22 -0700404 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Crossfc3674a2017-09-18 17:41:52 -0700405 deps.classpath = append(deps.classpath, sdkDep.jar)
406 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
407 }
408
Colin Crossd11fcda2017-10-23 17:59:01 -0700409 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700410 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700411 tag := ctx.OtherModuleDependencyTag(module)
412
Colin Crossf506d872017-07-19 15:53:04 -0700413 dep, _ := module.(Dependency)
414 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700415 switch tag {
416 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700417 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700418 case systemModulesTag:
419 if deps.systemModules != nil {
420 panic("Found two system module dependencies")
421 }
422 sm := module.(*SystemModules)
423 if sm.outputFile == nil {
424 panic("Missing directory for system module dependency")
425 }
426 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700427 default:
428 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700429 }
Colin Crossec7a0422017-07-07 14:47:12 -0700430 return
431 }
432
Colin Crossbe1da472017-07-07 15:59:46 -0700433 switch tag {
434 case bootClasspathTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700435 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Crossf506d872017-07-19 15:53:04 -0700436 case libTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700437 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Colin Crossf506d872017-07-19 15:53:04 -0700438 case staticLibTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700439 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
440 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
441 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700442 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700443 if ctx.ModuleName() == "framework" {
444 // framework.jar has a one-off dependency on the R.java and Manifest.java files
445 // generated by framework-res.apk
Colin Cross59149b62017-10-16 18:07:29 -0700446 // TODO(ccross): aapt java files should go in a src jar
Colin Crossec7a0422017-07-07 14:47:12 -0700447 }
Colin Cross93e85952017-08-15 13:34:18 -0700448 case kotlinStdlibTag:
Nan Zhanged19fc32017-10-19 13:06:22 -0700449 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossbe1da472017-07-07 15:59:46 -0700450 default:
451 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700452 }
Colin Crossbe1da472017-07-07 15:59:46 -0700453
Colin Cross32f676a2017-09-06 13:41:06 -0700454 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700455 })
456
Colin Cross32f676a2017-09-06 13:41:06 -0700457 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700458}
459
Nan Zhanged19fc32017-10-19 13:06:22 -0700460func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700461
Colin Crossf03c82b2015-04-13 13:53:40 -0700462 var flags javaBuilderFlags
463
Nan Zhanged19fc32017-10-19 13:06:22 -0700464 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700465 javacFlags := j.properties.Javacflags
Colin Cross1369cdb2017-09-29 17:58:17 -0700466 if ctx.AConfig().TargetOpenJDK9() {
467 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700468 }
469 if len(javacFlags) > 0 {
470 // optimization.
471 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
472 flags.javacFlags = "$javacFlags"
Colin Cross4f26bc02017-09-06 12:52:16 -0700473 }
Colin Cross64162712017-08-08 13:17:59 -0700474
Nan Zhanged19fc32017-10-19 13:06:22 -0700475 // javaVersion flag.
Colin Cross1369cdb2017-09-29 17:58:17 -0700476 sdk := sdkStringToNumber(ctx, j.deviceProperties.Sdk_version)
Colin Cross64162712017-08-08 13:17:59 -0700477 if j.properties.Java_version != nil {
478 flags.javaVersion = *j.properties.Java_version
Colin Cross1369cdb2017-09-29 17:58:17 -0700479 } else if ctx.Device() && sdk <= 23 {
480 flags.javaVersion = "1.7"
481 } else if ctx.Device() && sdk <= 26 || !ctx.AConfig().TargetOpenJDK9() {
482 flags.javaVersion = "1.8"
Colin Cross2ebc4762017-10-20 14:00:31 -0700483 } else if ctx.Device() && j.deviceProperties.Sdk_version != "" && sdk == 10000 {
484 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
485 flags.javaVersion = "1.8"
Colin Cross64162712017-08-08 13:17:59 -0700486 } else {
Colin Cross1369cdb2017-09-29 17:58:17 -0700487 flags.javaVersion = "1.9"
Colin Cross64162712017-08-08 13:17:59 -0700488 }
489
Nan Zhanged19fc32017-10-19 13:06:22 -0700490 // classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700491 flags.bootClasspath.AddPaths(deps.bootClasspath)
492 flags.classpath.AddPaths(deps.classpath)
Nan Zhanged19fc32017-10-19 13:06:22 -0700493 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700494 if deps.systemModules != nil {
495 flags.systemModules = append(flags.systemModules, deps.systemModules)
496 }
497
Nan Zhanged19fc32017-10-19 13:06:22 -0700498 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700499 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700500 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700501 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700502 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
503 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700504 }
505
Nan Zhanged19fc32017-10-19 13:06:22 -0700506 return flags
507}
Colin Crossc0b06f12015-04-08 13:03:43 -0700508
Nan Zhanged19fc32017-10-19 13:06:22 -0700509func (j *Module) compile(ctx android.ModuleContext) {
510
511 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
512
513 deps := j.collectDeps(ctx)
514 flags := j.collectBuilderFlags(ctx, deps)
515
516 if ctx.AConfig().TargetOpenJDK9() {
517 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
518 }
519 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700520 if hasSrcExt(srcFiles.Strings(), ".proto") {
521 flags = protoFlags(ctx, &j.protoProperties, flags)
522 }
523
Colin Cross8eadbf02017-10-24 17:46:00 -0700524 var srcJars android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700525 srcFiles, srcJars = j.genSources(ctx, srcFiles, flags)
Colin Cross59149b62017-10-16 18:07:29 -0700526 srcJars = append(srcJars, deps.srcJars...)
Colin Cross59149b62017-10-16 18:07:29 -0700527 srcJars = append(srcJars, j.ExtraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700528
Colin Cross0a6e0072017-08-30 14:24:55 -0700529 var jars android.Paths
530
Colin Cross1ee23172017-10-18 14:44:18 -0700531 jarName := ctx.ModuleName() + ".jar"
532
Colin Cross93e85952017-08-15 13:34:18 -0700533 if srcFiles.HasExt(".kt") {
534 // If there are kotlin files, compile them first but pass all the kotlin and java files
535 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
536 // won't emit any classes for them.
537
538 flags.kotlincFlags = "-no-stdlib"
539 if ctx.Device() {
540 flags.kotlincFlags += " -no-jdk"
541 }
542
543 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
544 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
545
Colin Cross1ee23172017-10-18 14:44:18 -0700546 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross93e85952017-08-15 13:34:18 -0700547 TransformKotlinToClasses(ctx, kotlinJar, srcFiles, srcJars, flags)
548 if ctx.Failed() {
549 return
550 }
551
552 // Make javac rule depend on the kotlinc rule
553 flags.classpath = append(flags.classpath, kotlinJar)
554 // Jar kotlin classes into the final jar after javac
555 jars = append(jars, kotlinJar)
556 jars = append(jars, deps.kotlinStdlib...)
557 }
558
Nan Zhanged19fc32017-10-19 13:06:22 -0700559 javaSrcFiles := srcFiles.FilterByExt(".java")
560 var uniqueSrcFiles android.Paths
561 set := make(map[string]bool)
562 for _, v := range javaSrcFiles {
563 if _, found := set[v.String()]; !found {
564 set[v.String()] = true
565 uniqueSrcFiles = append(uniqueSrcFiles, v)
566 }
567 }
568
569 if ctx.Device() && !ctx.AConfig().IsEnvFalse("TURBINE_ENABLED") {
570 // If sdk jar is java module, then directly return classesJar as header.jar
571 if j.Name() != "android_stubs_current" && j.Name() != "android_system_stubs_current" &&
572 j.Name() != "android_test_stubs_current" {
573 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName)
574 if ctx.Failed() {
575 return
576 }
577 }
578 }
Colin Cross8eadbf02017-10-24 17:46:00 -0700579 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700580 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700581 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
582 // If error-prone is enabled, add an additional rule to compile the java files into
583 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700584 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700585 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
586 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -0700587 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Colin Cross93e85952017-08-15 13:34:18 -0700588 RunErrorProne(ctx, errorprone, javaSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700589 extraJarDeps = append(extraJarDeps, errorprone)
590 }
591
Colin Crossd6891432017-09-27 17:39:56 -0700592 // Compile java sources into .class files
Colin Cross1ee23172017-10-18 14:44:18 -0700593 classes := android.PathForModuleOut(ctx, "javac", jarName)
Colin Cross93e85952017-08-15 13:34:18 -0700594 TransformJavaToClasses(ctx, classes, javaSrcFiles, srcJars, flags, extraJarDeps)
Colin Crossd6891432017-09-27 17:39:56 -0700595 if ctx.Failed() {
596 return
597 }
598
Colin Cross0a6e0072017-08-30 14:24:55 -0700599 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700600 }
601
Colin Cross0f37af02017-09-27 17:42:05 -0700602 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
603 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
604
605 var resArgs []string
606 var resDeps android.Paths
607
608 resArgs = append(resArgs, dirArgs...)
609 resDeps = append(resDeps, dirDeps...)
610
611 resArgs = append(resArgs, fileArgs...)
612 resDeps = append(resDeps, fileDeps...)
613
614 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700615 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700616 resArgs = append(resArgs, srcArgs...)
617 resDeps = append(resDeps, srcDeps...)
618 }
Colin Cross40a36712017-09-27 17:41:35 -0700619
620 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -0700621 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700622 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700623 if ctx.Failed() {
624 return
625 }
Colin Cross20978302015-04-10 17:05:07 -0700626
Colin Cross0a6e0072017-08-30 14:24:55 -0700627 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700628 }
629
Colin Cross6ade34f2017-09-15 13:00:47 -0700630 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700631 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700632
Colin Cross635acc92017-09-12 22:50:46 -0700633 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
634
Colin Cross0a6e0072017-08-30 14:24:55 -0700635 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -0700636 // classes.jar. If there is only one input jar this step will be skipped.
Colin Crosse9a275b2017-10-16 17:09:48 -0700637 var outputFile android.Path
638
639 if len(jars) == 1 && !manifest.Valid() {
640 // Optimization: skip the combine step if there is nothing to do
641 outputFile = jars[0]
642 } else {
Colin Cross1ee23172017-10-18 14:44:18 -0700643 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Nan Zhanged19fc32017-10-19 13:06:22 -0700644 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700645 outputFile = combinedJar
646 }
Colin Cross0a6e0072017-08-30 14:24:55 -0700647
648 if j.properties.Jarjar_rules != nil {
649 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700650 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -0700651 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -0700652 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
653 outputFile = jarjarFile
Colin Cross0a6e0072017-08-30 14:24:55 -0700654 if ctx.Failed() {
655 return
656 }
657 }
Nan Zhanged19fc32017-10-19 13:06:22 -0700658 j.implementationJarFile = outputFile
659 if j.headerJarFile == nil {
660 j.headerJarFile = j.implementationJarFile
661 }
Colin Cross2fe66872015-03-30 17:20:39 -0700662
Colin Crossc157a8d2017-10-03 17:17:07 -0700663 if ctx.Device() && j.installable() {
Nan Zhanged19fc32017-10-19 13:06:22 -0700664 outputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -0700665 if ctx.Failed() {
666 return
667 }
Colin Cross2fe66872015-03-30 17:20:39 -0700668 }
Colin Crossb7a63242015-04-16 14:09:14 -0700669 ctx.CheckbuildFile(outputFile)
670 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700671}
672
Colin Cross8eadbf02017-10-24 17:46:00 -0700673func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Nan Zhanged19fc32017-10-19 13:06:22 -0700674 deps deps, flags javaBuilderFlags, jarName string) android.Path {
675
676 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -0700677 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700678 // Compile java sources into turbine.jar.
679 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
680 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
681 if ctx.Failed() {
682 return nil
683 }
684 jars = append(jars, turbineJar)
685 }
686
687 // Combine any static header libraries into classes-header.jar. If there is only
688 // one input jar this step will be skipped.
689 var headerJar android.Path
690 jars = append(jars, deps.staticHeaderJars...)
691
Colin Cross5c6ecc12017-10-23 18:12:27 -0700692 // we cannot skip the combine step for now if there is only one jar
693 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
694 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
695 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
696 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -0700697
698 if j.properties.Jarjar_rules != nil {
699 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
700 // Transform classes.jar into classes-jarjar.jar
701 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
702 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
703 headerJar = jarjarFile
704 if ctx.Failed() {
705 return nil
706 }
707 }
708
709 return headerJar
710}
711
712func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
713 classesJar android.Path, jarName string) android.Path {
714
715 dxFlags := j.deviceProperties.Dxflags
716 if false /* emma enabled */ {
717 // If you instrument class files that have local variable debug information in
718 // them emma does not correctly maintain the local variable table.
719 // This will cause an error when you try to convert the class files for Android.
720 // The workaround here is to build different dex file here based on emma switch
721 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
722 // option to remove local variable information
723 dxFlags = append(dxFlags, "--no-locals")
724 }
725
726 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
727 dxFlags = append(dxFlags, "--no-optimize")
728 }
729
730 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
731 dxFlags = append(dxFlags,
732 "--debug",
733 "--verbose",
734 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
735 "--dump-width=1000")
736 }
737
738 var minSdkVersion string
739 switch j.deviceProperties.Sdk_version {
740 case "", "current", "test_current", "system_current":
741 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
742 default:
743 minSdkVersion = j.deviceProperties.Sdk_version
744 }
745
746 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
747
748 flags.dxFlags = strings.Join(dxFlags, " ")
749
750 desugarFlags := []string{
751 "--min_sdk_version " + minSdkVersion,
752 "--desugar_try_with_resources_if_needed=false",
753 "--allow_empty_bootclasspath",
754 }
755
756 if inList("--core-library", dxFlags) {
757 desugarFlags = append(desugarFlags, "--core_library")
758 }
759
760 flags.desugarFlags = strings.Join(desugarFlags, " ")
761
762 desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
763 TransformDesugar(ctx, desugarJar, classesJar, flags)
764 if ctx.Failed() {
765 return nil
766 }
767
768 // Compile classes.jar into classes.dex and then javalib.jar
769 javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
770 TransformClassesJarToDexJar(ctx, javalibJar, desugarJar, flags)
771 if ctx.Failed() {
772 return nil
773 }
774
775 j.dexJarFile = javalibJar
776 return javalibJar
777}
778
Colin Cross59f1bb62017-09-27 17:59:10 -0700779func (j *Module) installable() bool {
780 return j.properties.Installable == nil || *j.properties.Installable
781}
782
Colin Crossf506d872017-07-19 15:53:04 -0700783var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700784
Nan Zhanged19fc32017-10-19 13:06:22 -0700785func (j *Module) HeaderJars() android.Paths {
786 return android.Paths{j.headerJarFile}
787}
788
789func (j *Module) ImplementationJars() android.Paths {
790 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700791}
792
Colin Cross46c9b8b2017-06-22 16:51:17 -0700793func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700794 return j.exportAidlIncludeDirs
795}
796
Colin Cross46c9b8b2017-06-22 16:51:17 -0700797var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700798
Colin Cross46c9b8b2017-06-22 16:51:17 -0700799func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700800 return j.logtagsSrcs
801}
802
Colin Cross2fe66872015-03-30 17:20:39 -0700803//
804// Java libraries (.jar file)
805//
806
Colin Crossf506d872017-07-19 15:53:04 -0700807type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700808 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700809}
810
Colin Crossf506d872017-07-19 15:53:04 -0700811func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700812 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700813
Colin Cross59f1bb62017-09-27 17:59:10 -0700814 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700815 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
816 ctx.ModuleName()+".jar", j.outputFile)
817 }
Colin Crossb7a63242015-04-16 14:09:14 -0700818}
819
Colin Crossf506d872017-07-19 15:53:04 -0700820func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700821 j.deps(ctx)
822}
823
Colin Crossa60ead82017-10-02 18:10:21 -0700824func LibraryFactory(installable bool) func() android.Module {
825 return func() android.Module {
826 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700827
Colin Crossa60ead82017-10-02 18:10:21 -0700828 if !installable {
829 module.properties.Installable = proptools.BoolPtr(false)
830 }
Colin Cross2fe66872015-03-30 17:20:39 -0700831
Colin Crossa60ead82017-10-02 18:10:21 -0700832 module.AddProperties(
833 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700834 &module.Module.deviceProperties,
835 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700836
Colin Crossa60ead82017-10-02 18:10:21 -0700837 InitJavaModule(module, android.HostAndDeviceSupported)
838 return module
839 }
Colin Cross2fe66872015-03-30 17:20:39 -0700840}
841
Colin Crossf506d872017-07-19 15:53:04 -0700842func LibraryHostFactory() android.Module {
843 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700844
Colin Cross6af17aa2017-09-20 12:59:05 -0700845 module.AddProperties(
846 &module.Module.properties,
847 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700848
Colin Cross89536d42017-07-07 14:35:50 -0700849 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700850 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700851}
852
853//
854// Java Binaries (.jar file plus wrapper script)
855//
856
Colin Crossf506d872017-07-19 15:53:04 -0700857type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700858 // installable script to execute the resulting jar
859 Wrapper string
860}
861
Colin Crossf506d872017-07-19 15:53:04 -0700862type Binary struct {
863 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700864
Colin Crossf506d872017-07-19 15:53:04 -0700865 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700866
867 wrapperFile android.ModuleSrcPath
868 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700869}
870
Colin Crossf506d872017-07-19 15:53:04 -0700871func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
872 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700873
874 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
875 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700876 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700877 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
878 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700879}
880
Colin Crossf506d872017-07-19 15:53:04 -0700881func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700882 j.deps(ctx)
883}
884
Colin Crossf506d872017-07-19 15:53:04 -0700885func BinaryFactory() android.Module {
886 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700887
Colin Cross36242852017-06-23 15:06:31 -0700888 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700889 &module.Module.properties,
890 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700891 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700892 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700893
Colin Cross89536d42017-07-07 14:35:50 -0700894 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700895 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700896}
897
Colin Crossf506d872017-07-19 15:53:04 -0700898func BinaryHostFactory() android.Module {
899 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700900
Colin Cross36242852017-06-23 15:06:31 -0700901 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700902 &module.Module.properties,
903 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700904 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700905 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700906
Colin Cross89536d42017-07-07 14:35:50 -0700907 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700908 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700909}
910
911//
912// Java prebuilts
913//
914
Colin Cross74d73e22017-08-02 11:05:49 -0700915type ImportProperties struct {
916 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -0700917
918 Sdk_version string
Colin Cross535e2cf2017-10-20 17:57:49 -0700919
920 Installable *bool
Colin Cross74d73e22017-08-02 11:05:49 -0700921}
922
923type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700924 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700925 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700926
Colin Cross74d73e22017-08-02 11:05:49 -0700927 properties ImportProperties
928
Colin Cross0a6e0072017-08-30 14:24:55 -0700929 classpathFiles android.Paths
930 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700931}
932
Colin Cross74d73e22017-08-02 11:05:49 -0700933func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700934 return &j.prebuilt
935}
936
Colin Cross74d73e22017-08-02 11:05:49 -0700937func (j *Import) PrebuiltSrcs() []string {
938 return j.properties.Jars
939}
940
941func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700942 return j.prebuilt.Name(j.ModuleBase.Name())
943}
944
Colin Cross74d73e22017-08-02 11:05:49 -0700945func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700946}
947
Colin Cross74d73e22017-08-02 11:05:49 -0700948func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
949 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700950
Colin Crosse9a275b2017-10-16 17:09:48 -0700951 outputFile := android.PathForModuleOut(ctx, "classes.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700952 TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -0700953 j.combinedClasspathFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700954}
955
Colin Cross74d73e22017-08-02 11:05:49 -0700956var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700957
Nan Zhanged19fc32017-10-19 13:06:22 -0700958func (j *Import) HeaderJars() android.Paths {
959 return j.classpathFiles
960}
961
962func (j *Import) ImplementationJars() android.Paths {
Colin Cross74d73e22017-08-02 11:05:49 -0700963 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700964}
965
Colin Cross74d73e22017-08-02 11:05:49 -0700966func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700967 return nil
968}
969
Colin Cross74d73e22017-08-02 11:05:49 -0700970var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700971
Colin Cross74d73e22017-08-02 11:05:49 -0700972func ImportFactory() android.Module {
973 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700974
Colin Cross74d73e22017-08-02 11:05:49 -0700975 module.AddProperties(&module.properties)
976
977 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700978 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
979 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700980}
981
Colin Cross74d73e22017-08-02 11:05:49 -0700982func ImportFactoryHost() android.Module {
983 module := &Import{}
984
985 module.AddProperties(&module.properties)
986
987 android.InitPrebuiltModule(module, &module.properties.Jars)
988 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
989 return module
990}
991
Colin Cross2fe66872015-03-30 17:20:39 -0700992func inList(s string, l []string) bool {
993 for _, e := range l {
994 if e == s {
995 return true
996 }
997 }
998 return false
999}
Colin Cross89536d42017-07-07 14:35:50 -07001000
1001//
1002// Defaults
1003//
1004type Defaults struct {
1005 android.ModuleBase
1006 android.DefaultsModuleBase
1007}
1008
1009func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1010}
1011
1012func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1013}
1014
1015func defaultsFactory() android.Module {
1016 return DefaultsFactory()
1017}
1018
1019func DefaultsFactory(props ...interface{}) android.Module {
1020 module := &Defaults{}
1021
1022 module.AddProperties(props...)
1023 module.AddProperties(
1024 &CompilerProperties{},
1025 &CompilerDeviceProperties{},
1026 )
1027
1028 android.InitDefaultsModule(module)
1029
1030 return module
1031}