blob: d18656c685bf1c97aed9e0332307adc74ee6ed89 [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 Cross2fe66872015-03-30 17:20:39 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070029 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070030 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross89536d42017-07-07 14:35:50 -070034 android.RegisterModuleType("java_defaults", defaultsFactory)
35
Colin Crossf506d872017-07-19 15:53:04 -070036 android.RegisterModuleType("java_library", LibraryFactory)
37 android.RegisterModuleType("java_library_static", LibraryFactory)
38 android.RegisterModuleType("java_library_host", LibraryHostFactory)
39 android.RegisterModuleType("java_binary", BinaryFactory)
40 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070041 android.RegisterModuleType("java_import", ImportFactory)
42 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Crosse8dc34a2017-07-19 11:22:16 -070043 android.RegisterModuleType("android_prebuilt_sdk", SdkPrebuiltFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070045
Colin Cross798bfce2016-10-12 14:28:16 -070046 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070047}
48
Colin Cross2fe66872015-03-30 17:20:39 -070049// TODO:
50// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070051// Proto
52// Renderscript
53// Post-jar passes:
54// Proguard
Colin Crossba211132017-06-22 15:36:39 -070055// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070056// Jarjar
57// Dex
58// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070059// DroidDoc
60// Findbugs
61
Colin Cross89536d42017-07-07 14:35:50 -070062type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070063 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
64 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070065 Srcs []string `android:"arch_variant"`
66
67 // list of source files that should not be used to build the Java module.
68 // This is most useful in the arch/multilib variants to remove non-common files
69 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070070
71 // list of directories containing Java resources
Colin Crosse8dc34a2017-07-19 11:22:16 -070072 Resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070073
Colin Crosse8dc34a2017-07-19 11:22:16 -070074 // list of directories that should be excluded from resource_dirs
75 Exclude_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070076
Paul Duffin2b67e3b2016-11-30 16:13:09 +000077 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070078 // ext, and framework for device targets)
79 No_standard_libraries bool
80
81 // list of module-specific flags that will be used for javac compiles
82 Javacflags []string `android:"arch_variant"`
83
Colin Cross7d5136f2015-05-11 13:39:40 -070084 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070085 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070086
87 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070088 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070089
90 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070092
Colin Cross540eff82017-06-22 17:01:52 -070093 // if not blank, run jarjar using the specified rules file
94 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -070095
96 // If not blank, set the java version passed to javac as -source and -target
97 Java_version *string
Colin Cross540eff82017-06-22 17:01:52 -070098}
99
Colin Cross89536d42017-07-07 14:35:50 -0700100type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700101 // list of module-specific flags that will be used for dex compiles
102 Dxflags []string `android:"arch_variant"`
103
Colin Cross7d5136f2015-05-11 13:39:40 -0700104 // if not blank, set to the version of the sdk to compile against
105 Sdk_version string
106
107 // Set for device java libraries, and for host versions of device java libraries
108 // built for testing
109 Dex bool `blueprint:"mutated"`
110
Colin Cross7d5136f2015-05-11 13:39:40 -0700111 // directories to pass to aidl tool
112 Aidl_includes []string
113
114 // directories that should be added as include directories
115 // for any aidl sources of modules that depend on this module
116 Export_aidl_include_dirs []string
117}
118
Colin Cross46c9b8b2017-06-22 16:51:17 -0700119// Module contains the properties and members used by all java module types
120type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700121 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700122 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700123
Colin Cross89536d42017-07-07 14:35:50 -0700124 properties CompilerProperties
125 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700126
127 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700128 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700129
Colin Crossb7a63242015-04-16 14:09:14 -0700130 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700131 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700132
Colin Cross2fe66872015-03-30 17:20:39 -0700133 // jarSpecs suitable for inserting classes from a static library into another jar
134 classJarSpecs []jarSpec
135
136 // jarSpecs suitable for inserting resources from a static library into another jar
137 resourceJarSpecs []jarSpec
138
Colin Cross635c3b02016-05-18 15:37:25 -0700139 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700140
Colin Cross635c3b02016-05-18 15:37:25 -0700141 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700142
Colin Crossb7a63242015-04-16 14:09:14 -0700143 // filelists of extra source files that should be included in the javac command line,
144 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700145 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700146
Colin Cross2fe66872015-03-30 17:20:39 -0700147 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700148 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700149}
150
Colin Crossf506d872017-07-19 15:53:04 -0700151type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700152 ClasspathFiles() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700153 ClassJarSpecs() []jarSpec
154 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700155 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700156}
157
Colin Cross89536d42017-07-07 14:35:50 -0700158func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
159 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
160 android.InitDefaultableModule(module)
161}
162
Colin Crossbe1da472017-07-07 15:59:46 -0700163type dependencyTag struct {
164 blueprint.BaseDependencyTag
165 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700166}
167
Colin Crossbe1da472017-07-07 15:59:46 -0700168var (
Colin Crossf506d872017-07-19 15:53:04 -0700169 staticLibTag = dependencyTag{name: "staticlib"}
170 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700171 bootClasspathTag = dependencyTag{name: "bootclasspath"}
172 frameworkResTag = dependencyTag{name: "framework-res"}
173 sdkDependencyTag = dependencyTag{name: "sdk"}
174)
Colin Cross2fe66872015-03-30 17:20:39 -0700175
Colin Crossbe1da472017-07-07 15:59:46 -0700176func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700177 if !j.properties.No_standard_libraries {
Colin Crossbe1da472017-07-07 15:59:46 -0700178 if ctx.Device() {
179 switch j.deviceProperties.Sdk_version {
180 case "":
Colin Cross227d4362017-08-30 14:14:52 -0700181 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
182 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700183 case "current":
184 // TODO: !TARGET_BUILD_APPS
185 // TODO: export preprocessed framework.aidl from android_stubs_current
186 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
Colin Cross227d4362017-08-30 14:14:52 -0700187 case "test_current":
188 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_test_stubs_current")
Colin Crossbe1da472017-07-07 15:59:46 -0700189 case "system_current":
190 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
191 default:
192 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
193 }
194 } else {
195 if j.deviceProperties.Dex {
Colin Cross227d4362017-08-30 14:14:52 -0700196 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
Colin Crossbe1da472017-07-07 15:59:46 -0700197 }
Colin Cross2fe66872015-03-30 17:20:39 -0700198 }
199 }
Colin Crossf506d872017-07-19 15:53:04 -0700200 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
201 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700202}
203
Colin Cross46c9b8b2017-06-22 16:51:17 -0700204func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700205 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700206
Colin Cross540eff82017-06-22 17:01:52 -0700207 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700208
209 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700210 if aidlPreprocess.Valid() {
211 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700212 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700213 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700214 }
215
Colin Cross635c3b02016-05-18 15:37:25 -0700216 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
217 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
218 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700219 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
220 flags = append(flags, "-I"+src.String())
221 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700222
Colin Crossf03c82b2015-04-13 13:53:40 -0700223 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700224}
225
Colin Cross46c9b8b2017-06-22 16:51:17 -0700226func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross74d73e22017-08-02 11:05:49 -0700227 bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700228 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700229
230 ctx.VisitDirectDeps(func(module blueprint.Module) {
231 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700232 tag := ctx.OtherModuleDependencyTag(module)
233
Colin Crossf506d872017-07-19 15:53:04 -0700234 dep, _ := module.(Dependency)
235 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700236 switch tag {
237 case android.DefaultsDepTag, android.SourceDepTag:
238 default:
239 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700240 }
Colin Crossec7a0422017-07-07 14:47:12 -0700241 return
242 }
243
Colin Crossbe1da472017-07-07 15:59:46 -0700244 switch tag {
245 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700246 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700247 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700248 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700249 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700250 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700251 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
252 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700253 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700254 if ctx.ModuleName() == "framework" {
255 // framework.jar has a one-off dependency on the R.java and Manifest.java files
256 // generated by framework-res.apk
257 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
258 }
Colin Crossbe1da472017-07-07 15:59:46 -0700259 case sdkDependencyTag:
260 sdkDep := module.(sdkDependency)
Colin Cross227d4362017-08-30 14:14:52 -0700261 bootClasspath = append(bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700262 if sdkDep.AidlPreprocessed().Valid() {
263 if aidlPreprocess.Valid() {
264 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
265 aidlPreprocess, sdkDep.AidlPreprocessed())
266 } else {
267 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700268 }
269 }
Colin Crossbe1da472017-07-07 15:59:46 -0700270 default:
271 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700272 }
Colin Crossbe1da472017-07-07 15:59:46 -0700273
Colin Crossf506d872017-07-19 15:53:04 -0700274 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700275 })
276
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700277 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
278 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700279}
280
Colin Cross46c9b8b2017-06-22 16:51:17 -0700281func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700282
Colin Cross540eff82017-06-22 17:01:52 -0700283 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700284
285 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700286 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700287
Colin Crossf03c82b2015-04-13 13:53:40 -0700288 var flags javaBuilderFlags
289
290 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700291
292 if j.properties.Java_version != nil {
293 flags.javaVersion = *j.properties.Java_version
294 } else {
295 flags.javaVersion = "${config.DefaultJavaVersion}"
296 }
297
Colin Crossf03c82b2015-04-13 13:53:40 -0700298 if len(javacFlags) > 0 {
299 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
300 flags.javacFlags = "$javacFlags"
301 }
302
303 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
304 if len(aidlFlags) > 0 {
305 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
306 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700307 }
308
Colin Crossf506d872017-07-19 15:53:04 -0700309 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700310
Colin Cross74d73e22017-08-02 11:05:49 -0700311 if len(bootClasspath) > 0 {
312 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
313 deps = append(deps, bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700314 } else if ctx.Device() {
315 // Explicitly clear the bootclasspath for device builds
316 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700317 }
318
319 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700320 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700321 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700322 }
323
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700324 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700325
Colin Crossf05fe972015-04-10 17:45:20 -0700326 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700327
Colin Cross0607cf72015-04-28 13:28:51 -0700328 ctx.VisitDirectDeps(func(module blueprint.Module) {
329 if gen, ok := module.(genrule.SourceFileGenerator); ok {
330 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
331 }
332 })
333
Colin Crossb7a63242015-04-16 14:09:14 -0700334 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
335
Colin Crossc6bbef32017-08-14 14:16:06 -0700336 var extraJarDeps android.Paths
337
Colin Cross8cf13342015-04-10 15:41:49 -0700338 if len(srcFiles) > 0 {
339 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700340 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700341 if ctx.Failed() {
342 return
343 }
344
Colin Crossc6bbef32017-08-14 14:16:06 -0700345 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
346 // If error-prone is enabled, add an additional rule to compile the java files into
347 // a separate set of classes (so that they don't overwrite the normal ones and require
348 // a rebuild when error-prone is turned off). Add the classes as a dependency to
349 // the jar command so the two compiles can run in parallel.
350 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
351 // enable error-prone without affecting the output class files.
352 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags, deps)
353 extraJarDeps = append(extraJarDeps, errorprone)
354 }
355
Colin Cross8cf13342015-04-10 15:41:49 -0700356 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700357 }
358
Colin Crosse8dc34a2017-07-19 11:22:16 -0700359 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700360 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700361
Colin Cross635c3b02016-05-18 15:37:25 -0700362 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700363
364 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
365 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
366
367 // Combine classes + resources into classes-full-debug.jar
Colin Crossc6bbef32017-08-14 14:16:06 -0700368 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest, extraJarDeps)
Colin Cross2fe66872015-03-30 17:20:39 -0700369 if ctx.Failed() {
370 return
371 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700372
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700373 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700374 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700375 // Transform classes-full-debug.jar into classes-jarjar.jar
376 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
377 if ctx.Failed() {
378 return
379 }
Colin Cross20978302015-04-10 17:05:07 -0700380
Colin Cross74d73e22017-08-02 11:05:49 -0700381 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700382 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700383 }
384
Colin Cross20978302015-04-10 17:05:07 -0700385 j.resourceJarSpecs = resourceJarSpecs
386 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700387 j.classpathFile = outputFile
388
Colin Cross540eff82017-06-22 17:01:52 -0700389 if j.deviceProperties.Dex && len(srcFiles) > 0 {
390 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700391 if false /* emma enabled */ {
392 // If you instrument class files that have local variable debug information in
393 // them emma does not correctly maintain the local variable table.
394 // This will cause an error when you try to convert the class files for Android.
395 // The workaround here is to build different dex file here based on emma switch
396 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
397 // option to remove local variable information
398 dxFlags = append(dxFlags, "--no-locals")
399 }
400
Colin Cross1332b002015-04-07 17:11:30 -0700401 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700402 dxFlags = append(dxFlags, "--no-optimize")
403 }
404
Colin Cross1332b002015-04-07 17:11:30 -0700405 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700406 dxFlags = append(dxFlags,
407 "--debug",
408 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700409 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700410 "--dump-width=1000")
411 }
412
Colin Cross595a4062017-08-31 12:30:37 -0700413 var minSdkVersion string
414 switch j.deviceProperties.Sdk_version {
415 case "", "current", "test_current", "system_current":
416 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
417 default:
418 minSdkVersion = j.deviceProperties.Sdk_version
419 }
420
421 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
422
Colin Cross2fe66872015-03-30 17:20:39 -0700423 flags.dxFlags = strings.Join(dxFlags, " ")
424
425 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700426 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700427 if ctx.Failed() {
428 return
429 }
430
431 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700432 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700433 }
Colin Crossb7a63242015-04-16 14:09:14 -0700434 ctx.CheckbuildFile(outputFile)
435 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700436}
437
Colin Crossf506d872017-07-19 15:53:04 -0700438var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700439
Colin Cross74d73e22017-08-02 11:05:49 -0700440func (j *Module) ClasspathFiles() android.Paths {
441 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700442}
443
Colin Cross46c9b8b2017-06-22 16:51:17 -0700444func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700445 return j.classJarSpecs
446}
447
Colin Cross46c9b8b2017-06-22 16:51:17 -0700448func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700449 return j.resourceJarSpecs
450}
451
Colin Cross46c9b8b2017-06-22 16:51:17 -0700452func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700453 return j.exportAidlIncludeDirs
454}
455
Colin Cross46c9b8b2017-06-22 16:51:17 -0700456var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700457
Colin Cross46c9b8b2017-06-22 16:51:17 -0700458func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700459 return j.logtagsSrcs
460}
461
Colin Cross2fe66872015-03-30 17:20:39 -0700462//
463// Java libraries (.jar file)
464//
465
Colin Crossf506d872017-07-19 15:53:04 -0700466type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700467 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700468}
469
Colin Crossf506d872017-07-19 15:53:04 -0700470func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700471 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700472
Colin Cross5c517922017-08-31 12:29:17 -0700473 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700474}
475
Colin Crossf506d872017-07-19 15:53:04 -0700476func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700477 j.deps(ctx)
478}
479
Colin Crossf506d872017-07-19 15:53:04 -0700480func LibraryFactory() android.Module {
481 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700482
Colin Cross540eff82017-06-22 17:01:52 -0700483 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700484
Colin Cross36242852017-06-23 15:06:31 -0700485 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700486 &module.Module.properties,
487 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700488
Colin Cross89536d42017-07-07 14:35:50 -0700489 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700490 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700491}
492
Colin Crossf506d872017-07-19 15:53:04 -0700493func LibraryHostFactory() android.Module {
494 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700495
Colin Cross36242852017-06-23 15:06:31 -0700496 module.AddProperties(&module.Module.properties)
497
Colin Cross89536d42017-07-07 14:35:50 -0700498 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700499 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700500}
501
502//
503// Java Binaries (.jar file plus wrapper script)
504//
505
Colin Crossf506d872017-07-19 15:53:04 -0700506type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700507 // installable script to execute the resulting jar
508 Wrapper string
509}
510
Colin Crossf506d872017-07-19 15:53:04 -0700511type Binary struct {
512 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700513
Colin Crossf506d872017-07-19 15:53:04 -0700514 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700515
516 wrapperFile android.ModuleSrcPath
517 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700518}
519
Colin Crossf506d872017-07-19 15:53:04 -0700520func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
521 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700522
523 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
524 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700525 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700526 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
527 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700528}
529
Colin Crossf506d872017-07-19 15:53:04 -0700530func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700531 j.deps(ctx)
532}
533
Colin Crossf506d872017-07-19 15:53:04 -0700534func BinaryFactory() android.Module {
535 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700536
Colin Cross540eff82017-06-22 17:01:52 -0700537 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700538
Colin Cross36242852017-06-23 15:06:31 -0700539 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700540 &module.Module.properties,
541 &module.Module.deviceProperties,
542 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700543
Colin Cross89536d42017-07-07 14:35:50 -0700544 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700545 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700546}
547
Colin Crossf506d872017-07-19 15:53:04 -0700548func BinaryHostFactory() android.Module {
549 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700550
Colin Cross36242852017-06-23 15:06:31 -0700551 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700552 &module.Module.properties,
553 &module.Module.deviceProperties,
554 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700555
Colin Cross89536d42017-07-07 14:35:50 -0700556 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700557 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700558}
559
560//
561// Java prebuilts
562//
563
Colin Cross74d73e22017-08-02 11:05:49 -0700564type ImportProperties struct {
565 Jars []string
566}
567
568type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700569 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700570 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700571
Colin Cross74d73e22017-08-02 11:05:49 -0700572 properties ImportProperties
573
574 classpathFiles android.Paths
575 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700576 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700577}
578
Colin Cross74d73e22017-08-02 11:05:49 -0700579func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700580 return &j.prebuilt
581}
582
Colin Cross74d73e22017-08-02 11:05:49 -0700583func (j *Import) PrebuiltSrcs() []string {
584 return j.properties.Jars
585}
586
587func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700588 return j.prebuilt.Name(j.ModuleBase.Name())
589}
590
Colin Cross74d73e22017-08-02 11:05:49 -0700591func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700592}
593
Colin Cross74d73e22017-08-02 11:05:49 -0700594func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
595 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700596
Colin Cross74d73e22017-08-02 11:05:49 -0700597 for i, prebuilt := range j.classpathFiles {
598 subdir := "extracted" + strconv.Itoa(i)
599 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
600 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
601 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
602 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700603
Colin Crossc6bbef32017-08-14 14:16:06 -0700604 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
Colin Cross74d73e22017-08-02 11:05:49 -0700605
Colin Cross5c517922017-08-31 12:29:17 -0700606 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Cross74d73e22017-08-02 11:05:49 -0700607 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700608}
609
Colin Cross74d73e22017-08-02 11:05:49 -0700610var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700611
Colin Cross74d73e22017-08-02 11:05:49 -0700612func (j *Import) ClasspathFiles() android.Paths {
613 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700614}
615
Colin Cross74d73e22017-08-02 11:05:49 -0700616func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700617 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700618}
619
Colin Cross74d73e22017-08-02 11:05:49 -0700620func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700621 return j.resourceJarSpecs
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}