blob: b4ce35ede4396c6522ae3c18571f5a19aca1ac18 [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 Cross74d73e22017-08-02 11:05:49 -070023 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070030 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Cross89536d42017-07-07 14:35:50 -070035 android.RegisterModuleType("java_defaults", defaultsFactory)
36
Colin Crossf506d872017-07-19 15:53:04 -070037 android.RegisterModuleType("java_library", LibraryFactory)
38 android.RegisterModuleType("java_library_static", LibraryFactory)
39 android.RegisterModuleType("java_library_host", LibraryHostFactory)
40 android.RegisterModuleType("java_binary", BinaryFactory)
41 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070042 android.RegisterModuleType("java_import", ImportFactory)
43 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Crosse8dc34a2017-07-19 11:22:16 -070044 android.RegisterModuleType("android_prebuilt_sdk", SdkPrebuiltFactory)
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 Crosse8dc34a2017-07-19 11:22:16 -070073 Resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070074
Colin Crosse8dc34a2017-07-19 11:22:16 -070075 // list of directories that should be excluded from resource_dirs
76 Exclude_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 Crossb7a63242015-04-16 14:09:14 -0700140 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700141 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700142
Colin Cross635c3b02016-05-18 15:37:25 -0700143 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700144
Colin Cross635c3b02016-05-18 15:37:25 -0700145 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700146
Colin Crossb7a63242015-04-16 14:09:14 -0700147 // filelists of extra source files that should be included in the javac command line,
148 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700149 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700150
Colin Cross2fe66872015-03-30 17:20:39 -0700151 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700152 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700153}
154
Colin Crossf506d872017-07-19 15:53:04 -0700155type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700156 ClasspathFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700157 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700158}
159
Colin Cross89536d42017-07-07 14:35:50 -0700160func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
161 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
162 android.InitDefaultableModule(module)
163}
164
Colin Crossbe1da472017-07-07 15:59:46 -0700165type dependencyTag struct {
166 blueprint.BaseDependencyTag
167 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700168}
169
Colin Crossbe1da472017-07-07 15:59:46 -0700170var (
Colin Cross32f676a2017-09-06 13:41:06 -0700171 staticLibTag = dependencyTag{name: "staticlib"}
172 libTag = dependencyTag{name: "javalib"}
173 bootClasspathTag = dependencyTag{name: "bootclasspath"}
174 frameworkResTag = dependencyTag{name: "framework-res"}
175 sdkDependencyTag = dependencyTag{name: "sdk"}
176 annotationProcessorTag = dependencyTag{name: "annotation processor"}
Colin Crossbe1da472017-07-07 15:59:46 -0700177)
Colin Cross2fe66872015-03-30 17:20:39 -0700178
Colin Crossbe1da472017-07-07 15:59:46 -0700179func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700180 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700181 if ctx.Device() {
182 switch j.deviceProperties.Sdk_version {
183 case "":
Colin Cross227d4362017-08-30 14:14:52 -0700184 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
185 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700186 case "current":
187 // TODO: !TARGET_BUILD_APPS
188 // TODO: export preprocessed framework.aidl from android_stubs_current
189 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
Colin Cross227d4362017-08-30 14:14:52 -0700190 case "test_current":
191 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_test_stubs_current")
Colin Crossbe1da472017-07-07 15:59:46 -0700192 case "system_current":
193 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
194 default:
195 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
196 }
197 } else {
198 if j.deviceProperties.Dex {
Colin Cross227d4362017-08-30 14:14:52 -0700199 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
Colin Crossbe1da472017-07-07 15:59:46 -0700200 }
Colin Cross2fe66872015-03-30 17:20:39 -0700201 }
202 }
Colin Crossf506d872017-07-19 15:53:04 -0700203 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
204 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross32f676a2017-09-06 13:41:06 -0700205 ctx.AddDependency(ctx.Module(), annotationProcessorTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700206
207 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross2fe66872015-03-30 17:20:39 -0700208}
209
Colin Cross46c9b8b2017-06-22 16:51:17 -0700210func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700211 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700212
Colin Cross540eff82017-06-22 17:01:52 -0700213 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700214
215 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700216 if aidlPreprocess.Valid() {
217 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700218 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700219 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700220 }
221
Colin Cross635c3b02016-05-18 15:37:25 -0700222 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
223 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
224 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700225 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
226 flags = append(flags, "-I"+src.String())
227 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700228
Colin Crossf03c82b2015-04-13 13:53:40 -0700229 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700230}
231
Colin Cross32f676a2017-09-06 13:41:06 -0700232type deps struct {
233 classpath android.Paths
234 bootClasspath android.Paths
235 staticJars android.Paths
236 aidlIncludeDirs android.Paths
237 srcFileLists android.Paths
238 annotationProcessors android.Paths
239 aidlPreprocess android.OptionalPath
240}
Colin Cross2fe66872015-03-30 17:20:39 -0700241
Colin Cross32f676a2017-09-06 13:41:06 -0700242func (j *Module) collectDeps(ctx android.ModuleContext) deps {
243 var deps deps
Colin Cross2fe66872015-03-30 17:20:39 -0700244 ctx.VisitDirectDeps(func(module blueprint.Module) {
245 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700246 tag := ctx.OtherModuleDependencyTag(module)
247
Colin Crossf506d872017-07-19 15:53:04 -0700248 dep, _ := module.(Dependency)
249 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700250 switch tag {
251 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700252 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700253 default:
254 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700255 }
Colin Crossec7a0422017-07-07 14:47:12 -0700256 return
257 }
258
Colin Crossbe1da472017-07-07 15:59:46 -0700259 switch tag {
260 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700261 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700262 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700263 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700264 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700265 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
266 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
267 case annotationProcessorTag:
268 deps.annotationProcessors = append(deps.annotationProcessors, dep.ClasspathFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700269 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700270 if ctx.ModuleName() == "framework" {
271 // framework.jar has a one-off dependency on the R.java and Manifest.java files
272 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700273 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700274 }
Colin Crossbe1da472017-07-07 15:59:46 -0700275 case sdkDependencyTag:
276 sdkDep := module.(sdkDependency)
Colin Cross32f676a2017-09-06 13:41:06 -0700277 deps.bootClasspath = append(deps.bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700278 if sdkDep.AidlPreprocessed().Valid() {
Colin Cross32f676a2017-09-06 13:41:06 -0700279 if deps.aidlPreprocess.Valid() {
Colin Crossec7a0422017-07-07 14:47:12 -0700280 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
Colin Cross32f676a2017-09-06 13:41:06 -0700281 deps.aidlPreprocess, sdkDep.AidlPreprocessed())
Colin Crossec7a0422017-07-07 14:47:12 -0700282 } else {
Colin Cross32f676a2017-09-06 13:41:06 -0700283 deps.aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700284 }
285 }
Colin Crossbe1da472017-07-07 15:59:46 -0700286 default:
287 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700288 }
Colin Crossbe1da472017-07-07 15:59:46 -0700289
Colin Cross32f676a2017-09-06 13:41:06 -0700290 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700291 })
292
Colin Cross32f676a2017-09-06 13:41:06 -0700293 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700294}
295
Colin Cross46c9b8b2017-06-22 16:51:17 -0700296func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700297
Colin Cross540eff82017-06-22 17:01:52 -0700298 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700299
Colin Cross32f676a2017-09-06 13:41:06 -0700300 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700301
Colin Crossf03c82b2015-04-13 13:53:40 -0700302 var flags javaBuilderFlags
303
304 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700305 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
306 javacFlags = config.StripJavac9Flags(javacFlags)
307 }
Colin Cross64162712017-08-08 13:17:59 -0700308
Colin Cross32f676a2017-09-06 13:41:06 -0700309 if len(deps.annotationProcessors) > 0 {
310 javacFlags = append(javacFlags,
311 "-processorpath "+strings.Join(deps.annotationProcessors.Strings(), ":"))
312 }
313
314 for _, c := range j.properties.Annotation_processor_classes {
315 javacFlags = append(javacFlags, "-processor "+c)
316 }
317
Colin Cross64162712017-08-08 13:17:59 -0700318 if j.properties.Java_version != nil {
319 flags.javaVersion = *j.properties.Java_version
320 } else {
321 flags.javaVersion = "${config.DefaultJavaVersion}"
322 }
323
Colin Crossf03c82b2015-04-13 13:53:40 -0700324 if len(javacFlags) > 0 {
325 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
326 flags.javacFlags = "$javacFlags"
327 }
328
Colin Cross32f676a2017-09-06 13:41:06 -0700329 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700330 if len(aidlFlags) > 0 {
331 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
332 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700333 }
334
Colin Cross32f676a2017-09-06 13:41:06 -0700335 var extraDeps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700336
Colin Cross32f676a2017-09-06 13:41:06 -0700337 if len(deps.bootClasspath) > 0 {
338 flags.bootClasspath = "-bootclasspath " + strings.Join(deps.bootClasspath.Strings(), ":")
339 extraDeps = append(extraDeps, deps.bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700340 } else if ctx.Device() {
341 // Explicitly clear the bootclasspath for device builds
342 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700343 }
344
Colin Cross32f676a2017-09-06 13:41:06 -0700345 if len(deps.classpath) > 0 {
346 flags.classpath = "-classpath " + strings.Join(deps.classpath.Strings(), ":")
347 extraDeps = append(extraDeps, deps.classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700348 }
349
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700350 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700351
Colin Crossf05fe972015-04-10 17:45:20 -0700352 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700353
Colin Cross0607cf72015-04-28 13:28:51 -0700354 ctx.VisitDirectDeps(func(module blueprint.Module) {
355 if gen, ok := module.(genrule.SourceFileGenerator); ok {
356 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
357 }
358 })
359
Colin Cross32f676a2017-09-06 13:41:06 -0700360 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700361
Colin Crossc6bbef32017-08-14 14:16:06 -0700362 var extraJarDeps android.Paths
363
Colin Cross0a6e0072017-08-30 14:24:55 -0700364 var jars android.Paths
365
Colin Cross8cf13342015-04-10 15:41:49 -0700366 if len(srcFiles) > 0 {
367 // Compile java sources into .class files
Colin Cross32f676a2017-09-06 13:41:06 -0700368 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
Colin Cross8cf13342015-04-10 15:41:49 -0700369 if ctx.Failed() {
370 return
371 }
372
Colin Crossc6bbef32017-08-14 14:16:06 -0700373 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
374 // If error-prone is enabled, add an additional rule to compile the java files into
375 // a separate set of classes (so that they don't overwrite the normal ones and require
376 // a rebuild when error-prone is turned off). Add the classes as a dependency to
377 // the jar command so the two compiles can run in parallel.
378 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
379 // enable error-prone without affecting the output class files.
Colin Cross32f676a2017-09-06 13:41:06 -0700380 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
Colin Crossc6bbef32017-08-14 14:16:06 -0700381 extraJarDeps = append(extraJarDeps, errorprone)
382 }
383
Colin Cross0a6e0072017-08-30 14:24:55 -0700384 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700385 }
386
Colin Cross0a6e0072017-08-30 14:24:55 -0700387 resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs)
Colin Cross635c3b02016-05-18 15:37:25 -0700388 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700389
Colin Cross0a6e0072017-08-30 14:24:55 -0700390 if len(resourceJarSpecs) > 0 || manifest.Valid() {
391 // Combine classes + resources into classes-full-debug.jar
392 resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, manifest, extraJarDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700393 if ctx.Failed() {
394 return
395 }
Colin Cross20978302015-04-10 17:05:07 -0700396
Colin Cross0a6e0072017-08-30 14:24:55 -0700397 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700398 }
399
Colin Cross32f676a2017-09-06 13:41:06 -0700400 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700401
402 // Combine the classes built from sources, any manifests, and any static libraries into
403 // classes-combined.jar. If there is only one input jar this step will be skipped.
404 outputFile := TransformJarsToJar(ctx, "classes-combined.jar", jars)
405
406 if j.properties.Jarjar_rules != nil {
407 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
408 // Transform classes-combined.jar into classes-jarjar.jar
409 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
410 if ctx.Failed() {
411 return
412 }
413 }
414
Colin Cross2fe66872015-03-30 17:20:39 -0700415 j.classpathFile = outputFile
416
Colin Cross540eff82017-06-22 17:01:52 -0700417 if j.deviceProperties.Dex && len(srcFiles) > 0 {
418 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700419 if false /* emma enabled */ {
420 // If you instrument class files that have local variable debug information in
421 // them emma does not correctly maintain the local variable table.
422 // This will cause an error when you try to convert the class files for Android.
423 // The workaround here is to build different dex file here based on emma switch
424 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
425 // option to remove local variable information
426 dxFlags = append(dxFlags, "--no-locals")
427 }
428
Colin Cross1332b002015-04-07 17:11:30 -0700429 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700430 dxFlags = append(dxFlags, "--no-optimize")
431 }
432
Colin Cross1332b002015-04-07 17:11:30 -0700433 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700434 dxFlags = append(dxFlags,
435 "--debug",
436 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700437 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700438 "--dump-width=1000")
439 }
440
Colin Cross595a4062017-08-31 12:30:37 -0700441 var minSdkVersion string
442 switch j.deviceProperties.Sdk_version {
443 case "", "current", "test_current", "system_current":
444 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
445 default:
446 minSdkVersion = j.deviceProperties.Sdk_version
447 }
448
449 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
450
Colin Cross2fe66872015-03-30 17:20:39 -0700451 flags.dxFlags = strings.Join(dxFlags, " ")
452
453 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700454 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700455 if ctx.Failed() {
456 return
457 }
458
459 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700460 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700461 }
Colin Crossb7a63242015-04-16 14:09:14 -0700462 ctx.CheckbuildFile(outputFile)
463 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700464}
465
Colin Crossf506d872017-07-19 15:53:04 -0700466var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700467
Colin Cross74d73e22017-08-02 11:05:49 -0700468func (j *Module) ClasspathFiles() android.Paths {
469 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700470}
471
Colin Cross46c9b8b2017-06-22 16:51:17 -0700472func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700473 return j.exportAidlIncludeDirs
474}
475
Colin Cross46c9b8b2017-06-22 16:51:17 -0700476var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700477
Colin Cross46c9b8b2017-06-22 16:51:17 -0700478func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700479 return j.logtagsSrcs
480}
481
Colin Cross2fe66872015-03-30 17:20:39 -0700482//
483// Java libraries (.jar file)
484//
485
Colin Crossf506d872017-07-19 15:53:04 -0700486type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700487 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700488}
489
Colin Crossf506d872017-07-19 15:53:04 -0700490func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700491 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700492
Colin Cross2c429dc2017-08-31 16:45:16 -0700493 if j.properties.Installable == nil || *j.properties.Installable == true {
494 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
495 ctx.ModuleName()+".jar", j.outputFile)
496 }
Colin Crossb7a63242015-04-16 14:09:14 -0700497}
498
Colin Crossf506d872017-07-19 15:53:04 -0700499func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700500 j.deps(ctx)
501}
502
Colin Crossf506d872017-07-19 15:53:04 -0700503func LibraryFactory() android.Module {
504 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700505
Colin Cross540eff82017-06-22 17:01:52 -0700506 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700507
Colin Cross36242852017-06-23 15:06:31 -0700508 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700509 &module.Module.properties,
510 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700511
Colin Cross89536d42017-07-07 14:35:50 -0700512 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700513 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700514}
515
Colin Crossf506d872017-07-19 15:53:04 -0700516func LibraryHostFactory() android.Module {
517 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700518
Colin Cross36242852017-06-23 15:06:31 -0700519 module.AddProperties(&module.Module.properties)
520
Colin Cross89536d42017-07-07 14:35:50 -0700521 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700522 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700523}
524
525//
526// Java Binaries (.jar file plus wrapper script)
527//
528
Colin Crossf506d872017-07-19 15:53:04 -0700529type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700530 // installable script to execute the resulting jar
531 Wrapper string
532}
533
Colin Crossf506d872017-07-19 15:53:04 -0700534type Binary struct {
535 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700536
Colin Crossf506d872017-07-19 15:53:04 -0700537 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700538
539 wrapperFile android.ModuleSrcPath
540 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700541}
542
Colin Crossf506d872017-07-19 15:53:04 -0700543func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
544 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700545
546 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
547 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700548 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700549 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
550 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700551}
552
Colin Crossf506d872017-07-19 15:53:04 -0700553func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700554 j.deps(ctx)
555}
556
Colin Crossf506d872017-07-19 15:53:04 -0700557func BinaryFactory() android.Module {
558 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700559
Colin Cross540eff82017-06-22 17:01:52 -0700560 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700561
Colin Cross36242852017-06-23 15:06:31 -0700562 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700563 &module.Module.properties,
564 &module.Module.deviceProperties,
565 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700566
Colin Cross89536d42017-07-07 14:35:50 -0700567 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700568 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700569}
570
Colin Crossf506d872017-07-19 15:53:04 -0700571func BinaryHostFactory() android.Module {
572 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700573
Colin Cross36242852017-06-23 15:06:31 -0700574 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700575 &module.Module.properties,
576 &module.Module.deviceProperties,
577 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700578
Colin Cross89536d42017-07-07 14:35:50 -0700579 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700580 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700581}
582
583//
584// Java prebuilts
585//
586
Colin Cross74d73e22017-08-02 11:05:49 -0700587type ImportProperties struct {
588 Jars []string
589}
590
591type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700592 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700593 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700594
Colin Cross74d73e22017-08-02 11:05:49 -0700595 properties ImportProperties
596
Colin Cross0a6e0072017-08-30 14:24:55 -0700597 classpathFiles android.Paths
598 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700599}
600
Colin Cross74d73e22017-08-02 11:05:49 -0700601func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700602 return &j.prebuilt
603}
604
Colin Cross74d73e22017-08-02 11:05:49 -0700605func (j *Import) PrebuiltSrcs() []string {
606 return j.properties.Jars
607}
608
609func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700610 return j.prebuilt.Name(j.ModuleBase.Name())
611}
612
Colin Cross74d73e22017-08-02 11:05:49 -0700613func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700614}
615
Colin Cross74d73e22017-08-02 11:05:49 -0700616func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
617 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700618
Colin Cross0a6e0072017-08-30 14:24:55 -0700619 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles)
Colin Cross2fe66872015-03-30 17:20:39 -0700620}
621
Colin Cross74d73e22017-08-02 11:05:49 -0700622var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700623
Colin Cross74d73e22017-08-02 11:05:49 -0700624func (j *Import) ClasspathFiles() android.Paths {
625 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700626}
627
Colin Cross74d73e22017-08-02 11:05:49 -0700628func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700629 return nil
630}
631
Colin Cross74d73e22017-08-02 11:05:49 -0700632var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700633
Colin Cross74d73e22017-08-02 11:05:49 -0700634func ImportFactory() android.Module {
635 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700636
Colin Cross74d73e22017-08-02 11:05:49 -0700637 module.AddProperties(&module.properties)
638
639 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700640 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
641 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700642}
643
Colin Cross74d73e22017-08-02 11:05:49 -0700644func ImportFactoryHost() android.Module {
645 module := &Import{}
646
647 module.AddProperties(&module.properties)
648
649 android.InitPrebuiltModule(module, &module.properties.Jars)
650 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
651 return module
652}
653
Colin Crossaa8630b2015-04-13 13:52:22 -0700654//
655// SDK java prebuilts (.jar containing resources plus framework.aidl)
656//
657
658type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700659 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700660 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700661}
662
663var _ sdkDependency = (*sdkPrebuilt)(nil)
664
Colin Cross7d5136f2015-05-11 13:39:40 -0700665type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700666 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700667}
668
Colin Crossaa8630b2015-04-13 13:52:22 -0700669type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700670 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700671
Colin Cross7d5136f2015-05-11 13:39:40 -0700672 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700673
Colin Cross635c3b02016-05-18 15:37:25 -0700674 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700675}
676
Colin Cross635c3b02016-05-18 15:37:25 -0700677func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700678 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700679
Colin Cross635c3b02016-05-18 15:37:25 -0700680 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700681}
682
Colin Cross635c3b02016-05-18 15:37:25 -0700683func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700684 return j.aidlPreprocessed
685}
686
Colin Cross36242852017-06-23 15:06:31 -0700687func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700688 module := &sdkPrebuilt{}
689
Colin Cross74d73e22017-08-02 11:05:49 -0700690 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700691
Colin Cross74d73e22017-08-02 11:05:49 -0700692 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700693 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
694 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700695}
696
Colin Cross2fe66872015-03-30 17:20:39 -0700697func inList(s string, l []string) bool {
698 for _, e := range l {
699 if e == s {
700 return true
701 }
702 }
703 return false
704}
Colin Cross89536d42017-07-07 14:35:50 -0700705
706//
707// Defaults
708//
709type Defaults struct {
710 android.ModuleBase
711 android.DefaultsModuleBase
712}
713
714func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
715}
716
717func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
718}
719
720func defaultsFactory() android.Module {
721 return DefaultsFactory()
722}
723
724func DefaultsFactory(props ...interface{}) android.Module {
725 module := &Defaults{}
726
727 module.AddProperties(props...)
728 module.AddProperties(
729 &CompilerProperties{},
730 &CompilerDeviceProperties{},
731 )
732
733 android.InitDefaultsModule(module)
734
735 return module
736}