blob: 805e06a17a33d6dd66cc662b3aaee43d754d511c [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
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
Paul Duffin2b67e3b2016-11-30 16:13:09 +000084 // don't build against the default libraries (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
88 // list of module-specific flags that will be used for javac compiles
89 Javacflags []string `android:"arch_variant"`
90
Colin Cross7d5136f2015-05-11 13:39:40 -070091 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070092 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070093
94 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070095 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070096
97 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070098 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070099
Colin Cross540eff82017-06-22 17:01:52 -0700100 // if not blank, run jarjar using the specified rules file
101 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -0700102
103 // If not blank, set the java version passed to javac as -source and -target
104 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700105
106 // If set to false, don't allow this module to be installed. Defaults to true.
107 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700108
Colin Cross0f37af02017-09-27 17:42:05 -0700109 // If set to true, include sources used to compile the module in to the final jar
110 Include_srcs *bool
111
Colin Cross32f676a2017-09-06 13:41:06 -0700112 // List of modules to use as annotation processors
113 Annotation_processors []string
114
115 // List of classes to pass to javac to use as annotation processors
116 Annotation_processor_classes []string
Colin Cross540eff82017-06-22 17:01:52 -0700117}
118
Colin Cross89536d42017-07-07 14:35:50 -0700119type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700120 // list of module-specific flags that will be used for dex compiles
121 Dxflags []string `android:"arch_variant"`
122
Colin Cross7d5136f2015-05-11 13:39:40 -0700123 // if not blank, set to the version of the sdk to compile against
124 Sdk_version string
125
126 // Set for device java libraries, and for host versions of device java libraries
127 // built for testing
128 Dex bool `blueprint:"mutated"`
129
Colin Cross7d5136f2015-05-11 13:39:40 -0700130 // directories to pass to aidl tool
131 Aidl_includes []string
132
133 // directories that should be added as include directories
134 // for any aidl sources of modules that depend on this module
135 Export_aidl_include_dirs []string
136}
137
Colin Cross46c9b8b2017-06-22 16:51:17 -0700138// Module contains the properties and members used by all java module types
139type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700140 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700141 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700142
Colin Cross89536d42017-07-07 14:35:50 -0700143 properties CompilerProperties
144 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700145
146 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700147 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700148
Colin Cross6ade34f2017-09-15 13:00:47 -0700149 // output file containing classes.dex
150 dexJarFile android.Path
151
152 // output files containing resources
153 resourceJarFiles android.Paths
154
Colin Crossb7a63242015-04-16 14:09:14 -0700155 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700156 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700157
Colin Cross635c3b02016-05-18 15:37:25 -0700158 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700159
Colin Cross635c3b02016-05-18 15:37:25 -0700160 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700161
Colin Crossb7a63242015-04-16 14:09:14 -0700162 // filelists of extra source files that should be included in the javac command line,
163 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700164 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700165
Colin Cross2fe66872015-03-30 17:20:39 -0700166 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700167 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700168}
169
Colin Crossf506d872017-07-19 15:53:04 -0700170type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700171 ClasspathFiles() android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700172 ResourceJarFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700173 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700174}
175
Colin Cross89536d42017-07-07 14:35:50 -0700176func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
177 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
178 android.InitDefaultableModule(module)
179}
180
Colin Crossbe1da472017-07-07 15:59:46 -0700181type dependencyTag struct {
182 blueprint.BaseDependencyTag
183 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700184}
185
Colin Crossbe1da472017-07-07 15:59:46 -0700186var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700187 staticLibTag = dependencyTag{name: "staticlib"}
188 libTag = dependencyTag{name: "javalib"}
189 bootClasspathTag = dependencyTag{name: "bootclasspath"}
190 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700191)
Colin Cross2fe66872015-03-30 17:20:39 -0700192
Colin Crossfc3674a2017-09-18 17:41:52 -0700193type sdkDep struct {
194 useModule, useFiles, useDefaultLibs bool
195 module string
196 jar android.Path
197 aidl android.Path
198}
199
200func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
201 switch v {
202 case "", "current", "system_current", "test_current":
203 // OK
204 default:
205 if _, err := strconv.Atoi(v); err != nil {
206 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
207 return sdkDep{}
208 }
209 }
210
211 toFile := func(v string) sdkDep {
212 dir := filepath.Join("prebuilts/sdk", v)
213 jar := filepath.Join(dir, "android.jar")
214 aidl := filepath.Join(dir, "framework.aidl")
215 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
216 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
217 if !jarPath.Valid() {
218 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
219 return sdkDep{}
220 }
221 if !aidlPath.Valid() {
222 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
223 return sdkDep{}
224 }
225 return sdkDep{
226 useFiles: true,
227 jar: jarPath.Path(),
228 aidl: aidlPath.Path(),
229 }
230 }
231
232 toModule := func(m string) sdkDep {
233 return sdkDep{
234 useModule: true,
235 module: m,
236 }
237 }
238
Colin Cross8b9d37b2017-09-22 15:30:06 -0700239 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700240 return toFile(v)
241 }
242
243 switch v {
244 case "":
245 return sdkDep{
246 useDefaultLibs: true,
247 }
248 case "current":
249 return toModule("android_stubs_current")
250 case "system_current":
251 return toModule("android_system_stubs_current")
252 case "test_current":
253 return toModule("android_test_stubs_current")
254 default:
255 return toFile(v)
256 }
257}
258
Colin Crossbe1da472017-07-07 15:59:46 -0700259func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700260 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700261 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700262 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
263 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700264 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Cross227d4362017-08-30 14:14:52 -0700265 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossfc3674a2017-09-18 17:41:52 -0700266 }
267 if sdkDep.useModule {
268 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700269 }
270 } else {
271 if j.deviceProperties.Dex {
Colin Crosscb2c9292017-09-23 19:57:16 -0700272 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700273 }
Colin Cross2fe66872015-03-30 17:20:39 -0700274 }
275 }
Colin Crossf506d872017-07-19 15:53:04 -0700276 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
277 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700278 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700279
280 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700281 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross2fe66872015-03-30 17:20:39 -0700282}
283
Colin Cross46c9b8b2017-06-22 16:51:17 -0700284func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700285 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700286
Colin Cross540eff82017-06-22 17:01:52 -0700287 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700288
289 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700290 if aidlPreprocess.Valid() {
291 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700292 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700293 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700294 }
295
Colin Cross635c3b02016-05-18 15:37:25 -0700296 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
297 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
298 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700299 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
300 flags = append(flags, "-I"+src.String())
301 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700302
Colin Crossf03c82b2015-04-13 13:53:40 -0700303 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700304}
305
Colin Cross32f676a2017-09-06 13:41:06 -0700306type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700307 classpath android.Paths
308 bootClasspath android.Paths
309 staticJars android.Paths
310 staticJarResources android.Paths
311 aidlIncludeDirs android.Paths
312 srcFileLists android.Paths
313 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700314}
Colin Cross2fe66872015-03-30 17:20:39 -0700315
Colin Cross32f676a2017-09-06 13:41:06 -0700316func (j *Module) collectDeps(ctx android.ModuleContext) deps {
317 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700318
319 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
320 if sdkDep.useFiles {
321 deps.classpath = append(deps.classpath, sdkDep.jar)
322 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
323 }
324
Colin Cross2fe66872015-03-30 17:20:39 -0700325 ctx.VisitDirectDeps(func(module blueprint.Module) {
326 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700327 tag := ctx.OtherModuleDependencyTag(module)
328
Colin Crossf506d872017-07-19 15:53:04 -0700329 dep, _ := module.(Dependency)
330 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700331 switch tag {
332 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700333 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700334 default:
335 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700336 }
Colin Crossec7a0422017-07-07 14:47:12 -0700337 return
338 }
339
Colin Crossbe1da472017-07-07 15:59:46 -0700340 switch tag {
341 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700342 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700343 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700344 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700345 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700346 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
347 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700348 deps.staticJarResources = append(deps.staticJarResources, dep.ResourceJarFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700349 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700350 if ctx.ModuleName() == "framework" {
351 // framework.jar has a one-off dependency on the R.java and Manifest.java files
352 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700353 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700354 }
Colin Crossbe1da472017-07-07 15:59:46 -0700355 default:
356 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700357 }
Colin Crossbe1da472017-07-07 15:59:46 -0700358
Colin Cross32f676a2017-09-06 13:41:06 -0700359 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700360 })
361
Colin Cross32f676a2017-09-06 13:41:06 -0700362 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700363}
364
Colin Cross46c9b8b2017-06-22 16:51:17 -0700365func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700366
Colin Cross540eff82017-06-22 17:01:52 -0700367 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700368
Colin Cross32f676a2017-09-06 13:41:06 -0700369 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700370
Colin Crossf03c82b2015-04-13 13:53:40 -0700371 var flags javaBuilderFlags
372
373 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700374 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
375 javacFlags = config.StripJavac9Flags(javacFlags)
376 }
Colin Cross64162712017-08-08 13:17:59 -0700377
378 if j.properties.Java_version != nil {
379 flags.javaVersion = *j.properties.Java_version
380 } else {
381 flags.javaVersion = "${config.DefaultJavaVersion}"
382 }
383
Colin Cross6ade34f2017-09-15 13:00:47 -0700384 flags.bootClasspath.AddPaths(deps.bootClasspath)
385 flags.classpath.AddPaths(deps.classpath)
386
Colin Crossf03c82b2015-04-13 13:53:40 -0700387 if len(javacFlags) > 0 {
388 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
389 flags.javacFlags = "$javacFlags"
390 }
391
Colin Cross32f676a2017-09-06 13:41:06 -0700392 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700393 if len(aidlFlags) > 0 {
394 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
395 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700396 }
397
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700398 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700399
Colin Crossf05fe972015-04-10 17:45:20 -0700400 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700401
Colin Cross0607cf72015-04-28 13:28:51 -0700402 ctx.VisitDirectDeps(func(module blueprint.Module) {
403 if gen, ok := module.(genrule.SourceFileGenerator); ok {
404 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
405 }
406 })
407
Colin Cross32f676a2017-09-06 13:41:06 -0700408 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700409
Colin Cross0a6e0072017-08-30 14:24:55 -0700410 var jars android.Paths
411
Colin Cross8cf13342015-04-10 15:41:49 -0700412 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700413 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700414 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
415 // If error-prone is enabled, add an additional rule to compile the java files into
416 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700417 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700418 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
419 // enable error-prone without affecting the output class files.
Colin Crossd6891432017-09-27 17:39:56 -0700420 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, nil)
Colin Crossc6bbef32017-08-14 14:16:06 -0700421 extraJarDeps = append(extraJarDeps, errorprone)
422 }
423
Colin Crossd6891432017-09-27 17:39:56 -0700424 // Compile java sources into .class files
425 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraJarDeps)
426 if ctx.Failed() {
427 return
428 }
429
Colin Cross0a6e0072017-08-30 14:24:55 -0700430 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700431 }
432
Colin Cross0f37af02017-09-27 17:42:05 -0700433 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
434 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
435
436 var resArgs []string
437 var resDeps android.Paths
438
439 resArgs = append(resArgs, dirArgs...)
440 resDeps = append(resDeps, dirDeps...)
441
442 resArgs = append(resArgs, fileArgs...)
443 resDeps = append(resDeps, fileDeps...)
444
445 if proptools.Bool(j.properties.Include_srcs) {
446 srcArgs, srcDeps := ResourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
447 resArgs = append(resArgs, srcArgs...)
448 resDeps = append(resDeps, srcDeps...)
449 }
Colin Cross40a36712017-09-27 17:41:35 -0700450
451 if len(resArgs) > 0 {
Colin Cross0a6e0072017-08-30 14:24:55 -0700452 // Combine classes + resources into classes-full-debug.jar
Colin Cross40a36712017-09-27 17:41:35 -0700453 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700454 if ctx.Failed() {
455 return
456 }
Colin Cross20978302015-04-10 17:05:07 -0700457
Colin Cross6ade34f2017-09-15 13:00:47 -0700458 j.resourceJarFiles = append(j.resourceJarFiles, resourceJar)
Colin Cross0a6e0072017-08-30 14:24:55 -0700459 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700460 }
461
Colin Cross6ade34f2017-09-15 13:00:47 -0700462 // Propagate the resources from the transitive closure of static dependencies for copying
463 // into dex jars
464 j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...)
465
466 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700467 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700468
Colin Cross635acc92017-09-12 22:50:46 -0700469 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
470
Colin Cross0a6e0072017-08-30 14:24:55 -0700471 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700472 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700473 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700474
475 if j.properties.Jarjar_rules != nil {
476 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700477 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700478 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
479 if ctx.Failed() {
480 return
481 }
482 }
483
Colin Cross2fe66872015-03-30 17:20:39 -0700484 j.classpathFile = outputFile
485
Colin Crossa713a6f2017-09-20 18:04:44 -0700486 // TODO(ccross): handle hostdex
Colin Cross59f1bb62017-09-27 17:59:10 -0700487 if ctx.Device() && len(srcFiles) > 0 && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700488 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700489 if false /* emma enabled */ {
490 // If you instrument class files that have local variable debug information in
491 // them emma does not correctly maintain the local variable table.
492 // This will cause an error when you try to convert the class files for Android.
493 // The workaround here is to build different dex file here based on emma switch
494 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
495 // option to remove local variable information
496 dxFlags = append(dxFlags, "--no-locals")
497 }
498
Colin Cross1332b002015-04-07 17:11:30 -0700499 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700500 dxFlags = append(dxFlags, "--no-optimize")
501 }
502
Colin Cross1332b002015-04-07 17:11:30 -0700503 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700504 dxFlags = append(dxFlags,
505 "--debug",
506 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700507 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700508 "--dump-width=1000")
509 }
510
Colin Cross595a4062017-08-31 12:30:37 -0700511 var minSdkVersion string
512 switch j.deviceProperties.Sdk_version {
513 case "", "current", "test_current", "system_current":
514 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
515 default:
516 minSdkVersion = j.deviceProperties.Sdk_version
517 }
518
519 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
520
Colin Cross2fe66872015-03-30 17:20:39 -0700521 flags.dxFlags = strings.Join(dxFlags, " ")
522
Colin Cross6ade34f2017-09-15 13:00:47 -0700523 desugarFlags := []string{
524 "--min_sdk_version " + minSdkVersion,
525 "--desugar_try_with_resources_if_needed=false",
526 "--allow_empty_bootclasspath",
527 }
528
529 if inList("--core-library", dxFlags) {
530 desugarFlags = append(desugarFlags, "--core_library")
531 }
532
533 flags.desugarFlags = strings.Join(desugarFlags, " ")
534
535 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700536 if ctx.Failed() {
537 return
538 }
539
Colin Cross6ade34f2017-09-15 13:00:47 -0700540 // Compile classes.jar into classes.dex
541 dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
542 if ctx.Failed() {
543 return
544 }
545
546 jars := android.Paths{dexJarFile}
547 jars = append(jars, j.resourceJarFiles...)
548
549 outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true)
550
551 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700552 }
Colin Crossb7a63242015-04-16 14:09:14 -0700553 ctx.CheckbuildFile(outputFile)
554 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700555}
556
Colin Cross59f1bb62017-09-27 17:59:10 -0700557func (j *Module) installable() bool {
558 return j.properties.Installable == nil || *j.properties.Installable
559}
560
Colin Crossf506d872017-07-19 15:53:04 -0700561var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700562
Colin Cross74d73e22017-08-02 11:05:49 -0700563func (j *Module) ClasspathFiles() android.Paths {
564 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700565}
566
Colin Cross6ade34f2017-09-15 13:00:47 -0700567func (j *Module) ResourceJarFiles() android.Paths {
568 return j.resourceJarFiles
569}
570
Colin Cross46c9b8b2017-06-22 16:51:17 -0700571func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700572 return j.exportAidlIncludeDirs
573}
574
Colin Cross46c9b8b2017-06-22 16:51:17 -0700575var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700576
Colin Cross46c9b8b2017-06-22 16:51:17 -0700577func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700578 return j.logtagsSrcs
579}
580
Colin Cross2fe66872015-03-30 17:20:39 -0700581//
582// Java libraries (.jar file)
583//
584
Colin Crossf506d872017-07-19 15:53:04 -0700585type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700586 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700587}
588
Colin Crossf506d872017-07-19 15:53:04 -0700589func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700590 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700591
Colin Cross59f1bb62017-09-27 17:59:10 -0700592 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700593 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
594 ctx.ModuleName()+".jar", j.outputFile)
595 }
Colin Crossb7a63242015-04-16 14:09:14 -0700596}
597
Colin Crossf506d872017-07-19 15:53:04 -0700598func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700599 j.deps(ctx)
600}
601
Colin Crossf506d872017-07-19 15:53:04 -0700602func LibraryFactory() android.Module {
603 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700604
Colin Cross540eff82017-06-22 17:01:52 -0700605 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700606
Colin Cross36242852017-06-23 15:06:31 -0700607 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700608 &module.Module.properties,
609 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700610
Colin Cross89536d42017-07-07 14:35:50 -0700611 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700612 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700613}
614
Colin Crossf506d872017-07-19 15:53:04 -0700615func LibraryHostFactory() android.Module {
616 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700617
Colin Cross36242852017-06-23 15:06:31 -0700618 module.AddProperties(&module.Module.properties)
619
Colin Cross89536d42017-07-07 14:35:50 -0700620 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700621 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700622}
623
624//
625// Java Binaries (.jar file plus wrapper script)
626//
627
Colin Crossf506d872017-07-19 15:53:04 -0700628type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700629 // installable script to execute the resulting jar
630 Wrapper string
631}
632
Colin Crossf506d872017-07-19 15:53:04 -0700633type Binary struct {
634 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700635
Colin Crossf506d872017-07-19 15:53:04 -0700636 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700637
638 wrapperFile android.ModuleSrcPath
639 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700640}
641
Colin Crossf506d872017-07-19 15:53:04 -0700642func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
643 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700644
645 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
646 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700647 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700648 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
649 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700650}
651
Colin Crossf506d872017-07-19 15:53:04 -0700652func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700653 j.deps(ctx)
654}
655
Colin Crossf506d872017-07-19 15:53:04 -0700656func BinaryFactory() android.Module {
657 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700658
Colin Cross540eff82017-06-22 17:01:52 -0700659 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700660
Colin Cross36242852017-06-23 15:06:31 -0700661 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700662 &module.Module.properties,
663 &module.Module.deviceProperties,
664 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700665
Colin Cross89536d42017-07-07 14:35:50 -0700666 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700667 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700668}
669
Colin Crossf506d872017-07-19 15:53:04 -0700670func BinaryHostFactory() android.Module {
671 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700672
Colin Cross36242852017-06-23 15:06:31 -0700673 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700674 &module.Module.properties,
675 &module.Module.deviceProperties,
676 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700677
Colin Cross89536d42017-07-07 14:35:50 -0700678 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700679 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700680}
681
682//
683// Java prebuilts
684//
685
Colin Cross74d73e22017-08-02 11:05:49 -0700686type ImportProperties struct {
687 Jars []string
688}
689
690type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700691 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700692 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700693
Colin Cross74d73e22017-08-02 11:05:49 -0700694 properties ImportProperties
695
Colin Cross0a6e0072017-08-30 14:24:55 -0700696 classpathFiles android.Paths
697 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700698}
699
Colin Cross74d73e22017-08-02 11:05:49 -0700700func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700701 return &j.prebuilt
702}
703
Colin Cross74d73e22017-08-02 11:05:49 -0700704func (j *Import) PrebuiltSrcs() []string {
705 return j.properties.Jars
706}
707
708func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700709 return j.prebuilt.Name(j.ModuleBase.Name())
710}
711
Colin Cross74d73e22017-08-02 11:05:49 -0700712func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700713}
714
Colin Cross74d73e22017-08-02 11:05:49 -0700715func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
716 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700717
Colin Cross635acc92017-09-12 22:50:46 -0700718 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700719}
720
Colin Cross74d73e22017-08-02 11:05:49 -0700721var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700722
Colin Cross74d73e22017-08-02 11:05:49 -0700723func (j *Import) ClasspathFiles() android.Paths {
724 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700725}
726
Colin Cross6ade34f2017-09-15 13:00:47 -0700727func (j *Import) ResourceJarFiles() android.Paths {
728 // resources are in the ClasspathFiles
729 return nil
730}
731
Colin Cross74d73e22017-08-02 11:05:49 -0700732func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700733 return nil
734}
735
Colin Cross74d73e22017-08-02 11:05:49 -0700736var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700737
Colin Cross74d73e22017-08-02 11:05:49 -0700738func ImportFactory() android.Module {
739 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700740
Colin Cross74d73e22017-08-02 11:05:49 -0700741 module.AddProperties(&module.properties)
742
743 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700744 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
745 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700746}
747
Colin Cross74d73e22017-08-02 11:05:49 -0700748func ImportFactoryHost() android.Module {
749 module := &Import{}
750
751 module.AddProperties(&module.properties)
752
753 android.InitPrebuiltModule(module, &module.properties.Jars)
754 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
755 return module
756}
757
Colin Cross2fe66872015-03-30 17:20:39 -0700758func inList(s string, l []string) bool {
759 for _, e := range l {
760 if e == s {
761 return true
762 }
763 }
764 return false
765}
Colin Cross89536d42017-07-07 14:35:50 -0700766
767//
768// Defaults
769//
770type Defaults struct {
771 android.ModuleBase
772 android.DefaultsModuleBase
773}
774
775func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
776}
777
778func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
779}
780
781func defaultsFactory() android.Module {
782 return DefaultsFactory()
783}
784
785func DefaultsFactory(props ...interface{}) android.Module {
786 module := &Defaults{}
787
788 module.AddProperties(props...)
789 module.AddProperties(
790 &CompilerProperties{},
791 &CompilerDeviceProperties{},
792 )
793
794 android.InitDefaultsModule(module)
795
796 return module
797}