blob: 06ba44db87cab7fff8d1d3820fc5df72afe93a9f [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 Cross40a36712017-09-27 17:41:35 -0700429 resArgs, resDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
430
431 if len(resArgs) > 0 {
Colin Cross0a6e0072017-08-30 14:24:55 -0700432 // Combine classes + resources into classes-full-debug.jar
Colin Cross40a36712017-09-27 17:41:35 -0700433 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700434 if ctx.Failed() {
435 return
436 }
Colin Cross20978302015-04-10 17:05:07 -0700437
Colin Cross6ade34f2017-09-15 13:00:47 -0700438 j.resourceJarFiles = append(j.resourceJarFiles, resourceJar)
Colin Cross0a6e0072017-08-30 14:24:55 -0700439 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700440 }
441
Colin Cross6ade34f2017-09-15 13:00:47 -0700442 // Propagate the resources from the transitive closure of static dependencies for copying
443 // into dex jars
444 j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...)
445
446 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700447 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700448
Colin Cross635acc92017-09-12 22:50:46 -0700449 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
450
Colin Cross0a6e0072017-08-30 14:24:55 -0700451 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700452 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700453 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700454
455 if j.properties.Jarjar_rules != nil {
456 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700457 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700458 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
459 if ctx.Failed() {
460 return
461 }
462 }
463
Colin Cross2fe66872015-03-30 17:20:39 -0700464 j.classpathFile = outputFile
465
Colin Crossa713a6f2017-09-20 18:04:44 -0700466 // TODO(ccross): handle hostdex
Colin Cross59f1bb62017-09-27 17:59:10 -0700467 if ctx.Device() && len(srcFiles) > 0 && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700468 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700469 if false /* emma enabled */ {
470 // If you instrument class files that have local variable debug information in
471 // them emma does not correctly maintain the local variable table.
472 // This will cause an error when you try to convert the class files for Android.
473 // The workaround here is to build different dex file here based on emma switch
474 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
475 // option to remove local variable information
476 dxFlags = append(dxFlags, "--no-locals")
477 }
478
Colin Cross1332b002015-04-07 17:11:30 -0700479 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700480 dxFlags = append(dxFlags, "--no-optimize")
481 }
482
Colin Cross1332b002015-04-07 17:11:30 -0700483 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700484 dxFlags = append(dxFlags,
485 "--debug",
486 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700487 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700488 "--dump-width=1000")
489 }
490
Colin Cross595a4062017-08-31 12:30:37 -0700491 var minSdkVersion string
492 switch j.deviceProperties.Sdk_version {
493 case "", "current", "test_current", "system_current":
494 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
495 default:
496 minSdkVersion = j.deviceProperties.Sdk_version
497 }
498
499 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
500
Colin Cross2fe66872015-03-30 17:20:39 -0700501 flags.dxFlags = strings.Join(dxFlags, " ")
502
Colin Cross6ade34f2017-09-15 13:00:47 -0700503 desugarFlags := []string{
504 "--min_sdk_version " + minSdkVersion,
505 "--desugar_try_with_resources_if_needed=false",
506 "--allow_empty_bootclasspath",
507 }
508
509 if inList("--core-library", dxFlags) {
510 desugarFlags = append(desugarFlags, "--core_library")
511 }
512
513 flags.desugarFlags = strings.Join(desugarFlags, " ")
514
515 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700516 if ctx.Failed() {
517 return
518 }
519
Colin Cross6ade34f2017-09-15 13:00:47 -0700520 // Compile classes.jar into classes.dex
521 dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
522 if ctx.Failed() {
523 return
524 }
525
526 jars := android.Paths{dexJarFile}
527 jars = append(jars, j.resourceJarFiles...)
528
529 outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true)
530
531 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700532 }
Colin Crossb7a63242015-04-16 14:09:14 -0700533 ctx.CheckbuildFile(outputFile)
534 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700535}
536
Colin Cross59f1bb62017-09-27 17:59:10 -0700537func (j *Module) installable() bool {
538 return j.properties.Installable == nil || *j.properties.Installable
539}
540
Colin Crossf506d872017-07-19 15:53:04 -0700541var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700542
Colin Cross74d73e22017-08-02 11:05:49 -0700543func (j *Module) ClasspathFiles() android.Paths {
544 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700545}
546
Colin Cross6ade34f2017-09-15 13:00:47 -0700547func (j *Module) ResourceJarFiles() android.Paths {
548 return j.resourceJarFiles
549}
550
Colin Cross46c9b8b2017-06-22 16:51:17 -0700551func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700552 return j.exportAidlIncludeDirs
553}
554
Colin Cross46c9b8b2017-06-22 16:51:17 -0700555var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700556
Colin Cross46c9b8b2017-06-22 16:51:17 -0700557func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700558 return j.logtagsSrcs
559}
560
Colin Cross2fe66872015-03-30 17:20:39 -0700561//
562// Java libraries (.jar file)
563//
564
Colin Crossf506d872017-07-19 15:53:04 -0700565type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700566 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700567}
568
Colin Crossf506d872017-07-19 15:53:04 -0700569func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700570 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700571
Colin Cross59f1bb62017-09-27 17:59:10 -0700572 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700573 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
574 ctx.ModuleName()+".jar", j.outputFile)
575 }
Colin Crossb7a63242015-04-16 14:09:14 -0700576}
577
Colin Crossf506d872017-07-19 15:53:04 -0700578func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700579 j.deps(ctx)
580}
581
Colin Crossf506d872017-07-19 15:53:04 -0700582func LibraryFactory() android.Module {
583 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700584
Colin Cross540eff82017-06-22 17:01:52 -0700585 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700586
Colin Cross36242852017-06-23 15:06:31 -0700587 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700588 &module.Module.properties,
589 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700590
Colin Cross89536d42017-07-07 14:35:50 -0700591 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700592 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700593}
594
Colin Crossf506d872017-07-19 15:53:04 -0700595func LibraryHostFactory() android.Module {
596 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700597
Colin Cross36242852017-06-23 15:06:31 -0700598 module.AddProperties(&module.Module.properties)
599
Colin Cross89536d42017-07-07 14:35:50 -0700600 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700601 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700602}
603
604//
605// Java Binaries (.jar file plus wrapper script)
606//
607
Colin Crossf506d872017-07-19 15:53:04 -0700608type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700609 // installable script to execute the resulting jar
610 Wrapper string
611}
612
Colin Crossf506d872017-07-19 15:53:04 -0700613type Binary struct {
614 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700615
Colin Crossf506d872017-07-19 15:53:04 -0700616 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700617
618 wrapperFile android.ModuleSrcPath
619 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700620}
621
Colin Crossf506d872017-07-19 15:53:04 -0700622func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
623 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700624
625 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
626 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700627 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700628 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
629 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700630}
631
Colin Crossf506d872017-07-19 15:53:04 -0700632func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700633 j.deps(ctx)
634}
635
Colin Crossf506d872017-07-19 15:53:04 -0700636func BinaryFactory() android.Module {
637 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700638
Colin Cross540eff82017-06-22 17:01:52 -0700639 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700640
Colin Cross36242852017-06-23 15:06:31 -0700641 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700642 &module.Module.properties,
643 &module.Module.deviceProperties,
644 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700645
Colin Cross89536d42017-07-07 14:35:50 -0700646 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700647 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700648}
649
Colin Crossf506d872017-07-19 15:53:04 -0700650func BinaryHostFactory() android.Module {
651 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700652
Colin Cross36242852017-06-23 15:06:31 -0700653 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700654 &module.Module.properties,
655 &module.Module.deviceProperties,
656 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700657
Colin Cross89536d42017-07-07 14:35:50 -0700658 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700659 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700660}
661
662//
663// Java prebuilts
664//
665
Colin Cross74d73e22017-08-02 11:05:49 -0700666type ImportProperties struct {
667 Jars []string
668}
669
670type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700671 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700672 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700673
Colin Cross74d73e22017-08-02 11:05:49 -0700674 properties ImportProperties
675
Colin Cross0a6e0072017-08-30 14:24:55 -0700676 classpathFiles android.Paths
677 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700678}
679
Colin Cross74d73e22017-08-02 11:05:49 -0700680func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700681 return &j.prebuilt
682}
683
Colin Cross74d73e22017-08-02 11:05:49 -0700684func (j *Import) PrebuiltSrcs() []string {
685 return j.properties.Jars
686}
687
688func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700689 return j.prebuilt.Name(j.ModuleBase.Name())
690}
691
Colin Cross74d73e22017-08-02 11:05:49 -0700692func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700693}
694
Colin Cross74d73e22017-08-02 11:05:49 -0700695func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
696 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700697
Colin Cross635acc92017-09-12 22:50:46 -0700698 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700699}
700
Colin Cross74d73e22017-08-02 11:05:49 -0700701var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700702
Colin Cross74d73e22017-08-02 11:05:49 -0700703func (j *Import) ClasspathFiles() android.Paths {
704 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700705}
706
Colin Cross6ade34f2017-09-15 13:00:47 -0700707func (j *Import) ResourceJarFiles() android.Paths {
708 // resources are in the ClasspathFiles
709 return nil
710}
711
Colin Cross74d73e22017-08-02 11:05:49 -0700712func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700713 return nil
714}
715
Colin Cross74d73e22017-08-02 11:05:49 -0700716var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700717
Colin Cross74d73e22017-08-02 11:05:49 -0700718func ImportFactory() android.Module {
719 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700720
Colin Cross74d73e22017-08-02 11:05:49 -0700721 module.AddProperties(&module.properties)
722
723 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700724 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
725 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700726}
727
Colin Cross74d73e22017-08-02 11:05:49 -0700728func ImportFactoryHost() android.Module {
729 module := &Import{}
730
731 module.AddProperties(&module.properties)
732
733 android.InitPrebuiltModule(module, &module.properties.Jars)
734 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
735 return module
736}
737
Colin Cross2fe66872015-03-30 17:20:39 -0700738func inList(s string, l []string) bool {
739 for _, e := range l {
740 if e == s {
741 return true
742 }
743 }
744 return false
745}
Colin Cross89536d42017-07-07 14:35:50 -0700746
747//
748// Defaults
749//
750type Defaults struct {
751 android.ModuleBase
752 android.DefaultsModuleBase
753}
754
755func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
756}
757
758func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
759}
760
761func defaultsFactory() android.Module {
762 return DefaultsFactory()
763}
764
765func DefaultsFactory(props ...interface{}) android.Module {
766 module := &Defaults{}
767
768 module.AddProperties(props...)
769 module.AddProperties(
770 &CompilerProperties{},
771 &CompilerDeviceProperties{},
772 )
773
774 android.InitDefaultsModule(module)
775
776 return module
777}