blob: 82dabfaaa01c0c507208c9077f54a0d74a5dd9e0 [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 Crossf506d872017-07-19 15:53:04 -070038 android.RegisterModuleType("java_library", LibraryFactory)
39 android.RegisterModuleType("java_library_static", LibraryFactory)
40 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
Paul Duffin2b67e3b2016-11-30 16:13:09 +000078 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070080 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070081
82 // list of module-specific flags that will be used for javac compiles
83 Javacflags []string `android:"arch_variant"`
84
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070086 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070089 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070090
91 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070093
Colin Cross540eff82017-06-22 17:01:52 -070094 // if not blank, run jarjar using the specified rules file
95 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -070096
97 // If not blank, set the java version passed to javac as -source and -target
98 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -070099
100 // If set to false, don't allow this module to be installed. Defaults to true.
101 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700102
103 // List of modules to use as annotation processors
104 Annotation_processors []string
105
106 // List of classes to pass to javac to use as annotation processors
107 Annotation_processor_classes []string
Colin Cross540eff82017-06-22 17:01:52 -0700108}
109
Colin Cross89536d42017-07-07 14:35:50 -0700110type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700111 // list of module-specific flags that will be used for dex compiles
112 Dxflags []string `android:"arch_variant"`
113
Colin Cross7d5136f2015-05-11 13:39:40 -0700114 // if not blank, set to the version of the sdk to compile against
115 Sdk_version string
116
117 // Set for device java libraries, and for host versions of device java libraries
118 // built for testing
119 Dex bool `blueprint:"mutated"`
120
Colin Cross7d5136f2015-05-11 13:39:40 -0700121 // directories to pass to aidl tool
122 Aidl_includes []string
123
124 // directories that should be added as include directories
125 // for any aidl sources of modules that depend on this module
126 Export_aidl_include_dirs []string
127}
128
Colin Cross46c9b8b2017-06-22 16:51:17 -0700129// Module contains the properties and members used by all java module types
130type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700131 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700132 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700133
Colin Cross89536d42017-07-07 14:35:50 -0700134 properties CompilerProperties
135 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700136
137 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700138 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700139
Colin Cross6ade34f2017-09-15 13:00:47 -0700140 // output file containing classes.dex
141 dexJarFile android.Path
142
143 // output files containing resources
144 resourceJarFiles android.Paths
145
Colin Crossb7a63242015-04-16 14:09:14 -0700146 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700147 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700148
Colin Cross635c3b02016-05-18 15:37:25 -0700149 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700150
Colin Cross635c3b02016-05-18 15:37:25 -0700151 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700152
Colin Crossb7a63242015-04-16 14:09:14 -0700153 // filelists of extra source files that should be included in the javac command line,
154 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700155 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700156
Colin Cross2fe66872015-03-30 17:20:39 -0700157 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700158 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700159}
160
Colin Crossf506d872017-07-19 15:53:04 -0700161type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700162 ClasspathFiles() android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700163 ResourceJarFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700164 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700165}
166
Colin Cross89536d42017-07-07 14:35:50 -0700167func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
168 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
169 android.InitDefaultableModule(module)
170}
171
Colin Crossbe1da472017-07-07 15:59:46 -0700172type dependencyTag struct {
173 blueprint.BaseDependencyTag
174 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700175}
176
Colin Crossbe1da472017-07-07 15:59:46 -0700177var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700178 staticLibTag = dependencyTag{name: "staticlib"}
179 libTag = dependencyTag{name: "javalib"}
180 bootClasspathTag = dependencyTag{name: "bootclasspath"}
181 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700182)
Colin Cross2fe66872015-03-30 17:20:39 -0700183
Colin Crossfc3674a2017-09-18 17:41:52 -0700184type sdkDep struct {
185 useModule, useFiles, useDefaultLibs bool
186 module string
187 jar android.Path
188 aidl android.Path
189}
190
191func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
192 switch v {
193 case "", "current", "system_current", "test_current":
194 // OK
195 default:
196 if _, err := strconv.Atoi(v); err != nil {
197 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
198 return sdkDep{}
199 }
200 }
201
202 toFile := func(v string) sdkDep {
203 dir := filepath.Join("prebuilts/sdk", v)
204 jar := filepath.Join(dir, "android.jar")
205 aidl := filepath.Join(dir, "framework.aidl")
206 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
207 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
208 if !jarPath.Valid() {
209 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
210 return sdkDep{}
211 }
212 if !aidlPath.Valid() {
213 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
214 return sdkDep{}
215 }
216 return sdkDep{
217 useFiles: true,
218 jar: jarPath.Path(),
219 aidl: aidlPath.Path(),
220 }
221 }
222
223 toModule := func(m string) sdkDep {
224 return sdkDep{
225 useModule: true,
226 module: m,
227 }
228 }
229
230 if ctx.AConfig().UnbundledBuild() {
231 if v == "" {
232 if ctx, ok := ctx.(android.ModuleContext); ok {
233 ctx.AddMissingDependencies([]string{"sdk_version_must_be_set_for_modules_used_in_unbundled_builds"})
234 }
235 return sdkDep{}
236 }
237 return toFile(v)
238 }
239
240 switch v {
241 case "":
242 return sdkDep{
243 useDefaultLibs: true,
244 }
245 case "current":
246 return toModule("android_stubs_current")
247 case "system_current":
248 return toModule("android_system_stubs_current")
249 case "test_current":
250 return toModule("android_test_stubs_current")
251 default:
252 return toFile(v)
253 }
254}
255
Colin Crossbe1da472017-07-07 15:59:46 -0700256func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700257 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700258 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700259 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
260 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700261 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross227d4362017-08-30 14:14:52 -0700262 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossfc3674a2017-09-18 17:41:52 -0700263 }
264 if sdkDep.useModule {
265 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700266 }
267 } else {
268 if j.deviceProperties.Dex {
Colin Crosscb2c9292017-09-23 19:57:16 -0700269 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700270 }
Colin Cross2fe66872015-03-30 17:20:39 -0700271 }
272 }
Colin Crossf506d872017-07-19 15:53:04 -0700273 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
274 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700275 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700276
277 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross2fe66872015-03-30 17:20:39 -0700278}
279
Colin Cross46c9b8b2017-06-22 16:51:17 -0700280func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700281 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700282
Colin Cross540eff82017-06-22 17:01:52 -0700283 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700284
285 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700286 if aidlPreprocess.Valid() {
287 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700288 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700289 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700290 }
291
Colin Cross635c3b02016-05-18 15:37:25 -0700292 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
293 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
294 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700295 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
296 flags = append(flags, "-I"+src.String())
297 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700298
Colin Crossf03c82b2015-04-13 13:53:40 -0700299 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700300}
301
Colin Cross32f676a2017-09-06 13:41:06 -0700302type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700303 classpath android.Paths
304 bootClasspath android.Paths
305 staticJars android.Paths
306 staticJarResources android.Paths
307 aidlIncludeDirs android.Paths
308 srcFileLists android.Paths
309 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700310}
Colin Cross2fe66872015-03-30 17:20:39 -0700311
Colin Cross32f676a2017-09-06 13:41:06 -0700312func (j *Module) collectDeps(ctx android.ModuleContext) deps {
313 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700314
315 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
316 if sdkDep.useFiles {
317 deps.classpath = append(deps.classpath, sdkDep.jar)
318 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
319 }
320
Colin Cross2fe66872015-03-30 17:20:39 -0700321 ctx.VisitDirectDeps(func(module blueprint.Module) {
322 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700323 tag := ctx.OtherModuleDependencyTag(module)
324
Colin Crossf506d872017-07-19 15:53:04 -0700325 dep, _ := module.(Dependency)
326 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700327 switch tag {
328 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700329 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700330 default:
331 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700332 }
Colin Crossec7a0422017-07-07 14:47:12 -0700333 return
334 }
335
Colin Crossbe1da472017-07-07 15:59:46 -0700336 switch tag {
337 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700338 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700339 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700340 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700341 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700342 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
343 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700344 deps.staticJarResources = append(deps.staticJarResources, dep.ResourceJarFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700345 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700346 if ctx.ModuleName() == "framework" {
347 // framework.jar has a one-off dependency on the R.java and Manifest.java files
348 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700349 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700350 }
Colin Crossbe1da472017-07-07 15:59:46 -0700351 default:
352 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700353 }
Colin Crossbe1da472017-07-07 15:59:46 -0700354
Colin Cross32f676a2017-09-06 13:41:06 -0700355 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700356 })
357
Colin Cross32f676a2017-09-06 13:41:06 -0700358 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700359}
360
Colin Cross46c9b8b2017-06-22 16:51:17 -0700361func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700362
Colin Cross540eff82017-06-22 17:01:52 -0700363 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700364
Colin Cross32f676a2017-09-06 13:41:06 -0700365 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700366
Colin Crossf03c82b2015-04-13 13:53:40 -0700367 var flags javaBuilderFlags
368
369 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700370 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
371 javacFlags = config.StripJavac9Flags(javacFlags)
372 }
Colin Cross64162712017-08-08 13:17:59 -0700373
374 if j.properties.Java_version != nil {
375 flags.javaVersion = *j.properties.Java_version
376 } else {
377 flags.javaVersion = "${config.DefaultJavaVersion}"
378 }
379
Colin Cross6ade34f2017-09-15 13:00:47 -0700380 flags.bootClasspath.AddPaths(deps.bootClasspath)
381 flags.classpath.AddPaths(deps.classpath)
382
Colin Crossf03c82b2015-04-13 13:53:40 -0700383 if len(javacFlags) > 0 {
384 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
385 flags.javacFlags = "$javacFlags"
386 }
387
Colin Cross32f676a2017-09-06 13:41:06 -0700388 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700389 if len(aidlFlags) > 0 {
390 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
391 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700392 }
393
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700394 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700395
Colin Crossf05fe972015-04-10 17:45:20 -0700396 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700397
Colin Cross0607cf72015-04-28 13:28:51 -0700398 ctx.VisitDirectDeps(func(module blueprint.Module) {
399 if gen, ok := module.(genrule.SourceFileGenerator); ok {
400 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
401 }
402 })
403
Colin Cross32f676a2017-09-06 13:41:06 -0700404 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700405
Colin Cross0a6e0072017-08-30 14:24:55 -0700406 var jars android.Paths
407
Colin Cross8cf13342015-04-10 15:41:49 -0700408 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700409 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700410 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
411 // If error-prone is enabled, add an additional rule to compile the java files into
412 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700413 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700414 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
415 // enable error-prone without affecting the output class files.
Colin Crossd6891432017-09-27 17:39:56 -0700416 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, nil)
Colin Crossc6bbef32017-08-14 14:16:06 -0700417 extraJarDeps = append(extraJarDeps, errorprone)
418 }
419
Colin Crossd6891432017-09-27 17:39:56 -0700420 // Compile java sources into .class files
421 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraJarDeps)
422 if ctx.Failed() {
423 return
424 }
425
Colin Cross0a6e0072017-08-30 14:24:55 -0700426 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700427 }
428
Colin Cross86a63ff2017-09-27 17:33:10 -0700429 resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
Colin Cross635acc92017-09-12 22:50:46 -0700430 if len(resourceJarSpecs) > 0 {
Colin Cross0a6e0072017-08-30 14:24:55 -0700431 // Combine classes + resources into classes-full-debug.jar
Colin Cross635acc92017-09-12 22:50:46 -0700432 resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, extraJarDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700433 if ctx.Failed() {
434 return
435 }
Colin Cross20978302015-04-10 17:05:07 -0700436
Colin Cross6ade34f2017-09-15 13:00:47 -0700437 j.resourceJarFiles = append(j.resourceJarFiles, resourceJar)
Colin Cross0a6e0072017-08-30 14:24:55 -0700438 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700439 }
440
Colin Cross6ade34f2017-09-15 13:00:47 -0700441 // Propagate the resources from the transitive closure of static dependencies for copying
442 // into dex jars
443 j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...)
444
445 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700446 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700447
Colin Cross635acc92017-09-12 22:50:46 -0700448 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
449
Colin Cross0a6e0072017-08-30 14:24:55 -0700450 // Combine the classes built from sources, any manifests, and any static libraries into
451 // classes-combined.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700452 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700453
454 if j.properties.Jarjar_rules != nil {
455 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
456 // Transform classes-combined.jar into classes-jarjar.jar
457 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
458 if ctx.Failed() {
459 return
460 }
461 }
462
Colin Cross2fe66872015-03-30 17:20:39 -0700463 j.classpathFile = outputFile
464
Colin Crossa713a6f2017-09-20 18:04:44 -0700465 // TODO(ccross): handle hostdex
466 if ctx.Device() && len(srcFiles) > 0 {
Colin Cross540eff82017-06-22 17:01:52 -0700467 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700468 if false /* emma enabled */ {
469 // If you instrument class files that have local variable debug information in
470 // them emma does not correctly maintain the local variable table.
471 // This will cause an error when you try to convert the class files for Android.
472 // The workaround here is to build different dex file here based on emma switch
473 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
474 // option to remove local variable information
475 dxFlags = append(dxFlags, "--no-locals")
476 }
477
Colin Cross1332b002015-04-07 17:11:30 -0700478 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700479 dxFlags = append(dxFlags, "--no-optimize")
480 }
481
Colin Cross1332b002015-04-07 17:11:30 -0700482 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700483 dxFlags = append(dxFlags,
484 "--debug",
485 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700486 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700487 "--dump-width=1000")
488 }
489
Colin Cross595a4062017-08-31 12:30:37 -0700490 var minSdkVersion string
491 switch j.deviceProperties.Sdk_version {
492 case "", "current", "test_current", "system_current":
493 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
494 default:
495 minSdkVersion = j.deviceProperties.Sdk_version
496 }
497
498 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
499
Colin Cross2fe66872015-03-30 17:20:39 -0700500 flags.dxFlags = strings.Join(dxFlags, " ")
501
Colin Cross6ade34f2017-09-15 13:00:47 -0700502 desugarFlags := []string{
503 "--min_sdk_version " + minSdkVersion,
504 "--desugar_try_with_resources_if_needed=false",
505 "--allow_empty_bootclasspath",
506 }
507
508 if inList("--core-library", dxFlags) {
509 desugarFlags = append(desugarFlags, "--core_library")
510 }
511
512 flags.desugarFlags = strings.Join(desugarFlags, " ")
513
514 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700515 if ctx.Failed() {
516 return
517 }
518
Colin Cross6ade34f2017-09-15 13:00:47 -0700519 // TODO(ccross): For now, use the desugared jar as the classpath file. Eventually this
520 // might cause problems because desugar wants non-desugared jars in its class path.
521 j.classpathFile = desugarJar
522
523 // Compile classes.jar into classes.dex
524 dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
525 if ctx.Failed() {
526 return
527 }
528
529 jars := android.Paths{dexJarFile}
530 jars = append(jars, j.resourceJarFiles...)
531
532 outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true)
533
534 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700535 }
Colin Crossb7a63242015-04-16 14:09:14 -0700536 ctx.CheckbuildFile(outputFile)
537 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700538}
539
Colin Crossf506d872017-07-19 15:53:04 -0700540var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700541
Colin Cross74d73e22017-08-02 11:05:49 -0700542func (j *Module) ClasspathFiles() android.Paths {
543 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700544}
545
Colin Cross6ade34f2017-09-15 13:00:47 -0700546func (j *Module) ResourceJarFiles() android.Paths {
547 return j.resourceJarFiles
548}
549
Colin Cross46c9b8b2017-06-22 16:51:17 -0700550func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700551 return j.exportAidlIncludeDirs
552}
553
Colin Cross46c9b8b2017-06-22 16:51:17 -0700554var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700555
Colin Cross46c9b8b2017-06-22 16:51:17 -0700556func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700557 return j.logtagsSrcs
558}
559
Colin Cross2fe66872015-03-30 17:20:39 -0700560//
561// Java libraries (.jar file)
562//
563
Colin Crossf506d872017-07-19 15:53:04 -0700564type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700565 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700566}
567
Colin Crossf506d872017-07-19 15:53:04 -0700568func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700569 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700570
Colin Cross2c429dc2017-08-31 16:45:16 -0700571 if j.properties.Installable == nil || *j.properties.Installable == true {
572 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
573 ctx.ModuleName()+".jar", j.outputFile)
574 }
Colin Crossb7a63242015-04-16 14:09:14 -0700575}
576
Colin Crossf506d872017-07-19 15:53:04 -0700577func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700578 j.deps(ctx)
579}
580
Colin Crossf506d872017-07-19 15:53:04 -0700581func LibraryFactory() android.Module {
582 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700583
Colin Cross540eff82017-06-22 17:01:52 -0700584 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700585
Colin Cross36242852017-06-23 15:06:31 -0700586 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700587 &module.Module.properties,
588 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700589
Colin Cross89536d42017-07-07 14:35:50 -0700590 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700591 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700592}
593
Colin Crossf506d872017-07-19 15:53:04 -0700594func LibraryHostFactory() android.Module {
595 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700596
Colin Cross36242852017-06-23 15:06:31 -0700597 module.AddProperties(&module.Module.properties)
598
Colin Cross89536d42017-07-07 14:35:50 -0700599 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700600 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700601}
602
603//
604// Java Binaries (.jar file plus wrapper script)
605//
606
Colin Crossf506d872017-07-19 15:53:04 -0700607type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700608 // installable script to execute the resulting jar
609 Wrapper string
610}
611
Colin Crossf506d872017-07-19 15:53:04 -0700612type Binary struct {
613 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700614
Colin Crossf506d872017-07-19 15:53:04 -0700615 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700616
617 wrapperFile android.ModuleSrcPath
618 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700619}
620
Colin Crossf506d872017-07-19 15:53:04 -0700621func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
622 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700623
624 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
625 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700626 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700627 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
628 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700629}
630
Colin Crossf506d872017-07-19 15:53:04 -0700631func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700632 j.deps(ctx)
633}
634
Colin Crossf506d872017-07-19 15:53:04 -0700635func BinaryFactory() android.Module {
636 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700637
Colin Cross540eff82017-06-22 17:01:52 -0700638 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700639
Colin Cross36242852017-06-23 15:06:31 -0700640 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700641 &module.Module.properties,
642 &module.Module.deviceProperties,
643 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700644
Colin Cross89536d42017-07-07 14:35:50 -0700645 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700646 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700647}
648
Colin Crossf506d872017-07-19 15:53:04 -0700649func BinaryHostFactory() android.Module {
650 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700651
Colin Cross36242852017-06-23 15:06:31 -0700652 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700653 &module.Module.properties,
654 &module.Module.deviceProperties,
655 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700656
Colin Cross89536d42017-07-07 14:35:50 -0700657 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700658 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700659}
660
661//
662// Java prebuilts
663//
664
Colin Cross74d73e22017-08-02 11:05:49 -0700665type ImportProperties struct {
666 Jars []string
667}
668
669type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700670 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700671 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700672
Colin Cross74d73e22017-08-02 11:05:49 -0700673 properties ImportProperties
674
Colin Cross0a6e0072017-08-30 14:24:55 -0700675 classpathFiles android.Paths
676 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700677}
678
Colin Cross74d73e22017-08-02 11:05:49 -0700679func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700680 return &j.prebuilt
681}
682
Colin Cross74d73e22017-08-02 11:05:49 -0700683func (j *Import) PrebuiltSrcs() []string {
684 return j.properties.Jars
685}
686
687func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700688 return j.prebuilt.Name(j.ModuleBase.Name())
689}
690
Colin Cross74d73e22017-08-02 11:05:49 -0700691func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700692}
693
Colin Cross74d73e22017-08-02 11:05:49 -0700694func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
695 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700696
Colin Cross635acc92017-09-12 22:50:46 -0700697 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700698}
699
Colin Cross74d73e22017-08-02 11:05:49 -0700700var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700701
Colin Cross74d73e22017-08-02 11:05:49 -0700702func (j *Import) ClasspathFiles() android.Paths {
703 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700704}
705
Colin Cross6ade34f2017-09-15 13:00:47 -0700706func (j *Import) ResourceJarFiles() android.Paths {
707 // resources are in the ClasspathFiles
708 return nil
709}
710
Colin Cross74d73e22017-08-02 11:05:49 -0700711func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700712 return nil
713}
714
Colin Cross74d73e22017-08-02 11:05:49 -0700715var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700716
Colin Cross74d73e22017-08-02 11:05:49 -0700717func ImportFactory() android.Module {
718 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700719
Colin Cross74d73e22017-08-02 11:05:49 -0700720 module.AddProperties(&module.properties)
721
722 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700723 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
724 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700725}
726
Colin Cross74d73e22017-08-02 11:05:49 -0700727func ImportFactoryHost() android.Module {
728 module := &Import{}
729
730 module.AddProperties(&module.properties)
731
732 android.InitPrebuiltModule(module, &module.properties.Jars)
733 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
734 return module
735}
736
Colin Cross2fe66872015-03-30 17:20:39 -0700737func inList(s string, l []string) bool {
738 for _, e := range l {
739 if e == s {
740 return true
741 }
742 }
743 return false
744}
Colin Cross89536d42017-07-07 14:35:50 -0700745
746//
747// Defaults
748//
749type Defaults struct {
750 android.ModuleBase
751 android.DefaultsModuleBase
752}
753
754func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
755}
756
757func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
758}
759
760func defaultsFactory() android.Module {
761 return DefaultsFactory()
762}
763
764func DefaultsFactory(props ...interface{}) android.Module {
765 module := &Defaults{}
766
767 module.AddProperties(props...)
768 module.AddProperties(
769 &CompilerProperties{},
770 &CompilerDeviceProperties{},
771 )
772
773 android.InitDefaultsModule(module)
774
775 return module
776}