blob: b4ed20765d4e5ab5b499db31ed26ccd81057eb37 [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:
252 default:
253 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700254 }
Colin Crossec7a0422017-07-07 14:47:12 -0700255 return
256 }
257
Colin Crossbe1da472017-07-07 15:59:46 -0700258 switch tag {
259 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700260 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700261 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700262 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700263 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700264 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
265 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
266 case annotationProcessorTag:
267 deps.annotationProcessors = append(deps.annotationProcessors, dep.ClasspathFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700268 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700269 if ctx.ModuleName() == "framework" {
270 // framework.jar has a one-off dependency on the R.java and Manifest.java files
271 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700272 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700273 }
Colin Crossbe1da472017-07-07 15:59:46 -0700274 case sdkDependencyTag:
275 sdkDep := module.(sdkDependency)
Colin Cross32f676a2017-09-06 13:41:06 -0700276 deps.bootClasspath = append(deps.bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700277 if sdkDep.AidlPreprocessed().Valid() {
Colin Cross32f676a2017-09-06 13:41:06 -0700278 if deps.aidlPreprocess.Valid() {
Colin Crossec7a0422017-07-07 14:47:12 -0700279 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
Colin Cross32f676a2017-09-06 13:41:06 -0700280 deps.aidlPreprocess, sdkDep.AidlPreprocessed())
Colin Crossec7a0422017-07-07 14:47:12 -0700281 } else {
Colin Cross32f676a2017-09-06 13:41:06 -0700282 deps.aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700283 }
284 }
Colin Crossbe1da472017-07-07 15:59:46 -0700285 default:
286 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700287 }
Colin Crossbe1da472017-07-07 15:59:46 -0700288
Colin Cross32f676a2017-09-06 13:41:06 -0700289 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700290 })
291
Colin Cross32f676a2017-09-06 13:41:06 -0700292 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700293}
294
Colin Cross46c9b8b2017-06-22 16:51:17 -0700295func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700296
Colin Cross540eff82017-06-22 17:01:52 -0700297 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700298
Colin Cross32f676a2017-09-06 13:41:06 -0700299 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700300
Colin Crossf03c82b2015-04-13 13:53:40 -0700301 var flags javaBuilderFlags
302
303 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700304
Colin Cross32f676a2017-09-06 13:41:06 -0700305 if len(deps.annotationProcessors) > 0 {
306 javacFlags = append(javacFlags,
307 "-processorpath "+strings.Join(deps.annotationProcessors.Strings(), ":"))
308 }
309
310 for _, c := range j.properties.Annotation_processor_classes {
311 javacFlags = append(javacFlags, "-processor "+c)
312 }
313
Colin Cross64162712017-08-08 13:17:59 -0700314 if j.properties.Java_version != nil {
315 flags.javaVersion = *j.properties.Java_version
316 } else {
317 flags.javaVersion = "${config.DefaultJavaVersion}"
318 }
319
Colin Crossf03c82b2015-04-13 13:53:40 -0700320 if len(javacFlags) > 0 {
321 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
322 flags.javacFlags = "$javacFlags"
323 }
324
Colin Cross32f676a2017-09-06 13:41:06 -0700325 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700326 if len(aidlFlags) > 0 {
327 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
328 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700329 }
330
Colin Cross32f676a2017-09-06 13:41:06 -0700331 var extraDeps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700332
Colin Cross32f676a2017-09-06 13:41:06 -0700333 if len(deps.bootClasspath) > 0 {
334 flags.bootClasspath = "-bootclasspath " + strings.Join(deps.bootClasspath.Strings(), ":")
335 extraDeps = append(extraDeps, deps.bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700336 } else if ctx.Device() {
337 // Explicitly clear the bootclasspath for device builds
338 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700339 }
340
Colin Cross32f676a2017-09-06 13:41:06 -0700341 if len(deps.classpath) > 0 {
342 flags.classpath = "-classpath " + strings.Join(deps.classpath.Strings(), ":")
343 extraDeps = append(extraDeps, deps.classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700344 }
345
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700346 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700347
Colin Crossf05fe972015-04-10 17:45:20 -0700348 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700349
Colin Cross0607cf72015-04-28 13:28:51 -0700350 ctx.VisitDirectDeps(func(module blueprint.Module) {
351 if gen, ok := module.(genrule.SourceFileGenerator); ok {
352 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
353 }
354 })
355
Colin Cross32f676a2017-09-06 13:41:06 -0700356 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700357
Colin Crossc6bbef32017-08-14 14:16:06 -0700358 var extraJarDeps android.Paths
359
Colin Cross0a6e0072017-08-30 14:24:55 -0700360 var jars android.Paths
361
Colin Cross8cf13342015-04-10 15:41:49 -0700362 if len(srcFiles) > 0 {
363 // Compile java sources into .class files
Colin Cross32f676a2017-09-06 13:41:06 -0700364 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
Colin Cross8cf13342015-04-10 15:41:49 -0700365 if ctx.Failed() {
366 return
367 }
368
Colin Crossc6bbef32017-08-14 14:16:06 -0700369 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
370 // If error-prone is enabled, add an additional rule to compile the java files into
371 // a separate set of classes (so that they don't overwrite the normal ones and require
372 // a rebuild when error-prone is turned off). Add the classes as a dependency to
373 // the jar command so the two compiles can run in parallel.
374 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
375 // enable error-prone without affecting the output class files.
Colin Cross32f676a2017-09-06 13:41:06 -0700376 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
Colin Crossc6bbef32017-08-14 14:16:06 -0700377 extraJarDeps = append(extraJarDeps, errorprone)
378 }
379
Colin Cross0a6e0072017-08-30 14:24:55 -0700380 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700381 }
382
Colin Cross0a6e0072017-08-30 14:24:55 -0700383 resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs)
Colin Cross635c3b02016-05-18 15:37:25 -0700384 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700385
Colin Cross0a6e0072017-08-30 14:24:55 -0700386 if len(resourceJarSpecs) > 0 || manifest.Valid() {
387 // Combine classes + resources into classes-full-debug.jar
388 resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, manifest, extraJarDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700389 if ctx.Failed() {
390 return
391 }
Colin Cross20978302015-04-10 17:05:07 -0700392
Colin Cross0a6e0072017-08-30 14:24:55 -0700393 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700394 }
395
Colin Cross32f676a2017-09-06 13:41:06 -0700396 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700397
398 // Combine the classes built from sources, any manifests, and any static libraries into
399 // classes-combined.jar. If there is only one input jar this step will be skipped.
400 outputFile := TransformJarsToJar(ctx, "classes-combined.jar", jars)
401
402 if j.properties.Jarjar_rules != nil {
403 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
404 // Transform classes-combined.jar into classes-jarjar.jar
405 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
406 if ctx.Failed() {
407 return
408 }
409 }
410
Colin Cross2fe66872015-03-30 17:20:39 -0700411 j.classpathFile = outputFile
412
Colin Cross540eff82017-06-22 17:01:52 -0700413 if j.deviceProperties.Dex && len(srcFiles) > 0 {
414 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700415 if false /* emma enabled */ {
416 // If you instrument class files that have local variable debug information in
417 // them emma does not correctly maintain the local variable table.
418 // This will cause an error when you try to convert the class files for Android.
419 // The workaround here is to build different dex file here based on emma switch
420 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
421 // option to remove local variable information
422 dxFlags = append(dxFlags, "--no-locals")
423 }
424
Colin Cross1332b002015-04-07 17:11:30 -0700425 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700426 dxFlags = append(dxFlags, "--no-optimize")
427 }
428
Colin Cross1332b002015-04-07 17:11:30 -0700429 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700430 dxFlags = append(dxFlags,
431 "--debug",
432 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700433 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700434 "--dump-width=1000")
435 }
436
Colin Cross595a4062017-08-31 12:30:37 -0700437 var minSdkVersion string
438 switch j.deviceProperties.Sdk_version {
439 case "", "current", "test_current", "system_current":
440 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
441 default:
442 minSdkVersion = j.deviceProperties.Sdk_version
443 }
444
445 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
446
Colin Cross2fe66872015-03-30 17:20:39 -0700447 flags.dxFlags = strings.Join(dxFlags, " ")
448
449 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700450 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700451 if ctx.Failed() {
452 return
453 }
454
455 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700456 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700457 }
Colin Crossb7a63242015-04-16 14:09:14 -0700458 ctx.CheckbuildFile(outputFile)
459 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700460}
461
Colin Crossf506d872017-07-19 15:53:04 -0700462var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700463
Colin Cross74d73e22017-08-02 11:05:49 -0700464func (j *Module) ClasspathFiles() android.Paths {
465 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700466}
467
Colin Cross46c9b8b2017-06-22 16:51:17 -0700468func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700469 return j.exportAidlIncludeDirs
470}
471
Colin Cross46c9b8b2017-06-22 16:51:17 -0700472var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700473
Colin Cross46c9b8b2017-06-22 16:51:17 -0700474func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700475 return j.logtagsSrcs
476}
477
Colin Cross2fe66872015-03-30 17:20:39 -0700478//
479// Java libraries (.jar file)
480//
481
Colin Crossf506d872017-07-19 15:53:04 -0700482type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700483 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700484}
485
Colin Crossf506d872017-07-19 15:53:04 -0700486func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700487 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700488
Colin Cross2c429dc2017-08-31 16:45:16 -0700489 if j.properties.Installable == nil || *j.properties.Installable == true {
490 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
491 ctx.ModuleName()+".jar", j.outputFile)
492 }
Colin Crossb7a63242015-04-16 14:09:14 -0700493}
494
Colin Crossf506d872017-07-19 15:53:04 -0700495func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700496 j.deps(ctx)
497}
498
Colin Crossf506d872017-07-19 15:53:04 -0700499func LibraryFactory() android.Module {
500 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700501
Colin Cross540eff82017-06-22 17:01:52 -0700502 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700503
Colin Cross36242852017-06-23 15:06:31 -0700504 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700505 &module.Module.properties,
506 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700507
Colin Cross89536d42017-07-07 14:35:50 -0700508 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700509 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700510}
511
Colin Crossf506d872017-07-19 15:53:04 -0700512func LibraryHostFactory() android.Module {
513 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700514
Colin Cross36242852017-06-23 15:06:31 -0700515 module.AddProperties(&module.Module.properties)
516
Colin Cross89536d42017-07-07 14:35:50 -0700517 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700518 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700519}
520
521//
522// Java Binaries (.jar file plus wrapper script)
523//
524
Colin Crossf506d872017-07-19 15:53:04 -0700525type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700526 // installable script to execute the resulting jar
527 Wrapper string
528}
529
Colin Crossf506d872017-07-19 15:53:04 -0700530type Binary struct {
531 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700532
Colin Crossf506d872017-07-19 15:53:04 -0700533 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700534
535 wrapperFile android.ModuleSrcPath
536 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700537}
538
Colin Crossf506d872017-07-19 15:53:04 -0700539func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
540 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700541
542 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
543 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700544 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700545 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
546 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700547}
548
Colin Crossf506d872017-07-19 15:53:04 -0700549func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700550 j.deps(ctx)
551}
552
Colin Crossf506d872017-07-19 15:53:04 -0700553func BinaryFactory() android.Module {
554 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700555
Colin Cross540eff82017-06-22 17:01:52 -0700556 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700557
Colin Cross36242852017-06-23 15:06:31 -0700558 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700559 &module.Module.properties,
560 &module.Module.deviceProperties,
561 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700562
Colin Cross89536d42017-07-07 14:35:50 -0700563 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700564 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700565}
566
Colin Crossf506d872017-07-19 15:53:04 -0700567func BinaryHostFactory() android.Module {
568 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700569
Colin Cross36242852017-06-23 15:06:31 -0700570 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700571 &module.Module.properties,
572 &module.Module.deviceProperties,
573 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700574
Colin Cross89536d42017-07-07 14:35:50 -0700575 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700576 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700577}
578
579//
580// Java prebuilts
581//
582
Colin Cross74d73e22017-08-02 11:05:49 -0700583type ImportProperties struct {
584 Jars []string
585}
586
587type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700588 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700589 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700590
Colin Cross74d73e22017-08-02 11:05:49 -0700591 properties ImportProperties
592
Colin Cross0a6e0072017-08-30 14:24:55 -0700593 classpathFiles android.Paths
594 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700595}
596
Colin Cross74d73e22017-08-02 11:05:49 -0700597func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700598 return &j.prebuilt
599}
600
Colin Cross74d73e22017-08-02 11:05:49 -0700601func (j *Import) PrebuiltSrcs() []string {
602 return j.properties.Jars
603}
604
605func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700606 return j.prebuilt.Name(j.ModuleBase.Name())
607}
608
Colin Cross74d73e22017-08-02 11:05:49 -0700609func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700610}
611
Colin Cross74d73e22017-08-02 11:05:49 -0700612func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
613 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700614
Colin Cross0a6e0072017-08-30 14:24:55 -0700615 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles)
Colin Cross2fe66872015-03-30 17:20:39 -0700616}
617
Colin Cross74d73e22017-08-02 11:05:49 -0700618var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700619
Colin Cross74d73e22017-08-02 11:05:49 -0700620func (j *Import) ClasspathFiles() android.Paths {
621 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700622}
623
Colin Cross74d73e22017-08-02 11:05:49 -0700624func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700625 return nil
626}
627
Colin Cross74d73e22017-08-02 11:05:49 -0700628var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700629
Colin Cross74d73e22017-08-02 11:05:49 -0700630func ImportFactory() android.Module {
631 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700632
Colin Cross74d73e22017-08-02 11:05:49 -0700633 module.AddProperties(&module.properties)
634
635 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700636 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
637 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700638}
639
Colin Cross74d73e22017-08-02 11:05:49 -0700640func ImportFactoryHost() android.Module {
641 module := &Import{}
642
643 module.AddProperties(&module.properties)
644
645 android.InitPrebuiltModule(module, &module.properties.Jars)
646 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
647 return module
648}
649
Colin Crossaa8630b2015-04-13 13:52:22 -0700650//
651// SDK java prebuilts (.jar containing resources plus framework.aidl)
652//
653
654type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700655 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700656 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700657}
658
659var _ sdkDependency = (*sdkPrebuilt)(nil)
660
Colin Cross7d5136f2015-05-11 13:39:40 -0700661type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700662 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700663}
664
Colin Crossaa8630b2015-04-13 13:52:22 -0700665type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700666 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700667
Colin Cross7d5136f2015-05-11 13:39:40 -0700668 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700669
Colin Cross635c3b02016-05-18 15:37:25 -0700670 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700671}
672
Colin Cross635c3b02016-05-18 15:37:25 -0700673func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700674 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700675
Colin Cross635c3b02016-05-18 15:37:25 -0700676 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700677}
678
Colin Cross635c3b02016-05-18 15:37:25 -0700679func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700680 return j.aidlPreprocessed
681}
682
Colin Cross36242852017-06-23 15:06:31 -0700683func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700684 module := &sdkPrebuilt{}
685
Colin Cross74d73e22017-08-02 11:05:49 -0700686 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700687
Colin Cross74d73e22017-08-02 11:05:49 -0700688 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700689 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
690 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700691}
692
Colin Cross2fe66872015-03-30 17:20:39 -0700693func inList(s string, l []string) bool {
694 for _, e := range l {
695 if e == s {
696 return true
697 }
698 }
699 return false
700}
Colin Cross89536d42017-07-07 14:35:50 -0700701
702//
703// Defaults
704//
705type Defaults struct {
706 android.ModuleBase
707 android.DefaultsModuleBase
708}
709
710func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
711}
712
713func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
714}
715
716func defaultsFactory() android.Module {
717 return DefaultsFactory()
718}
719
720func DefaultsFactory(props ...interface{}) android.Module {
721 module := &Defaults{}
722
723 module.AddProperties(props...)
724 module.AddProperties(
725 &CompilerProperties{},
726 &CompilerDeviceProperties{},
727 )
728
729 android.InitDefaultsModule(module)
730
731 return module
732}