blob: d8bc0c2cb1bd92b0b9c5021a3ad657fdfeee5120 [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 Cross0607cf72015-04-28 13:28:51 -070031 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Crossa60ead82017-10-02 18:10:21 -070038 android.RegisterModuleType("java_library", LibraryFactory(true))
39 android.RegisterModuleType("java_library_static", LibraryFactory(false))
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070043 android.RegisterModuleType("java_import", ImportFactory)
44 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross798bfce2016-10-12 14:28:16 -070045 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070046
Colin Cross798bfce2016-10-12 14:28:16 -070047 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070048}
49
Colin Cross2fe66872015-03-30 17:20:39 -070050// TODO:
51// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070052// Proto
53// Renderscript
54// Post-jar passes:
55// Proguard
Colin Crossba211132017-06-22 15:36:39 -070056// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070057// Jarjar
58// Dex
59// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070060// DroidDoc
61// Findbugs
62
Colin Cross89536d42017-07-07 14:35:50 -070063type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070064 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
65 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070066 Srcs []string `android:"arch_variant"`
67
68 // list of source files that should not be used to build the Java module.
69 // This is most useful in the arch/multilib variants to remove non-common files
70 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
72 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070073 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070074
Colin Cross86a63ff2017-09-27 17:33:10 -070075 // list of directories that should be excluded from java_resource_dirs
76 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070077
Colin Cross0f37af02017-09-27 17:42:05 -070078 // list of files to use as Java resources
79 Java_resources []string `android:"arch_variant"`
80
81 // list of files that should be excluded from java_resources
82 Exclude_java_resources []string `android:"arch_variant"`
83
Colin Crossfa5eb232017-10-01 20:33:03 -070084 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070086 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070087
Colin Crossfa5eb232017-10-01 20:33:03 -070088 // don't build against the framework libraries (legacy-test, core-junit,
89 // ext, and framework for device targets)
90 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Colin Cross7d5136f2015-05-11 13:39:40 -070095 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross540eff82017-06-22 17:01:52 -0700104 // if not blank, run jarjar using the specified rules file
105 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -0700106
107 // If not blank, set the java version passed to javac as -source and -target
108 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700109
110 // If set to false, don't allow this module to be installed. Defaults to true.
111 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700112
Colin Cross0f37af02017-09-27 17:42:05 -0700113 // If set to true, include sources used to compile the module in to the final jar
114 Include_srcs *bool
115
Colin Cross32f676a2017-09-06 13:41:06 -0700116 // List of modules to use as annotation processors
117 Annotation_processors []string
118
119 // List of classes to pass to javac to use as annotation processors
120 Annotation_processor_classes []string
Colin Cross540eff82017-06-22 17:01:52 -0700121}
122
Colin Cross89536d42017-07-07 14:35:50 -0700123type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700124 // list of module-specific flags that will be used for dex compiles
125 Dxflags []string `android:"arch_variant"`
126
Colin Cross7d5136f2015-05-11 13:39:40 -0700127 // if not blank, set to the version of the sdk to compile against
128 Sdk_version string
129
130 // Set for device java libraries, and for host versions of device java libraries
131 // built for testing
132 Dex bool `blueprint:"mutated"`
133
Colin Cross7d5136f2015-05-11 13:39:40 -0700134 // directories to pass to aidl tool
135 Aidl_includes []string
136
137 // directories that should be added as include directories
138 // for any aidl sources of modules that depend on this module
139 Export_aidl_include_dirs []string
140}
141
Colin Cross46c9b8b2017-06-22 16:51:17 -0700142// Module contains the properties and members used by all java module types
143type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700144 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700145 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700146
Colin Cross89536d42017-07-07 14:35:50 -0700147 properties CompilerProperties
148 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700149
150 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700151 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700152
Colin Cross6ade34f2017-09-15 13:00:47 -0700153 // output file containing classes.dex
154 dexJarFile android.Path
155
156 // output files containing resources
157 resourceJarFiles android.Paths
158
Colin Crossb7a63242015-04-16 14:09:14 -0700159 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700160 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700161
Colin Cross635c3b02016-05-18 15:37:25 -0700162 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700163
Colin Cross635c3b02016-05-18 15:37:25 -0700164 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700165
Colin Crossb7a63242015-04-16 14:09:14 -0700166 // filelists of extra source files that should be included in the javac command line,
167 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700168 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700169
Colin Cross2fe66872015-03-30 17:20:39 -0700170 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700171 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700172}
173
Colin Crossf506d872017-07-19 15:53:04 -0700174type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700175 ClasspathFiles() android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700176 ResourceJarFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700177 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700178}
179
Colin Cross89536d42017-07-07 14:35:50 -0700180func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
181 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
182 android.InitDefaultableModule(module)
183}
184
Colin Crossbe1da472017-07-07 15:59:46 -0700185type dependencyTag struct {
186 blueprint.BaseDependencyTag
187 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700188}
189
Colin Crossbe1da472017-07-07 15:59:46 -0700190var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700191 staticLibTag = dependencyTag{name: "staticlib"}
192 libTag = dependencyTag{name: "javalib"}
193 bootClasspathTag = dependencyTag{name: "bootclasspath"}
194 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700195)
Colin Cross2fe66872015-03-30 17:20:39 -0700196
Colin Crossfc3674a2017-09-18 17:41:52 -0700197type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700198 useModule, useFiles, useDefaultLibs, invalidVersion bool
199
200 module string
201 jar android.Path
202 aidl android.Path
Colin Crossfc3674a2017-09-18 17:41:52 -0700203}
204
205func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
206 switch v {
207 case "", "current", "system_current", "test_current":
208 // OK
209 default:
210 if _, err := strconv.Atoi(v); err != nil {
211 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
212 return sdkDep{}
213 }
214 }
215
216 toFile := func(v string) sdkDep {
217 dir := filepath.Join("prebuilts/sdk", v)
218 jar := filepath.Join(dir, "android.jar")
219 aidl := filepath.Join(dir, "framework.aidl")
220 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
221 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700222
223 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() {
224 return sdkDep{
225 invalidVersion: true,
226 module: "sdk_v" + v,
227 }
228 }
229
Colin Crossfc3674a2017-09-18 17:41:52 -0700230 if !jarPath.Valid() {
231 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
232 return sdkDep{}
233 }
Colin Cross47ff2522017-10-02 14:22:08 -0700234
Colin Crossfc3674a2017-09-18 17:41:52 -0700235 if !aidlPath.Valid() {
236 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
237 return sdkDep{}
238 }
Colin Cross47ff2522017-10-02 14:22:08 -0700239
Colin Crossfc3674a2017-09-18 17:41:52 -0700240 return sdkDep{
241 useFiles: true,
242 jar: jarPath.Path(),
243 aidl: aidlPath.Path(),
244 }
245 }
246
247 toModule := func(m string) sdkDep {
248 return sdkDep{
249 useModule: true,
250 module: m,
251 }
252 }
253
Colin Cross8b9d37b2017-09-22 15:30:06 -0700254 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700255 return toFile(v)
256 }
257
258 switch v {
259 case "":
260 return sdkDep{
261 useDefaultLibs: true,
262 }
263 case "current":
264 return toModule("android_stubs_current")
265 case "system_current":
266 return toModule("android_system_stubs_current")
267 case "test_current":
268 return toModule("android_test_stubs_current")
269 default:
270 return toFile(v)
271 }
272}
273
Colin Crossbe1da472017-07-07 15:59:46 -0700274func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700275 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700276 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700277 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
278 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700279 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700280 if !proptools.Bool(j.properties.No_framework_libs) {
281 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
282 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700283 }
284 if sdkDep.useModule {
285 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700286 }
287 } else {
288 if j.deviceProperties.Dex {
Colin Crosscb2c9292017-09-23 19:57:16 -0700289 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700290 }
Colin Cross2fe66872015-03-30 17:20:39 -0700291 }
292 }
Colin Crossf506d872017-07-19 15:53:04 -0700293 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
294 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700295 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700296
297 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700298 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross2fe66872015-03-30 17:20:39 -0700299}
300
Colin Cross46c9b8b2017-06-22 16:51:17 -0700301func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700302 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700303
Colin Cross540eff82017-06-22 17:01:52 -0700304 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700305
306 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700307 if aidlPreprocess.Valid() {
308 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700309 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700310 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700311 }
312
Colin Cross635c3b02016-05-18 15:37:25 -0700313 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
314 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
315 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700316 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
317 flags = append(flags, "-I"+src.String())
318 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700319
Colin Crossf03c82b2015-04-13 13:53:40 -0700320 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700321}
322
Colin Cross32f676a2017-09-06 13:41:06 -0700323type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700324 classpath android.Paths
325 bootClasspath android.Paths
326 staticJars android.Paths
327 staticJarResources android.Paths
328 aidlIncludeDirs android.Paths
329 srcFileLists android.Paths
330 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700331}
Colin Cross2fe66872015-03-30 17:20:39 -0700332
Colin Cross32f676a2017-09-06 13:41:06 -0700333func (j *Module) collectDeps(ctx android.ModuleContext) deps {
334 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700335
336 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
Colin Cross47ff2522017-10-02 14:22:08 -0700337 if sdkDep.invalidVersion {
338 ctx.AddMissingDependencies([]string{sdkDep.module})
339 } else if sdkDep.useFiles {
Colin Crossfc3674a2017-09-18 17:41:52 -0700340 deps.classpath = append(deps.classpath, sdkDep.jar)
341 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
342 }
343
Colin Cross2fe66872015-03-30 17:20:39 -0700344 ctx.VisitDirectDeps(func(module blueprint.Module) {
345 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700346 tag := ctx.OtherModuleDependencyTag(module)
347
Colin Crossf506d872017-07-19 15:53:04 -0700348 dep, _ := module.(Dependency)
349 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700350 switch tag {
351 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700352 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700353 default:
354 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700355 }
Colin Crossec7a0422017-07-07 14:47:12 -0700356 return
357 }
358
Colin Crossbe1da472017-07-07 15:59:46 -0700359 switch tag {
360 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700361 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700362 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700363 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700364 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700365 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
366 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700367 deps.staticJarResources = append(deps.staticJarResources, dep.ResourceJarFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700368 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700369 if ctx.ModuleName() == "framework" {
370 // framework.jar has a one-off dependency on the R.java and Manifest.java files
371 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700372 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700373 }
Colin Crossbe1da472017-07-07 15:59:46 -0700374 default:
375 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700376 }
Colin Crossbe1da472017-07-07 15:59:46 -0700377
Colin Cross32f676a2017-09-06 13:41:06 -0700378 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700379 })
380
Colin Cross32f676a2017-09-06 13:41:06 -0700381 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700382}
383
Colin Cross46c9b8b2017-06-22 16:51:17 -0700384func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700385
Colin Cross540eff82017-06-22 17:01:52 -0700386 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700387
Colin Cross32f676a2017-09-06 13:41:06 -0700388 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700389
Colin Crossf03c82b2015-04-13 13:53:40 -0700390 var flags javaBuilderFlags
391
392 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700393 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
394 javacFlags = config.StripJavac9Flags(javacFlags)
395 }
Colin Cross64162712017-08-08 13:17:59 -0700396
397 if j.properties.Java_version != nil {
398 flags.javaVersion = *j.properties.Java_version
399 } else {
400 flags.javaVersion = "${config.DefaultJavaVersion}"
401 }
402
Colin Cross6ade34f2017-09-15 13:00:47 -0700403 flags.bootClasspath.AddPaths(deps.bootClasspath)
404 flags.classpath.AddPaths(deps.classpath)
405
Colin Crossf03c82b2015-04-13 13:53:40 -0700406 if len(javacFlags) > 0 {
407 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
408 flags.javacFlags = "$javacFlags"
409 }
410
Colin Cross32f676a2017-09-06 13:41:06 -0700411 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700412 if len(aidlFlags) > 0 {
413 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
414 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700415 }
416
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700417 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700418
Colin Crossf05fe972015-04-10 17:45:20 -0700419 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700420
Colin Cross0607cf72015-04-28 13:28:51 -0700421 ctx.VisitDirectDeps(func(module blueprint.Module) {
422 if gen, ok := module.(genrule.SourceFileGenerator); ok {
423 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
424 }
425 })
426
Colin Cross32f676a2017-09-06 13:41:06 -0700427 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700428
Colin Cross0a6e0072017-08-30 14:24:55 -0700429 var jars android.Paths
430
Colin Cross8cf13342015-04-10 15:41:49 -0700431 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700432 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700433 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
434 // If error-prone is enabled, add an additional rule to compile the java files into
435 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700436 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700437 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
438 // enable error-prone without affecting the output class files.
Colin Crossd6891432017-09-27 17:39:56 -0700439 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, nil)
Colin Crossc6bbef32017-08-14 14:16:06 -0700440 extraJarDeps = append(extraJarDeps, errorprone)
441 }
442
Colin Crossd6891432017-09-27 17:39:56 -0700443 // Compile java sources into .class files
444 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraJarDeps)
445 if ctx.Failed() {
446 return
447 }
448
Colin Cross0a6e0072017-08-30 14:24:55 -0700449 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700450 }
451
Colin Cross0f37af02017-09-27 17:42:05 -0700452 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
453 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
454
455 var resArgs []string
456 var resDeps android.Paths
457
458 resArgs = append(resArgs, dirArgs...)
459 resDeps = append(resDeps, dirDeps...)
460
461 resArgs = append(resArgs, fileArgs...)
462 resDeps = append(resDeps, fileDeps...)
463
464 if proptools.Bool(j.properties.Include_srcs) {
465 srcArgs, srcDeps := ResourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
466 resArgs = append(resArgs, srcArgs...)
467 resDeps = append(resDeps, srcDeps...)
468 }
Colin Cross40a36712017-09-27 17:41:35 -0700469
470 if len(resArgs) > 0 {
Colin Cross0a6e0072017-08-30 14:24:55 -0700471 // Combine classes + resources into classes-full-debug.jar
Colin Cross40a36712017-09-27 17:41:35 -0700472 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700473 if ctx.Failed() {
474 return
475 }
Colin Cross20978302015-04-10 17:05:07 -0700476
Colin Cross6ade34f2017-09-15 13:00:47 -0700477 j.resourceJarFiles = append(j.resourceJarFiles, resourceJar)
Colin Cross0a6e0072017-08-30 14:24:55 -0700478 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700479 }
480
Colin Cross6ade34f2017-09-15 13:00:47 -0700481 // Propagate the resources from the transitive closure of static dependencies for copying
482 // into dex jars
483 j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...)
484
485 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700486 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700487
Colin Cross635acc92017-09-12 22:50:46 -0700488 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
489
Colin Cross0a6e0072017-08-30 14:24:55 -0700490 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700491 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700492 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700493
494 if j.properties.Jarjar_rules != nil {
495 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700496 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700497 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
498 if ctx.Failed() {
499 return
500 }
501 }
502
Colin Cross2fe66872015-03-30 17:20:39 -0700503 j.classpathFile = outputFile
504
Colin Crossa713a6f2017-09-20 18:04:44 -0700505 // TODO(ccross): handle hostdex
Colin Cross59f1bb62017-09-27 17:59:10 -0700506 if ctx.Device() && len(srcFiles) > 0 && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700507 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700508 if false /* emma enabled */ {
509 // If you instrument class files that have local variable debug information in
510 // them emma does not correctly maintain the local variable table.
511 // This will cause an error when you try to convert the class files for Android.
512 // The workaround here is to build different dex file here based on emma switch
513 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
514 // option to remove local variable information
515 dxFlags = append(dxFlags, "--no-locals")
516 }
517
Colin Cross1332b002015-04-07 17:11:30 -0700518 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700519 dxFlags = append(dxFlags, "--no-optimize")
520 }
521
Colin Cross1332b002015-04-07 17:11:30 -0700522 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700523 dxFlags = append(dxFlags,
524 "--debug",
525 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700526 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700527 "--dump-width=1000")
528 }
529
Colin Cross595a4062017-08-31 12:30:37 -0700530 var minSdkVersion string
531 switch j.deviceProperties.Sdk_version {
532 case "", "current", "test_current", "system_current":
533 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
534 default:
535 minSdkVersion = j.deviceProperties.Sdk_version
536 }
537
538 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
539
Colin Cross2fe66872015-03-30 17:20:39 -0700540 flags.dxFlags = strings.Join(dxFlags, " ")
541
Colin Cross6ade34f2017-09-15 13:00:47 -0700542 desugarFlags := []string{
543 "--min_sdk_version " + minSdkVersion,
544 "--desugar_try_with_resources_if_needed=false",
545 "--allow_empty_bootclasspath",
546 }
547
548 if inList("--core-library", dxFlags) {
549 desugarFlags = append(desugarFlags, "--core_library")
550 }
551
552 flags.desugarFlags = strings.Join(desugarFlags, " ")
553
554 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700555 if ctx.Failed() {
556 return
557 }
558
Colin Cross6ade34f2017-09-15 13:00:47 -0700559 // Compile classes.jar into classes.dex
560 dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
561 if ctx.Failed() {
562 return
563 }
564
565 jars := android.Paths{dexJarFile}
566 jars = append(jars, j.resourceJarFiles...)
567
568 outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true)
569
570 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700571 }
Colin Crossb7a63242015-04-16 14:09:14 -0700572 ctx.CheckbuildFile(outputFile)
573 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700574}
575
Colin Cross59f1bb62017-09-27 17:59:10 -0700576func (j *Module) installable() bool {
577 return j.properties.Installable == nil || *j.properties.Installable
578}
579
Colin Crossf506d872017-07-19 15:53:04 -0700580var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700581
Colin Cross74d73e22017-08-02 11:05:49 -0700582func (j *Module) ClasspathFiles() android.Paths {
583 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700584}
585
Colin Cross6ade34f2017-09-15 13:00:47 -0700586func (j *Module) ResourceJarFiles() android.Paths {
587 return j.resourceJarFiles
588}
589
Colin Cross46c9b8b2017-06-22 16:51:17 -0700590func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700591 return j.exportAidlIncludeDirs
592}
593
Colin Cross46c9b8b2017-06-22 16:51:17 -0700594var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700595
Colin Cross46c9b8b2017-06-22 16:51:17 -0700596func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700597 return j.logtagsSrcs
598}
599
Colin Cross2fe66872015-03-30 17:20:39 -0700600//
601// Java libraries (.jar file)
602//
603
Colin Crossf506d872017-07-19 15:53:04 -0700604type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700605 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700606}
607
Colin Crossf506d872017-07-19 15:53:04 -0700608func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700609 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700610
Colin Cross59f1bb62017-09-27 17:59:10 -0700611 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700612 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
613 ctx.ModuleName()+".jar", j.outputFile)
614 }
Colin Crossb7a63242015-04-16 14:09:14 -0700615}
616
Colin Crossf506d872017-07-19 15:53:04 -0700617func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700618 j.deps(ctx)
619}
620
Colin Crossa60ead82017-10-02 18:10:21 -0700621func LibraryFactory(installable bool) func() android.Module {
622 return func() android.Module {
623 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700624
Colin Crossa60ead82017-10-02 18:10:21 -0700625 if !installable {
626 module.properties.Installable = proptools.BoolPtr(false)
627 }
628 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700629
Colin Crossa60ead82017-10-02 18:10:21 -0700630 module.AddProperties(
631 &module.Module.properties,
632 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700633
Colin Crossa60ead82017-10-02 18:10:21 -0700634 InitJavaModule(module, android.HostAndDeviceSupported)
635 return module
636 }
Colin Cross2fe66872015-03-30 17:20:39 -0700637}
638
Colin Crossf506d872017-07-19 15:53:04 -0700639func LibraryHostFactory() android.Module {
640 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700641
Colin Cross36242852017-06-23 15:06:31 -0700642 module.AddProperties(&module.Module.properties)
643
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,
688 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700689
Colin Cross89536d42017-07-07 14:35:50 -0700690 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700691 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700692}
693
Colin Crossf506d872017-07-19 15:53:04 -0700694func BinaryHostFactory() android.Module {
695 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700696
Colin Cross36242852017-06-23 15:06:31 -0700697 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700698 &module.Module.properties,
699 &module.Module.deviceProperties,
700 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700701
Colin Cross89536d42017-07-07 14:35:50 -0700702 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700703 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700704}
705
706//
707// Java prebuilts
708//
709
Colin Cross74d73e22017-08-02 11:05:49 -0700710type ImportProperties struct {
711 Jars []string
712}
713
714type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700715 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700716 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700717
Colin Cross74d73e22017-08-02 11:05:49 -0700718 properties ImportProperties
719
Colin Cross0a6e0072017-08-30 14:24:55 -0700720 classpathFiles android.Paths
721 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700722}
723
Colin Cross74d73e22017-08-02 11:05:49 -0700724func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700725 return &j.prebuilt
726}
727
Colin Cross74d73e22017-08-02 11:05:49 -0700728func (j *Import) PrebuiltSrcs() []string {
729 return j.properties.Jars
730}
731
732func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700733 return j.prebuilt.Name(j.ModuleBase.Name())
734}
735
Colin Cross74d73e22017-08-02 11:05:49 -0700736func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700737}
738
Colin Cross74d73e22017-08-02 11:05:49 -0700739func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
740 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700741
Colin Cross635acc92017-09-12 22:50:46 -0700742 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700743}
744
Colin Cross74d73e22017-08-02 11:05:49 -0700745var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700746
Colin Cross74d73e22017-08-02 11:05:49 -0700747func (j *Import) ClasspathFiles() android.Paths {
748 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700749}
750
Colin Cross6ade34f2017-09-15 13:00:47 -0700751func (j *Import) ResourceJarFiles() android.Paths {
752 // resources are in the ClasspathFiles
753 return nil
754}
755
Colin Cross74d73e22017-08-02 11:05:49 -0700756func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700757 return nil
758}
759
Colin Cross74d73e22017-08-02 11:05:49 -0700760var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700761
Colin Cross74d73e22017-08-02 11:05:49 -0700762func ImportFactory() android.Module {
763 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700764
Colin Cross74d73e22017-08-02 11:05:49 -0700765 module.AddProperties(&module.properties)
766
767 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700768 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
769 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700770}
771
Colin Cross74d73e22017-08-02 11:05:49 -0700772func ImportFactoryHost() android.Module {
773 module := &Import{}
774
775 module.AddProperties(&module.properties)
776
777 android.InitPrebuiltModule(module, &module.properties.Jars)
778 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
779 return module
780}
781
Colin Cross2fe66872015-03-30 17:20:39 -0700782func inList(s string, l []string) bool {
783 for _, e := range l {
784 if e == s {
785 return true
786 }
787 }
788 return false
789}
Colin Cross89536d42017-07-07 14:35:50 -0700790
791//
792// Defaults
793//
794type Defaults struct {
795 android.ModuleBase
796 android.DefaultsModuleBase
797}
798
799func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
800}
801
802func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
803}
804
805func defaultsFactory() android.Module {
806 return DefaultsFactory()
807}
808
809func DefaultsFactory(props ...interface{}) android.Module {
810 module := &Defaults{}
811
812 module.AddProperties(props...)
813 module.AddProperties(
814 &CompilerProperties{},
815 &CompilerDeviceProperties{},
816 )
817
818 android.InitDefaultsModule(module)
819
820 return module
821}