blob: 7b213b2d7bd0dd8a58d053b9fcb51a6d310306cb [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 Cross540eff82017-06-22 17:01:52 -0700102}
103
Colin Cross89536d42017-07-07 14:35:50 -0700104type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700105 // list of module-specific flags that will be used for dex compiles
106 Dxflags []string `android:"arch_variant"`
107
Colin Cross7d5136f2015-05-11 13:39:40 -0700108 // if not blank, set to the version of the sdk to compile against
109 Sdk_version string
110
111 // Set for device java libraries, and for host versions of device java libraries
112 // built for testing
113 Dex bool `blueprint:"mutated"`
114
Colin Cross7d5136f2015-05-11 13:39:40 -0700115 // directories to pass to aidl tool
116 Aidl_includes []string
117
118 // directories that should be added as include directories
119 // for any aidl sources of modules that depend on this module
120 Export_aidl_include_dirs []string
121}
122
Colin Cross46c9b8b2017-06-22 16:51:17 -0700123// Module contains the properties and members used by all java module types
124type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700125 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700126 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700127
Colin Cross89536d42017-07-07 14:35:50 -0700128 properties CompilerProperties
129 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700130
131 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700132 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700133
Colin Crossb7a63242015-04-16 14:09:14 -0700134 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700135 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700136
Colin Cross2fe66872015-03-30 17:20:39 -0700137 // jarSpecs suitable for inserting classes from a static library into another jar
138 classJarSpecs []jarSpec
139
140 // jarSpecs suitable for inserting resources from a static library into another jar
141 resourceJarSpecs []jarSpec
142
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 Cross2fe66872015-03-30 17:20:39 -0700157 ClassJarSpecs() []jarSpec
158 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700159 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700160}
161
Colin Cross89536d42017-07-07 14:35:50 -0700162func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
163 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
164 android.InitDefaultableModule(module)
165}
166
Colin Crossbe1da472017-07-07 15:59:46 -0700167type dependencyTag struct {
168 blueprint.BaseDependencyTag
169 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700170}
171
Colin Crossbe1da472017-07-07 15:59:46 -0700172var (
Colin Crossf506d872017-07-19 15:53:04 -0700173 staticLibTag = dependencyTag{name: "staticlib"}
174 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700175 bootClasspathTag = dependencyTag{name: "bootclasspath"}
176 frameworkResTag = dependencyTag{name: "framework-res"}
177 sdkDependencyTag = dependencyTag{name: "sdk"}
178)
Colin Cross2fe66872015-03-30 17:20:39 -0700179
Colin Crossbe1da472017-07-07 15:59:46 -0700180func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700181 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700182 if ctx.Device() {
183 switch j.deviceProperties.Sdk_version {
184 case "":
Colin Cross227d4362017-08-30 14:14:52 -0700185 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
186 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700187 case "current":
188 // TODO: !TARGET_BUILD_APPS
189 // TODO: export preprocessed framework.aidl from android_stubs_current
190 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
Colin Cross227d4362017-08-30 14:14:52 -0700191 case "test_current":
192 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_test_stubs_current")
Colin Crossbe1da472017-07-07 15:59:46 -0700193 case "system_current":
194 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
195 default:
196 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
197 }
198 } else {
199 if j.deviceProperties.Dex {
Colin Cross227d4362017-08-30 14:14:52 -0700200 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
Colin Crossbe1da472017-07-07 15:59:46 -0700201 }
Colin Cross2fe66872015-03-30 17:20:39 -0700202 }
203 }
Colin Crossf506d872017-07-19 15:53:04 -0700204 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
205 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
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 Cross46c9b8b2017-06-22 16:51:17 -0700232func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross74d73e22017-08-02 11:05:49 -0700233 bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700234 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700235
236 ctx.VisitDirectDeps(func(module blueprint.Module) {
237 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700238 tag := ctx.OtherModuleDependencyTag(module)
239
Colin Crossf506d872017-07-19 15:53:04 -0700240 dep, _ := module.(Dependency)
241 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700242 switch tag {
243 case android.DefaultsDepTag, android.SourceDepTag:
244 default:
245 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700246 }
Colin Crossec7a0422017-07-07 14:47:12 -0700247 return
248 }
249
Colin Crossbe1da472017-07-07 15:59:46 -0700250 switch tag {
251 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700252 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700253 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700254 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700255 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700256 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700257 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
258 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700259 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700260 if ctx.ModuleName() == "framework" {
261 // framework.jar has a one-off dependency on the R.java and Manifest.java files
262 // generated by framework-res.apk
263 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
264 }
Colin Crossbe1da472017-07-07 15:59:46 -0700265 case sdkDependencyTag:
266 sdkDep := module.(sdkDependency)
Colin Cross227d4362017-08-30 14:14:52 -0700267 bootClasspath = append(bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700268 if sdkDep.AidlPreprocessed().Valid() {
269 if aidlPreprocess.Valid() {
270 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
271 aidlPreprocess, sdkDep.AidlPreprocessed())
272 } else {
273 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700274 }
275 }
Colin Crossbe1da472017-07-07 15:59:46 -0700276 default:
277 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700278 }
Colin Crossbe1da472017-07-07 15:59:46 -0700279
Colin Crossf506d872017-07-19 15:53:04 -0700280 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700281 })
282
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700283 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
284 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700285}
286
Colin Cross46c9b8b2017-06-22 16:51:17 -0700287func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700288
Colin Cross540eff82017-06-22 17:01:52 -0700289 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700290
291 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700292 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700293
Colin Crossf03c82b2015-04-13 13:53:40 -0700294 var flags javaBuilderFlags
295
296 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700297
298 if j.properties.Java_version != nil {
299 flags.javaVersion = *j.properties.Java_version
300 } else {
301 flags.javaVersion = "${config.DefaultJavaVersion}"
302 }
303
Colin Crossf03c82b2015-04-13 13:53:40 -0700304 if len(javacFlags) > 0 {
305 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
306 flags.javacFlags = "$javacFlags"
307 }
308
309 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
310 if len(aidlFlags) > 0 {
311 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
312 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700313 }
314
Colin Crossf506d872017-07-19 15:53:04 -0700315 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700316
Colin Cross74d73e22017-08-02 11:05:49 -0700317 if len(bootClasspath) > 0 {
318 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
319 deps = append(deps, bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700320 } else if ctx.Device() {
321 // Explicitly clear the bootclasspath for device builds
322 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700323 }
324
325 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700327 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700328 }
329
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700330 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700331
Colin Crossf05fe972015-04-10 17:45:20 -0700332 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700333
Colin Cross0607cf72015-04-28 13:28:51 -0700334 ctx.VisitDirectDeps(func(module blueprint.Module) {
335 if gen, ok := module.(genrule.SourceFileGenerator); ok {
336 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
337 }
338 })
339
Colin Crossb7a63242015-04-16 14:09:14 -0700340 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
341
Colin Crossc6bbef32017-08-14 14:16:06 -0700342 var extraJarDeps android.Paths
343
Colin Cross8cf13342015-04-10 15:41:49 -0700344 if len(srcFiles) > 0 {
345 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700346 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700347 if ctx.Failed() {
348 return
349 }
350
Colin Crossc6bbef32017-08-14 14:16:06 -0700351 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
352 // If error-prone is enabled, add an additional rule to compile the java files into
353 // a separate set of classes (so that they don't overwrite the normal ones and require
354 // a rebuild when error-prone is turned off). Add the classes as a dependency to
355 // the jar command so the two compiles can run in parallel.
356 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
357 // enable error-prone without affecting the output class files.
358 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags, deps)
359 extraJarDeps = append(extraJarDeps, errorprone)
360 }
361
Colin Cross8cf13342015-04-10 15:41:49 -0700362 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700363 }
364
Colin Crosse8dc34a2017-07-19 11:22:16 -0700365 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700366 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700367
Colin Cross635c3b02016-05-18 15:37:25 -0700368 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700369
370 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
371 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
372
373 // Combine classes + resources into classes-full-debug.jar
Colin Crossc6bbef32017-08-14 14:16:06 -0700374 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest, extraJarDeps)
Colin Cross2fe66872015-03-30 17:20:39 -0700375 if ctx.Failed() {
376 return
377 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700378
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700379 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700380 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700381 // Transform classes-full-debug.jar into classes-jarjar.jar
382 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
383 if ctx.Failed() {
384 return
385 }
Colin Cross20978302015-04-10 17:05:07 -0700386
Colin Cross74d73e22017-08-02 11:05:49 -0700387 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700388 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700389 }
390
Colin Cross20978302015-04-10 17:05:07 -0700391 j.resourceJarSpecs = resourceJarSpecs
392 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700393 j.classpathFile = outputFile
394
Colin Cross540eff82017-06-22 17:01:52 -0700395 if j.deviceProperties.Dex && len(srcFiles) > 0 {
396 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700397 if false /* emma enabled */ {
398 // If you instrument class files that have local variable debug information in
399 // them emma does not correctly maintain the local variable table.
400 // This will cause an error when you try to convert the class files for Android.
401 // The workaround here is to build different dex file here based on emma switch
402 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
403 // option to remove local variable information
404 dxFlags = append(dxFlags, "--no-locals")
405 }
406
Colin Cross1332b002015-04-07 17:11:30 -0700407 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700408 dxFlags = append(dxFlags, "--no-optimize")
409 }
410
Colin Cross1332b002015-04-07 17:11:30 -0700411 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700412 dxFlags = append(dxFlags,
413 "--debug",
414 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700415 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700416 "--dump-width=1000")
417 }
418
Colin Cross595a4062017-08-31 12:30:37 -0700419 var minSdkVersion string
420 switch j.deviceProperties.Sdk_version {
421 case "", "current", "test_current", "system_current":
422 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
423 default:
424 minSdkVersion = j.deviceProperties.Sdk_version
425 }
426
427 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
428
Colin Cross2fe66872015-03-30 17:20:39 -0700429 flags.dxFlags = strings.Join(dxFlags, " ")
430
431 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700432 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700433 if ctx.Failed() {
434 return
435 }
436
437 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700438 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700439 }
Colin Crossb7a63242015-04-16 14:09:14 -0700440 ctx.CheckbuildFile(outputFile)
441 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700442}
443
Colin Crossf506d872017-07-19 15:53:04 -0700444var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700445
Colin Cross74d73e22017-08-02 11:05:49 -0700446func (j *Module) ClasspathFiles() android.Paths {
447 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700448}
449
Colin Cross46c9b8b2017-06-22 16:51:17 -0700450func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700451 return j.classJarSpecs
452}
453
Colin Cross46c9b8b2017-06-22 16:51:17 -0700454func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700455 return j.resourceJarSpecs
456}
457
Colin Cross46c9b8b2017-06-22 16:51:17 -0700458func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700459 return j.exportAidlIncludeDirs
460}
461
Colin Cross46c9b8b2017-06-22 16:51:17 -0700462var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700463
Colin Cross46c9b8b2017-06-22 16:51:17 -0700464func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700465 return j.logtagsSrcs
466}
467
Colin Cross2fe66872015-03-30 17:20:39 -0700468//
469// Java libraries (.jar file)
470//
471
Colin Crossf506d872017-07-19 15:53:04 -0700472type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700473 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700474}
475
Colin Crossf506d872017-07-19 15:53:04 -0700476func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700477 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700478
Colin Cross2c429dc2017-08-31 16:45:16 -0700479 if j.properties.Installable == nil || *j.properties.Installable == true {
480 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
481 ctx.ModuleName()+".jar", j.outputFile)
482 }
Colin Crossb7a63242015-04-16 14:09:14 -0700483}
484
Colin Crossf506d872017-07-19 15:53:04 -0700485func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700486 j.deps(ctx)
487}
488
Colin Crossf506d872017-07-19 15:53:04 -0700489func LibraryFactory() android.Module {
490 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700491
Colin Cross540eff82017-06-22 17:01:52 -0700492 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700493
Colin Cross36242852017-06-23 15:06:31 -0700494 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700495 &module.Module.properties,
496 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700497
Colin Cross89536d42017-07-07 14:35:50 -0700498 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700499 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700500}
501
Colin Crossf506d872017-07-19 15:53:04 -0700502func LibraryHostFactory() android.Module {
503 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700504
Colin Cross36242852017-06-23 15:06:31 -0700505 module.AddProperties(&module.Module.properties)
506
Colin Cross89536d42017-07-07 14:35:50 -0700507 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700508 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700509}
510
511//
512// Java Binaries (.jar file plus wrapper script)
513//
514
Colin Crossf506d872017-07-19 15:53:04 -0700515type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700516 // installable script to execute the resulting jar
517 Wrapper string
518}
519
Colin Crossf506d872017-07-19 15:53:04 -0700520type Binary struct {
521 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700522
Colin Crossf506d872017-07-19 15:53:04 -0700523 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700524
525 wrapperFile android.ModuleSrcPath
526 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700527}
528
Colin Crossf506d872017-07-19 15:53:04 -0700529func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
530 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700531
532 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
533 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700534 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700535 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
536 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700537}
538
Colin Crossf506d872017-07-19 15:53:04 -0700539func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700540 j.deps(ctx)
541}
542
Colin Crossf506d872017-07-19 15:53:04 -0700543func BinaryFactory() android.Module {
544 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700545
Colin Cross540eff82017-06-22 17:01:52 -0700546 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700547
Colin Cross36242852017-06-23 15:06:31 -0700548 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700549 &module.Module.properties,
550 &module.Module.deviceProperties,
551 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700552
Colin Cross89536d42017-07-07 14:35:50 -0700553 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700554 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700555}
556
Colin Crossf506d872017-07-19 15:53:04 -0700557func BinaryHostFactory() android.Module {
558 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700559
Colin Cross36242852017-06-23 15:06:31 -0700560 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700561 &module.Module.properties,
562 &module.Module.deviceProperties,
563 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700564
Colin Cross89536d42017-07-07 14:35:50 -0700565 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700566 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700567}
568
569//
570// Java prebuilts
571//
572
Colin Cross74d73e22017-08-02 11:05:49 -0700573type ImportProperties struct {
574 Jars []string
575}
576
577type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700578 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700579 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700580
Colin Cross74d73e22017-08-02 11:05:49 -0700581 properties ImportProperties
582
583 classpathFiles android.Paths
584 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700585 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700586}
587
Colin Cross74d73e22017-08-02 11:05:49 -0700588func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700589 return &j.prebuilt
590}
591
Colin Cross74d73e22017-08-02 11:05:49 -0700592func (j *Import) PrebuiltSrcs() []string {
593 return j.properties.Jars
594}
595
596func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700597 return j.prebuilt.Name(j.ModuleBase.Name())
598}
599
Colin Cross74d73e22017-08-02 11:05:49 -0700600func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700601}
602
Colin Cross74d73e22017-08-02 11:05:49 -0700603func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
604 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700605
Colin Cross74d73e22017-08-02 11:05:49 -0700606 for i, prebuilt := range j.classpathFiles {
607 subdir := "extracted" + strconv.Itoa(i)
608 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
609 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
610 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
611 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700612
Colin Crossc6bbef32017-08-14 14:16:06 -0700613 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700614}
615
Colin Cross74d73e22017-08-02 11:05:49 -0700616var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700617
Colin Cross74d73e22017-08-02 11:05:49 -0700618func (j *Import) ClasspathFiles() android.Paths {
619 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700620}
621
Colin Cross74d73e22017-08-02 11:05:49 -0700622func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700623 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700624}
625
Colin Cross74d73e22017-08-02 11:05:49 -0700626func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700627 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700628}
629
Colin Cross74d73e22017-08-02 11:05:49 -0700630func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700631 return nil
632}
633
Colin Cross74d73e22017-08-02 11:05:49 -0700634var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700635
Colin Cross74d73e22017-08-02 11:05:49 -0700636func ImportFactory() android.Module {
637 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700638
Colin Cross74d73e22017-08-02 11:05:49 -0700639 module.AddProperties(&module.properties)
640
641 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700642 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
643 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700644}
645
Colin Cross74d73e22017-08-02 11:05:49 -0700646func ImportFactoryHost() android.Module {
647 module := &Import{}
648
649 module.AddProperties(&module.properties)
650
651 android.InitPrebuiltModule(module, &module.properties.Jars)
652 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
653 return module
654}
655
Colin Crossaa8630b2015-04-13 13:52:22 -0700656//
657// SDK java prebuilts (.jar containing resources plus framework.aidl)
658//
659
660type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700661 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700662 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700663}
664
665var _ sdkDependency = (*sdkPrebuilt)(nil)
666
Colin Cross7d5136f2015-05-11 13:39:40 -0700667type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700668 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700669}
670
Colin Crossaa8630b2015-04-13 13:52:22 -0700671type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700672 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700673
Colin Cross7d5136f2015-05-11 13:39:40 -0700674 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700675
Colin Cross635c3b02016-05-18 15:37:25 -0700676 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700677}
678
Colin Cross635c3b02016-05-18 15:37:25 -0700679func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700680 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700681
Colin Cross635c3b02016-05-18 15:37:25 -0700682 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700683}
684
Colin Cross635c3b02016-05-18 15:37:25 -0700685func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700686 return j.aidlPreprocessed
687}
688
Colin Cross36242852017-06-23 15:06:31 -0700689func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700690 module := &sdkPrebuilt{}
691
Colin Cross74d73e22017-08-02 11:05:49 -0700692 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700693
Colin Cross74d73e22017-08-02 11:05:49 -0700694 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700695 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
696 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700697}
698
Colin Cross2fe66872015-03-30 17:20:39 -0700699func inList(s string, l []string) bool {
700 for _, e := range l {
701 if e == s {
702 return true
703 }
704 }
705 return false
706}
Colin Cross89536d42017-07-07 14:35:50 -0700707
708//
709// Defaults
710//
711type Defaults struct {
712 android.ModuleBase
713 android.DefaultsModuleBase
714}
715
716func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
717}
718
719func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
720}
721
722func defaultsFactory() android.Module {
723 return DefaultsFactory()
724}
725
726func DefaultsFactory(props ...interface{}) android.Module {
727 module := &Defaults{}
728
729 module.AddProperties(props...)
730 module.AddProperties(
731 &CompilerProperties{},
732 &CompilerDeviceProperties{},
733 )
734
735 android.InitDefaultsModule(module)
736
737 return module
738}