blob: fde88e90f91707bd8b002adc965ba4842eaf553b [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
128 // Set for device java libraries, and for host versions of device java libraries
129 // built for testing
130 Dex bool `blueprint:"mutated"`
131
Colin Cross7d5136f2015-05-11 13:39:40 -0700132 // directories to pass to aidl tool
133 Aidl_includes []string
134
135 // directories that should be added as include directories
136 // for any aidl sources of modules that depend on this module
137 Export_aidl_include_dirs []string
138}
139
Colin Cross46c9b8b2017-06-22 16:51:17 -0700140// Module contains the properties and members used by all java module types
141type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700142 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700143 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700144
Colin Cross89536d42017-07-07 14:35:50 -0700145 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700146 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700147 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700148
149 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700150 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700151
Colin Cross6ade34f2017-09-15 13:00:47 -0700152 // output file containing classes.dex
153 dexJarFile android.Path
154
Colin Crossb7a63242015-04-16 14:09:14 -0700155 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700156 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700157
Colin Cross635c3b02016-05-18 15:37:25 -0700158 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700159
Colin Cross635c3b02016-05-18 15:37:25 -0700160 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700161
Colin Crossb7a63242015-04-16 14:09:14 -0700162 // filelists of extra source files that should be included in the javac command line,
163 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700164 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700165
Colin Cross2fe66872015-03-30 17:20:39 -0700166 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700167 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700168}
169
Colin Crossf506d872017-07-19 15:53:04 -0700170type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700171 ClasspathFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700172 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700173}
174
Colin Cross89536d42017-07-07 14:35:50 -0700175func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
176 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
177 android.InitDefaultableModule(module)
178}
179
Colin Crossbe1da472017-07-07 15:59:46 -0700180type dependencyTag struct {
181 blueprint.BaseDependencyTag
182 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700183}
184
Colin Crossbe1da472017-07-07 15:59:46 -0700185var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700186 staticLibTag = dependencyTag{name: "staticlib"}
187 libTag = dependencyTag{name: "javalib"}
188 bootClasspathTag = dependencyTag{name: "bootclasspath"}
189 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700190)
Colin Cross2fe66872015-03-30 17:20:39 -0700191
Colin Crossfc3674a2017-09-18 17:41:52 -0700192type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700193 useModule, useFiles, useDefaultLibs, invalidVersion bool
194
195 module string
196 jar android.Path
197 aidl android.Path
Colin Crossfc3674a2017-09-18 17:41:52 -0700198}
199
200func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
201 switch v {
202 case "", "current", "system_current", "test_current":
203 // OK
204 default:
205 if _, err := strconv.Atoi(v); err != nil {
206 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
207 return sdkDep{}
208 }
209 }
210
211 toFile := func(v string) sdkDep {
212 dir := filepath.Join("prebuilts/sdk", v)
213 jar := filepath.Join(dir, "android.jar")
214 aidl := filepath.Join(dir, "framework.aidl")
215 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
216 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700217
218 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() {
219 return sdkDep{
220 invalidVersion: true,
221 module: "sdk_v" + v,
222 }
223 }
224
Colin Crossfc3674a2017-09-18 17:41:52 -0700225 if !jarPath.Valid() {
226 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
227 return sdkDep{}
228 }
Colin Cross47ff2522017-10-02 14:22:08 -0700229
Colin Crossfc3674a2017-09-18 17:41:52 -0700230 if !aidlPath.Valid() {
231 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
232 return sdkDep{}
233 }
Colin Cross47ff2522017-10-02 14:22:08 -0700234
Colin Crossfc3674a2017-09-18 17:41:52 -0700235 return sdkDep{
236 useFiles: true,
237 jar: jarPath.Path(),
238 aidl: aidlPath.Path(),
239 }
240 }
241
242 toModule := func(m string) sdkDep {
243 return sdkDep{
244 useModule: true,
245 module: m,
246 }
247 }
248
Colin Cross8b9d37b2017-09-22 15:30:06 -0700249 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700250 return toFile(v)
251 }
252
253 switch v {
254 case "":
255 return sdkDep{
256 useDefaultLibs: true,
257 }
258 case "current":
259 return toModule("android_stubs_current")
260 case "system_current":
261 return toModule("android_system_stubs_current")
262 case "test_current":
263 return toModule("android_test_stubs_current")
264 default:
265 return toFile(v)
266 }
267}
268
Colin Crossbe1da472017-07-07 15:59:46 -0700269func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700270 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700271 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700272 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
273 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700274 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700275 if !proptools.Bool(j.properties.No_framework_libs) {
276 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
277 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700278 }
279 if sdkDep.useModule {
280 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700281 }
282 } else {
Colin Cross965714f2017-10-03 20:57:01 -0700283 // TODO(ccross): add hostdex support
Colin Cross2fe66872015-03-30 17:20:39 -0700284 }
285 }
Colin Crossf506d872017-07-19 15:53:04 -0700286 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
287 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700288 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700289
290 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700291 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross6af17aa2017-09-20 12:59:05 -0700292
293 if j.hasSrcExt(".proto") {
294 protoDeps(ctx, &j.protoProperties)
295 }
296}
297
298func hasSrcExt(srcs []string, ext string) bool {
299 for _, src := range srcs {
300 if filepath.Ext(src) == ext {
301 return true
302 }
303 }
304
305 return false
306}
307
308func (j *Module) hasSrcExt(ext string) bool {
309 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700310}
311
Colin Cross46c9b8b2017-06-22 16:51:17 -0700312func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700313 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700314
Colin Cross540eff82017-06-22 17:01:52 -0700315 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700316
317 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700318 if aidlPreprocess.Valid() {
319 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700320 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700321 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700322 }
323
Colin Cross635c3b02016-05-18 15:37:25 -0700324 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
325 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
326 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700327 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
328 flags = append(flags, "-I"+src.String())
329 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700330
Colin Crossf03c82b2015-04-13 13:53:40 -0700331 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700332}
333
Colin Cross32f676a2017-09-06 13:41:06 -0700334type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700335 classpath android.Paths
336 bootClasspath android.Paths
337 staticJars android.Paths
338 staticJarResources android.Paths
339 aidlIncludeDirs android.Paths
340 srcFileLists android.Paths
341 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700342}
Colin Cross2fe66872015-03-30 17:20:39 -0700343
Colin Cross32f676a2017-09-06 13:41:06 -0700344func (j *Module) collectDeps(ctx android.ModuleContext) deps {
345 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700346
347 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
Colin Cross47ff2522017-10-02 14:22:08 -0700348 if sdkDep.invalidVersion {
349 ctx.AddMissingDependencies([]string{sdkDep.module})
350 } else if sdkDep.useFiles {
Colin Crossfc3674a2017-09-18 17:41:52 -0700351 deps.classpath = append(deps.classpath, sdkDep.jar)
352 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
353 }
354
Colin Cross2fe66872015-03-30 17:20:39 -0700355 ctx.VisitDirectDeps(func(module blueprint.Module) {
356 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700357 tag := ctx.OtherModuleDependencyTag(module)
358
Colin Crossf506d872017-07-19 15:53:04 -0700359 dep, _ := module.(Dependency)
360 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700361 switch tag {
362 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700363 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700364 default:
365 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700366 }
Colin Crossec7a0422017-07-07 14:47:12 -0700367 return
368 }
369
Colin Crossbe1da472017-07-07 15:59:46 -0700370 switch tag {
371 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700372 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700373 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700374 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700375 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700376 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
377 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700378 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700379 if ctx.ModuleName() == "framework" {
380 // framework.jar has a one-off dependency on the R.java and Manifest.java files
381 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700382 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700383 }
Colin Crossbe1da472017-07-07 15:59:46 -0700384 default:
385 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700386 }
Colin Crossbe1da472017-07-07 15:59:46 -0700387
Colin Cross32f676a2017-09-06 13:41:06 -0700388 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700389 })
390
Colin Cross32f676a2017-09-06 13:41:06 -0700391 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700392}
393
Colin Cross46c9b8b2017-06-22 16:51:17 -0700394func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700395
Colin Cross540eff82017-06-22 17:01:52 -0700396 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700397
Colin Cross32f676a2017-09-06 13:41:06 -0700398 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700399
Colin Crossf03c82b2015-04-13 13:53:40 -0700400 var flags javaBuilderFlags
401
402 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700403 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
404 javacFlags = config.StripJavac9Flags(javacFlags)
405 }
Colin Cross64162712017-08-08 13:17:59 -0700406
407 if j.properties.Java_version != nil {
408 flags.javaVersion = *j.properties.Java_version
409 } else {
410 flags.javaVersion = "${config.DefaultJavaVersion}"
411 }
412
Colin Cross6ade34f2017-09-15 13:00:47 -0700413 flags.bootClasspath.AddPaths(deps.bootClasspath)
414 flags.classpath.AddPaths(deps.classpath)
415
Colin Crossf03c82b2015-04-13 13:53:40 -0700416 if len(javacFlags) > 0 {
417 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
418 flags.javacFlags = "$javacFlags"
419 }
420
Colin Cross32f676a2017-09-06 13:41:06 -0700421 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700422 if len(aidlFlags) > 0 {
423 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
424 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700425 }
426
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700427 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700428
Colin Cross6af17aa2017-09-20 12:59:05 -0700429 if hasSrcExt(srcFiles.Strings(), ".proto") {
430 flags = protoFlags(ctx, &j.protoProperties, flags)
431 }
432
433 var srcFileLists android.Paths
434
435 srcFiles, srcFileLists = j.genSources(ctx, srcFiles, flags)
436
437 srcFileLists = append(srcFileLists, deps.srcFileLists...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700438
Colin Cross6af17aa2017-09-20 12:59:05 -0700439 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700440
Colin Cross0a6e0072017-08-30 14:24:55 -0700441 var jars android.Paths
442
Colin Cross8cf13342015-04-10 15:41:49 -0700443 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700444 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700445 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
446 // If error-prone is enabled, add an additional rule to compile the java files into
447 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700448 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700449 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
450 // enable error-prone without affecting the output class files.
Colin Cross6af17aa2017-09-20 12:59:05 -0700451 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700452 extraJarDeps = append(extraJarDeps, errorprone)
453 }
454
Colin Crossd6891432017-09-27 17:39:56 -0700455 // Compile java sources into .class files
Colin Cross6af17aa2017-09-20 12:59:05 -0700456 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, extraJarDeps)
Colin Crossd6891432017-09-27 17:39:56 -0700457 if ctx.Failed() {
458 return
459 }
460
Colin Cross0a6e0072017-08-30 14:24:55 -0700461 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700462 }
463
Colin Cross0f37af02017-09-27 17:42:05 -0700464 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
465 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
466
467 var resArgs []string
468 var resDeps android.Paths
469
470 resArgs = append(resArgs, dirArgs...)
471 resDeps = append(resDeps, dirDeps...)
472
473 resArgs = append(resArgs, fileArgs...)
474 resDeps = append(resDeps, fileDeps...)
475
476 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700477 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700478 resArgs = append(resArgs, srcArgs...)
479 resDeps = append(resDeps, srcDeps...)
480 }
Colin Cross40a36712017-09-27 17:41:35 -0700481
482 if len(resArgs) > 0 {
Colin Cross40a36712017-09-27 17:41:35 -0700483 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700484 if ctx.Failed() {
485 return
486 }
Colin Cross20978302015-04-10 17:05:07 -0700487
Colin Cross0a6e0072017-08-30 14:24:55 -0700488 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700489 }
490
Colin Cross6ade34f2017-09-15 13:00:47 -0700491 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700492 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700493
Colin Cross635acc92017-09-12 22:50:46 -0700494 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
495
Colin Cross0a6e0072017-08-30 14:24:55 -0700496 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700497 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700498 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700499
500 if j.properties.Jarjar_rules != nil {
501 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700502 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700503 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
504 if ctx.Failed() {
505 return
506 }
507 }
508
Colin Cross2fe66872015-03-30 17:20:39 -0700509 j.classpathFile = outputFile
510
Colin Crossa713a6f2017-09-20 18:04:44 -0700511 // TODO(ccross): handle hostdex
Colin Crossc157a8d2017-10-03 17:17:07 -0700512 if ctx.Device() && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700513 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700514 if false /* emma enabled */ {
515 // If you instrument class files that have local variable debug information in
516 // them emma does not correctly maintain the local variable table.
517 // This will cause an error when you try to convert the class files for Android.
518 // The workaround here is to build different dex file here based on emma switch
519 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
520 // option to remove local variable information
521 dxFlags = append(dxFlags, "--no-locals")
522 }
523
Colin Cross1332b002015-04-07 17:11:30 -0700524 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700525 dxFlags = append(dxFlags, "--no-optimize")
526 }
527
Colin Cross1332b002015-04-07 17:11:30 -0700528 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700529 dxFlags = append(dxFlags,
530 "--debug",
531 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700532 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700533 "--dump-width=1000")
534 }
535
Colin Cross595a4062017-08-31 12:30:37 -0700536 var minSdkVersion string
537 switch j.deviceProperties.Sdk_version {
538 case "", "current", "test_current", "system_current":
539 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
540 default:
541 minSdkVersion = j.deviceProperties.Sdk_version
542 }
543
544 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
545
Colin Cross2fe66872015-03-30 17:20:39 -0700546 flags.dxFlags = strings.Join(dxFlags, " ")
547
Colin Cross6ade34f2017-09-15 13:00:47 -0700548 desugarFlags := []string{
549 "--min_sdk_version " + minSdkVersion,
550 "--desugar_try_with_resources_if_needed=false",
551 "--allow_empty_bootclasspath",
552 }
553
554 if inList("--core-library", dxFlags) {
555 desugarFlags = append(desugarFlags, "--core_library")
556 }
557
558 flags.desugarFlags = strings.Join(desugarFlags, " ")
559
560 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700561 if ctx.Failed() {
562 return
563 }
564
Colin Cross7db5d632017-10-04 17:07:09 -0700565 // Compile classes.jar into classes.dex and then javalib.jar
566 outputFile = TransformClassesJarToDexJar(ctx, "javalib.jar", desugarJar, flags)
Colin Cross6ade34f2017-09-15 13:00:47 -0700567 if ctx.Failed() {
568 return
569 }
570
Colin Cross6ade34f2017-09-15 13:00:47 -0700571 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700572 }
Colin Crossb7a63242015-04-16 14:09:14 -0700573 ctx.CheckbuildFile(outputFile)
574 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700575}
576
Colin Cross59f1bb62017-09-27 17:59:10 -0700577func (j *Module) installable() bool {
578 return j.properties.Installable == nil || *j.properties.Installable
579}
580
Colin Crossf506d872017-07-19 15:53:04 -0700581var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700582
Colin Cross74d73e22017-08-02 11:05:49 -0700583func (j *Module) ClasspathFiles() android.Paths {
584 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700585}
586
Colin Cross46c9b8b2017-06-22 16:51:17 -0700587func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700588 return j.exportAidlIncludeDirs
589}
590
Colin Cross46c9b8b2017-06-22 16:51:17 -0700591var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700592
Colin Cross46c9b8b2017-06-22 16:51:17 -0700593func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700594 return j.logtagsSrcs
595}
596
Colin Cross2fe66872015-03-30 17:20:39 -0700597//
598// Java libraries (.jar file)
599//
600
Colin Crossf506d872017-07-19 15:53:04 -0700601type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700602 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700603}
604
Colin Crossf506d872017-07-19 15:53:04 -0700605func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700606 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700607
Colin Cross59f1bb62017-09-27 17:59:10 -0700608 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700609 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
610 ctx.ModuleName()+".jar", j.outputFile)
611 }
Colin Crossb7a63242015-04-16 14:09:14 -0700612}
613
Colin Crossf506d872017-07-19 15:53:04 -0700614func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700615 j.deps(ctx)
616}
617
Colin Crossa60ead82017-10-02 18:10:21 -0700618func LibraryFactory(installable bool) func() android.Module {
619 return func() android.Module {
620 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700621
Colin Crossa60ead82017-10-02 18:10:21 -0700622 if !installable {
623 module.properties.Installable = proptools.BoolPtr(false)
624 }
625 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700626
Colin Crossa60ead82017-10-02 18:10:21 -0700627 module.AddProperties(
628 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700629 &module.Module.deviceProperties,
630 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700631
Colin Crossa60ead82017-10-02 18:10:21 -0700632 InitJavaModule(module, android.HostAndDeviceSupported)
633 return module
634 }
Colin Cross2fe66872015-03-30 17:20:39 -0700635}
636
Colin Crossf506d872017-07-19 15:53:04 -0700637func LibraryHostFactory() android.Module {
638 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700639
Colin Cross6af17aa2017-09-20 12:59:05 -0700640 module.AddProperties(
641 &module.Module.properties,
642 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700643
Colin Cross89536d42017-07-07 14:35:50 -0700644 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700645 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700646}
647
648//
649// Java Binaries (.jar file plus wrapper script)
650//
651
Colin Crossf506d872017-07-19 15:53:04 -0700652type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700653 // installable script to execute the resulting jar
654 Wrapper string
655}
656
Colin Crossf506d872017-07-19 15:53:04 -0700657type Binary struct {
658 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700659
Colin Crossf506d872017-07-19 15:53:04 -0700660 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700661
662 wrapperFile android.ModuleSrcPath
663 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700664}
665
Colin Crossf506d872017-07-19 15:53:04 -0700666func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
667 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700668
669 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
670 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700671 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700672 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
673 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700674}
675
Colin Crossf506d872017-07-19 15:53:04 -0700676func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700677 j.deps(ctx)
678}
679
Colin Crossf506d872017-07-19 15:53:04 -0700680func BinaryFactory() android.Module {
681 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700682
Colin Cross540eff82017-06-22 17:01:52 -0700683 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700684
Colin Cross36242852017-06-23 15:06:31 -0700685 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700686 &module.Module.properties,
687 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700688 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700689 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700690
Colin Cross89536d42017-07-07 14:35:50 -0700691 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700692 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700693}
694
Colin Crossf506d872017-07-19 15:53:04 -0700695func BinaryHostFactory() android.Module {
696 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700697
Colin Cross36242852017-06-23 15:06:31 -0700698 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700699 &module.Module.properties,
700 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700701 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700702 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700703
Colin Cross89536d42017-07-07 14:35:50 -0700704 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700705 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700706}
707
708//
709// Java prebuilts
710//
711
Colin Cross74d73e22017-08-02 11:05:49 -0700712type ImportProperties struct {
713 Jars []string
714}
715
716type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700717 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700718 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700719
Colin Cross74d73e22017-08-02 11:05:49 -0700720 properties ImportProperties
721
Colin Cross0a6e0072017-08-30 14:24:55 -0700722 classpathFiles android.Paths
723 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700724}
725
Colin Cross74d73e22017-08-02 11:05:49 -0700726func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700727 return &j.prebuilt
728}
729
Colin Cross74d73e22017-08-02 11:05:49 -0700730func (j *Import) PrebuiltSrcs() []string {
731 return j.properties.Jars
732}
733
734func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700735 return j.prebuilt.Name(j.ModuleBase.Name())
736}
737
Colin Cross74d73e22017-08-02 11:05:49 -0700738func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700739}
740
Colin Cross74d73e22017-08-02 11:05:49 -0700741func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
742 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700743
Colin Cross635acc92017-09-12 22:50:46 -0700744 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700745}
746
Colin Cross74d73e22017-08-02 11:05:49 -0700747var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700748
Colin Cross74d73e22017-08-02 11:05:49 -0700749func (j *Import) ClasspathFiles() android.Paths {
750 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700751}
752
Colin Cross74d73e22017-08-02 11:05:49 -0700753func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700754 return nil
755}
756
Colin Cross74d73e22017-08-02 11:05:49 -0700757var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700758
Colin Cross74d73e22017-08-02 11:05:49 -0700759func ImportFactory() android.Module {
760 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700761
Colin Cross74d73e22017-08-02 11:05:49 -0700762 module.AddProperties(&module.properties)
763
764 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700765 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
766 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700767}
768
Colin Cross74d73e22017-08-02 11:05:49 -0700769func ImportFactoryHost() android.Module {
770 module := &Import{}
771
772 module.AddProperties(&module.properties)
773
774 android.InitPrebuiltModule(module, &module.properties.Jars)
775 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
776 return module
777}
778
Colin Cross2fe66872015-03-30 17:20:39 -0700779func inList(s string, l []string) bool {
780 for _, e := range l {
781 if e == s {
782 return true
783 }
784 }
785 return false
786}
Colin Cross89536d42017-07-07 14:35:50 -0700787
788//
789// Defaults
790//
791type Defaults struct {
792 android.ModuleBase
793 android.DefaultsModuleBase
794}
795
796func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
797}
798
799func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
800}
801
802func defaultsFactory() android.Module {
803 return DefaultsFactory()
804}
805
806func DefaultsFactory(props ...interface{}) android.Module {
807 module := &Defaults{}
808
809 module.AddProperties(props...)
810 module.AddProperties(
811 &CompilerProperties{},
812 &CompilerDeviceProperties{},
813 )
814
815 android.InitDefaultsModule(module)
816
817 return module
818}