blob: 5393b065f488ffecc4dca089ce236d3f7ae261f6 [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
103 Jarjar_rules *string
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 Cross540eff82017-06-22 17:01:52 -0700119}
120
Colin Cross89536d42017-07-07 14:35:50 -0700121type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700122 // list of module-specific flags that will be used for dex compiles
123 Dxflags []string `android:"arch_variant"`
124
Colin Cross7d5136f2015-05-11 13:39:40 -0700125 // if not blank, set to the version of the sdk to compile against
126 Sdk_version string
127
Colin Cross7d5136f2015-05-11 13:39:40 -0700128 // directories to pass to aidl tool
129 Aidl_includes []string
130
131 // directories that should be added as include directories
132 // for any aidl sources of modules that depend on this module
133 Export_aidl_include_dirs []string
Colin Cross92430102017-10-09 14:59:32 -0700134
135 // If true, export a copy of the module as a -hostdex module for host testing.
136 Hostdex *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700137}
138
Colin Cross46c9b8b2017-06-22 16:51:17 -0700139// Module contains the properties and members used by all java module types
140type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700141 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700142 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700143
Colin Cross89536d42017-07-07 14:35:50 -0700144 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700145 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700146 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700147
148 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700149 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700150
Colin Cross6ade34f2017-09-15 13:00:47 -0700151 // output file containing classes.dex
152 dexJarFile android.Path
153
Colin Crossb7a63242015-04-16 14:09:14 -0700154 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700155 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700156
Colin Cross635c3b02016-05-18 15:37:25 -0700157 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700158
Colin Cross635c3b02016-05-18 15:37:25 -0700159 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700160
Colin Crossb7a63242015-04-16 14:09:14 -0700161 // filelists of extra source files that should be included in the javac command line,
162 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700163 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700164
Colin Cross2fe66872015-03-30 17:20:39 -0700165 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700166 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700167}
168
Colin Crossf506d872017-07-19 15:53:04 -0700169type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700170 ClasspathFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700171 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700172}
173
Colin Cross89536d42017-07-07 14:35:50 -0700174func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
175 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
176 android.InitDefaultableModule(module)
177}
178
Colin Crossbe1da472017-07-07 15:59:46 -0700179type dependencyTag struct {
180 blueprint.BaseDependencyTag
181 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700182}
183
Colin Crossbe1da472017-07-07 15:59:46 -0700184var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700185 staticLibTag = dependencyTag{name: "staticlib"}
186 libTag = dependencyTag{name: "javalib"}
187 bootClasspathTag = dependencyTag{name: "bootclasspath"}
188 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700189)
Colin Cross2fe66872015-03-30 17:20:39 -0700190
Colin Crossfc3674a2017-09-18 17:41:52 -0700191type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700192 useModule, useFiles, useDefaultLibs, invalidVersion bool
193
194 module string
195 jar android.Path
196 aidl android.Path
Colin Crossfc3674a2017-09-18 17:41:52 -0700197}
198
199func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
200 switch v {
201 case "", "current", "system_current", "test_current":
202 // OK
203 default:
204 if _, err := strconv.Atoi(v); err != nil {
205 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
206 return sdkDep{}
207 }
208 }
209
210 toFile := func(v string) sdkDep {
211 dir := filepath.Join("prebuilts/sdk", v)
212 jar := filepath.Join(dir, "android.jar")
213 aidl := filepath.Join(dir, "framework.aidl")
214 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
215 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700216
217 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() {
218 return sdkDep{
219 invalidVersion: true,
220 module: "sdk_v" + v,
221 }
222 }
223
Colin Crossfc3674a2017-09-18 17:41:52 -0700224 if !jarPath.Valid() {
225 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
226 return sdkDep{}
227 }
Colin Cross47ff2522017-10-02 14:22:08 -0700228
Colin Crossfc3674a2017-09-18 17:41:52 -0700229 if !aidlPath.Valid() {
230 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
231 return sdkDep{}
232 }
Colin Cross47ff2522017-10-02 14:22:08 -0700233
Colin Crossfc3674a2017-09-18 17:41:52 -0700234 return sdkDep{
235 useFiles: true,
236 jar: jarPath.Path(),
237 aidl: aidlPath.Path(),
238 }
239 }
240
241 toModule := func(m string) sdkDep {
242 return sdkDep{
243 useModule: true,
244 module: m,
245 }
246 }
247
Colin Cross8b9d37b2017-09-22 15:30:06 -0700248 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700249 return toFile(v)
250 }
251
252 switch v {
253 case "":
254 return sdkDep{
255 useDefaultLibs: true,
256 }
257 case "current":
258 return toModule("android_stubs_current")
259 case "system_current":
260 return toModule("android_system_stubs_current")
261 case "test_current":
262 return toModule("android_test_stubs_current")
263 default:
264 return toFile(v)
265 }
266}
267
Colin Crossbe1da472017-07-07 15:59:46 -0700268func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700269 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700270 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700271 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
272 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700273 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700274 if !proptools.Bool(j.properties.No_framework_libs) {
275 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
276 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700277 }
278 if sdkDep.useModule {
279 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700280 }
Colin Cross2fe66872015-03-30 17:20:39 -0700281 }
282 }
Colin Crossf506d872017-07-19 15:53:04 -0700283 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
284 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700285 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700286
287 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700288 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross6af17aa2017-09-20 12:59:05 -0700289
290 if j.hasSrcExt(".proto") {
291 protoDeps(ctx, &j.protoProperties)
292 }
293}
294
295func hasSrcExt(srcs []string, ext string) bool {
296 for _, src := range srcs {
297 if filepath.Ext(src) == ext {
298 return true
299 }
300 }
301
302 return false
303}
304
305func (j *Module) hasSrcExt(ext string) bool {
306 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700307}
308
Colin Cross46c9b8b2017-06-22 16:51:17 -0700309func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700310 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700311
Colin Cross540eff82017-06-22 17:01:52 -0700312 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700313
314 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700315 if aidlPreprocess.Valid() {
316 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700317 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700318 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700319 }
320
Colin Cross635c3b02016-05-18 15:37:25 -0700321 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
322 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
323 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700324 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
325 flags = append(flags, "-I"+src.String())
326 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700327
Colin Crossf03c82b2015-04-13 13:53:40 -0700328 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700329}
330
Colin Cross32f676a2017-09-06 13:41:06 -0700331type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700332 classpath android.Paths
333 bootClasspath android.Paths
334 staticJars android.Paths
335 staticJarResources android.Paths
336 aidlIncludeDirs android.Paths
337 srcFileLists android.Paths
338 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700339}
Colin Cross2fe66872015-03-30 17:20:39 -0700340
Colin Cross32f676a2017-09-06 13:41:06 -0700341func (j *Module) collectDeps(ctx android.ModuleContext) deps {
342 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700343
344 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
Colin Cross47ff2522017-10-02 14:22:08 -0700345 if sdkDep.invalidVersion {
346 ctx.AddMissingDependencies([]string{sdkDep.module})
347 } else if sdkDep.useFiles {
Colin Crossfc3674a2017-09-18 17:41:52 -0700348 deps.classpath = append(deps.classpath, sdkDep.jar)
349 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
350 }
351
Colin Cross2fe66872015-03-30 17:20:39 -0700352 ctx.VisitDirectDeps(func(module blueprint.Module) {
353 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700354 tag := ctx.OtherModuleDependencyTag(module)
355
Colin Crossf506d872017-07-19 15:53:04 -0700356 dep, _ := module.(Dependency)
357 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700358 switch tag {
359 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700360 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700361 default:
362 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700363 }
Colin Crossec7a0422017-07-07 14:47:12 -0700364 return
365 }
366
Colin Crossbe1da472017-07-07 15:59:46 -0700367 switch tag {
368 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700369 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700370 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700371 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700372 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700373 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
374 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700375 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700376 if ctx.ModuleName() == "framework" {
377 // framework.jar has a one-off dependency on the R.java and Manifest.java files
378 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700379 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700380 }
Colin Crossbe1da472017-07-07 15:59:46 -0700381 default:
382 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700383 }
Colin Crossbe1da472017-07-07 15:59:46 -0700384
Colin Cross32f676a2017-09-06 13:41:06 -0700385 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700386 })
387
Colin Cross32f676a2017-09-06 13:41:06 -0700388 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700389}
390
Colin Cross46c9b8b2017-06-22 16:51:17 -0700391func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700392
Colin Cross540eff82017-06-22 17:01:52 -0700393 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700394
Colin Cross32f676a2017-09-06 13:41:06 -0700395 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700396
Colin Crossf03c82b2015-04-13 13:53:40 -0700397 var flags javaBuilderFlags
398
399 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700400 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
401 javacFlags = config.StripJavac9Flags(javacFlags)
402 }
Colin Cross64162712017-08-08 13:17:59 -0700403
404 if j.properties.Java_version != nil {
405 flags.javaVersion = *j.properties.Java_version
406 } else {
407 flags.javaVersion = "${config.DefaultJavaVersion}"
408 }
409
Colin Cross6ade34f2017-09-15 13:00:47 -0700410 flags.bootClasspath.AddPaths(deps.bootClasspath)
411 flags.classpath.AddPaths(deps.classpath)
412
Colin Crossf03c82b2015-04-13 13:53:40 -0700413 if len(javacFlags) > 0 {
414 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
415 flags.javacFlags = "$javacFlags"
416 }
417
Colin Cross32f676a2017-09-06 13:41:06 -0700418 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700419 if len(aidlFlags) > 0 {
420 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
421 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700422 }
423
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700424 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700425
Colin Cross6af17aa2017-09-20 12:59:05 -0700426 if hasSrcExt(srcFiles.Strings(), ".proto") {
427 flags = protoFlags(ctx, &j.protoProperties, flags)
428 }
429
430 var srcFileLists android.Paths
431
432 srcFiles, srcFileLists = j.genSources(ctx, srcFiles, flags)
433
434 srcFileLists = append(srcFileLists, deps.srcFileLists...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700435
Colin Cross6af17aa2017-09-20 12:59:05 -0700436 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700437
Colin Cross0a6e0072017-08-30 14:24:55 -0700438 var jars android.Paths
439
Colin Cross8cf13342015-04-10 15:41:49 -0700440 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700441 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700442 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
443 // If error-prone is enabled, add an additional rule to compile the java files into
444 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700445 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700446 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
447 // enable error-prone without affecting the output class files.
Colin Cross6af17aa2017-09-20 12:59:05 -0700448 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700449 extraJarDeps = append(extraJarDeps, errorprone)
450 }
451
Colin Crossd6891432017-09-27 17:39:56 -0700452 // Compile java sources into .class files
Colin Cross6af17aa2017-09-20 12:59:05 -0700453 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, extraJarDeps)
Colin Crossd6891432017-09-27 17:39:56 -0700454 if ctx.Failed() {
455 return
456 }
457
Colin Cross0a6e0072017-08-30 14:24:55 -0700458 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700459 }
460
Colin Cross0f37af02017-09-27 17:42:05 -0700461 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
462 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
463
464 var resArgs []string
465 var resDeps android.Paths
466
467 resArgs = append(resArgs, dirArgs...)
468 resDeps = append(resDeps, dirDeps...)
469
470 resArgs = append(resArgs, fileArgs...)
471 resDeps = append(resDeps, fileDeps...)
472
473 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700474 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700475 resArgs = append(resArgs, srcArgs...)
476 resDeps = append(resDeps, srcDeps...)
477 }
Colin Cross40a36712017-09-27 17:41:35 -0700478
479 if len(resArgs) > 0 {
Colin Cross40a36712017-09-27 17:41:35 -0700480 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700481 if ctx.Failed() {
482 return
483 }
Colin Cross20978302015-04-10 17:05:07 -0700484
Colin Cross0a6e0072017-08-30 14:24:55 -0700485 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700486 }
487
Colin Cross6ade34f2017-09-15 13:00:47 -0700488 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700489 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700490
Colin Cross635acc92017-09-12 22:50:46 -0700491 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
492
Colin Cross0a6e0072017-08-30 14:24:55 -0700493 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700494 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700495 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700496
497 if j.properties.Jarjar_rules != nil {
498 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700499 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700500 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
501 if ctx.Failed() {
502 return
503 }
504 }
505
Colin Cross2fe66872015-03-30 17:20:39 -0700506 j.classpathFile = outputFile
507
Colin Crossc157a8d2017-10-03 17:17:07 -0700508 if ctx.Device() && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700509 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700510 if false /* emma enabled */ {
511 // If you instrument class files that have local variable debug information in
512 // them emma does not correctly maintain the local variable table.
513 // This will cause an error when you try to convert the class files for Android.
514 // The workaround here is to build different dex file here based on emma switch
515 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
516 // option to remove local variable information
517 dxFlags = append(dxFlags, "--no-locals")
518 }
519
Colin Cross1332b002015-04-07 17:11:30 -0700520 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700521 dxFlags = append(dxFlags, "--no-optimize")
522 }
523
Colin Cross1332b002015-04-07 17:11:30 -0700524 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700525 dxFlags = append(dxFlags,
526 "--debug",
527 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700528 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700529 "--dump-width=1000")
530 }
531
Colin Cross595a4062017-08-31 12:30:37 -0700532 var minSdkVersion string
533 switch j.deviceProperties.Sdk_version {
534 case "", "current", "test_current", "system_current":
535 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
536 default:
537 minSdkVersion = j.deviceProperties.Sdk_version
538 }
539
540 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
541
Colin Cross2fe66872015-03-30 17:20:39 -0700542 flags.dxFlags = strings.Join(dxFlags, " ")
543
Colin Cross6ade34f2017-09-15 13:00:47 -0700544 desugarFlags := []string{
545 "--min_sdk_version " + minSdkVersion,
546 "--desugar_try_with_resources_if_needed=false",
547 "--allow_empty_bootclasspath",
548 }
549
550 if inList("--core-library", dxFlags) {
551 desugarFlags = append(desugarFlags, "--core_library")
552 }
553
554 flags.desugarFlags = strings.Join(desugarFlags, " ")
555
556 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700557 if ctx.Failed() {
558 return
559 }
560
Colin Cross7db5d632017-10-04 17:07:09 -0700561 // Compile classes.jar into classes.dex and then javalib.jar
562 outputFile = TransformClassesJarToDexJar(ctx, "javalib.jar", desugarJar, flags)
Colin Cross6ade34f2017-09-15 13:00:47 -0700563 if ctx.Failed() {
564 return
565 }
566
Colin Cross6ade34f2017-09-15 13:00:47 -0700567 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700568 }
Colin Crossb7a63242015-04-16 14:09:14 -0700569 ctx.CheckbuildFile(outputFile)
570 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700571}
572
Colin Cross59f1bb62017-09-27 17:59:10 -0700573func (j *Module) installable() bool {
574 return j.properties.Installable == nil || *j.properties.Installable
575}
576
Colin Crossf506d872017-07-19 15:53:04 -0700577var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700578
Colin Cross74d73e22017-08-02 11:05:49 -0700579func (j *Module) ClasspathFiles() android.Paths {
580 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700581}
582
Colin Cross46c9b8b2017-06-22 16:51:17 -0700583func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700584 return j.exportAidlIncludeDirs
585}
586
Colin Cross46c9b8b2017-06-22 16:51:17 -0700587var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700588
Colin Cross46c9b8b2017-06-22 16:51:17 -0700589func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700590 return j.logtagsSrcs
591}
592
Colin Cross2fe66872015-03-30 17:20:39 -0700593//
594// Java libraries (.jar file)
595//
596
Colin Crossf506d872017-07-19 15:53:04 -0700597type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700598 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700599}
600
Colin Crossf506d872017-07-19 15:53:04 -0700601func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700602 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700603
Colin Cross59f1bb62017-09-27 17:59:10 -0700604 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700605 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
606 ctx.ModuleName()+".jar", j.outputFile)
607 }
Colin Crossb7a63242015-04-16 14:09:14 -0700608}
609
Colin Crossf506d872017-07-19 15:53:04 -0700610func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700611 j.deps(ctx)
612}
613
Colin Crossa60ead82017-10-02 18:10:21 -0700614func LibraryFactory(installable bool) func() android.Module {
615 return func() android.Module {
616 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700617
Colin Crossa60ead82017-10-02 18:10:21 -0700618 if !installable {
619 module.properties.Installable = proptools.BoolPtr(false)
620 }
Colin Cross2fe66872015-03-30 17:20:39 -0700621
Colin Crossa60ead82017-10-02 18:10:21 -0700622 module.AddProperties(
623 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700624 &module.Module.deviceProperties,
625 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700626
Colin Crossa60ead82017-10-02 18:10:21 -0700627 InitJavaModule(module, android.HostAndDeviceSupported)
628 return module
629 }
Colin Cross2fe66872015-03-30 17:20:39 -0700630}
631
Colin Crossf506d872017-07-19 15:53:04 -0700632func LibraryHostFactory() android.Module {
633 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700634
Colin Cross6af17aa2017-09-20 12:59:05 -0700635 module.AddProperties(
636 &module.Module.properties,
637 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700638
Colin Cross89536d42017-07-07 14:35:50 -0700639 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700640 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700641}
642
643//
644// Java Binaries (.jar file plus wrapper script)
645//
646
Colin Crossf506d872017-07-19 15:53:04 -0700647type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700648 // installable script to execute the resulting jar
649 Wrapper string
650}
651
Colin Crossf506d872017-07-19 15:53:04 -0700652type Binary struct {
653 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700654
Colin Crossf506d872017-07-19 15:53:04 -0700655 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700656
657 wrapperFile android.ModuleSrcPath
658 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700659}
660
Colin Crossf506d872017-07-19 15:53:04 -0700661func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
662 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700663
664 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
665 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700666 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700667 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
668 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700669}
670
Colin Crossf506d872017-07-19 15:53:04 -0700671func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700672 j.deps(ctx)
673}
674
Colin Crossf506d872017-07-19 15:53:04 -0700675func BinaryFactory() android.Module {
676 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700677
Colin Cross36242852017-06-23 15:06:31 -0700678 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700679 &module.Module.properties,
680 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700681 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700682 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700683
Colin Cross89536d42017-07-07 14:35:50 -0700684 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700685 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700686}
687
Colin Crossf506d872017-07-19 15:53:04 -0700688func BinaryHostFactory() android.Module {
689 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700690
Colin Cross36242852017-06-23 15:06:31 -0700691 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700692 &module.Module.properties,
693 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700694 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700695 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700696
Colin Cross89536d42017-07-07 14:35:50 -0700697 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700698 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700699}
700
701//
702// Java prebuilts
703//
704
Colin Cross74d73e22017-08-02 11:05:49 -0700705type ImportProperties struct {
706 Jars []string
707}
708
709type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700710 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700711 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700712
Colin Cross74d73e22017-08-02 11:05:49 -0700713 properties ImportProperties
714
Colin Cross0a6e0072017-08-30 14:24:55 -0700715 classpathFiles android.Paths
716 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700717}
718
Colin Cross74d73e22017-08-02 11:05:49 -0700719func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700720 return &j.prebuilt
721}
722
Colin Cross74d73e22017-08-02 11:05:49 -0700723func (j *Import) PrebuiltSrcs() []string {
724 return j.properties.Jars
725}
726
727func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700728 return j.prebuilt.Name(j.ModuleBase.Name())
729}
730
Colin Cross74d73e22017-08-02 11:05:49 -0700731func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700732}
733
Colin Cross74d73e22017-08-02 11:05:49 -0700734func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
735 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700736
Colin Cross635acc92017-09-12 22:50:46 -0700737 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700738}
739
Colin Cross74d73e22017-08-02 11:05:49 -0700740var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700741
Colin Cross74d73e22017-08-02 11:05:49 -0700742func (j *Import) ClasspathFiles() android.Paths {
743 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700744}
745
Colin Cross74d73e22017-08-02 11:05:49 -0700746func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700747 return nil
748}
749
Colin Cross74d73e22017-08-02 11:05:49 -0700750var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700751
Colin Cross74d73e22017-08-02 11:05:49 -0700752func ImportFactory() android.Module {
753 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700754
Colin Cross74d73e22017-08-02 11:05:49 -0700755 module.AddProperties(&module.properties)
756
757 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700758 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
759 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700760}
761
Colin Cross74d73e22017-08-02 11:05:49 -0700762func ImportFactoryHost() android.Module {
763 module := &Import{}
764
765 module.AddProperties(&module.properties)
766
767 android.InitPrebuiltModule(module, &module.properties.Jars)
768 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
769 return module
770}
771
Colin Cross2fe66872015-03-30 17:20:39 -0700772func inList(s string, l []string) bool {
773 for _, e := range l {
774 if e == s {
775 return true
776 }
777 }
778 return false
779}
Colin Cross89536d42017-07-07 14:35:50 -0700780
781//
782// Defaults
783//
784type Defaults struct {
785 android.ModuleBase
786 android.DefaultsModuleBase
787}
788
789func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
790}
791
792func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
793}
794
795func defaultsFactory() android.Module {
796 return DefaultsFactory()
797}
798
799func DefaultsFactory(props ...interface{}) android.Module {
800 module := &Defaults{}
801
802 module.AddProperties(props...)
803 module.AddProperties(
804 &CompilerProperties{},
805 &CompilerDeviceProperties{},
806 )
807
808 android.InitDefaultsModule(module)
809
810 return module
811}