blob: 9888b387dc27096413c13491c1fb7a69233e4c4a [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 "":
181 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
182 case "current":
183 // TODO: !TARGET_BUILD_APPS
184 // TODO: export preprocessed framework.aidl from android_stubs_current
185 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
186 case "system_current":
187 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
188 default:
189 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
190 }
191 } else {
192 if j.deviceProperties.Dex {
193 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
194 }
Colin Cross2fe66872015-03-30 17:20:39 -0700195 }
Colin Crossbe1da472017-07-07 15:59:46 -0700196
Colin Cross540eff82017-06-22 17:01:52 -0700197 if ctx.Device() && j.deviceProperties.Sdk_version == "" {
Colin Crossf506d872017-07-19 15:53:04 -0700198 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossefb9ebe2015-04-16 14:08:06 -0700199 }
Colin Cross2fe66872015-03-30 17:20:39 -0700200 }
Colin Crossf506d872017-07-19 15:53:04 -0700201 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
202 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700203}
204
Colin Cross46c9b8b2017-06-22 16:51:17 -0700205func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700206 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700207
Colin Cross540eff82017-06-22 17:01:52 -0700208 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700209
210 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700211 if aidlPreprocess.Valid() {
212 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700213 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700214 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700215 }
216
Colin Cross635c3b02016-05-18 15:37:25 -0700217 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
218 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
219 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700220 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
221 flags = append(flags, "-I"+src.String())
222 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700223
Colin Crossf03c82b2015-04-13 13:53:40 -0700224 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700225}
226
Colin Cross46c9b8b2017-06-22 16:51:17 -0700227func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross74d73e22017-08-02 11:05:49 -0700228 bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700229 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700230
231 ctx.VisitDirectDeps(func(module blueprint.Module) {
232 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700233 tag := ctx.OtherModuleDependencyTag(module)
234
Colin Crossf506d872017-07-19 15:53:04 -0700235 dep, _ := module.(Dependency)
236 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700237 switch tag {
238 case android.DefaultsDepTag, android.SourceDepTag:
239 default:
240 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700241 }
Colin Crossec7a0422017-07-07 14:47:12 -0700242 return
243 }
244
Colin Crossbe1da472017-07-07 15:59:46 -0700245 switch tag {
246 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700247 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700248 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700249 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700250 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700251 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700252 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
253 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700254 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700255 if ctx.ModuleName() == "framework" {
256 // framework.jar has a one-off dependency on the R.java and Manifest.java files
257 // generated by framework-res.apk
258 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
259 }
Colin Crossbe1da472017-07-07 15:59:46 -0700260 case sdkDependencyTag:
261 sdkDep := module.(sdkDependency)
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 Cross2fe66872015-03-30 17:20:39 -0700314 }
315
316 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700317 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700318 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700319 }
320
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700321 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700322
Colin Crossf05fe972015-04-10 17:45:20 -0700323 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700324
Colin Cross0607cf72015-04-28 13:28:51 -0700325 ctx.VisitDirectDeps(func(module blueprint.Module) {
326 if gen, ok := module.(genrule.SourceFileGenerator); ok {
327 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
328 }
329 })
330
Colin Crossb7a63242015-04-16 14:09:14 -0700331 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
332
Colin Crossc6bbef32017-08-14 14:16:06 -0700333 var extraJarDeps android.Paths
334
Colin Cross8cf13342015-04-10 15:41:49 -0700335 if len(srcFiles) > 0 {
336 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700337 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700338 if ctx.Failed() {
339 return
340 }
341
Colin Crossc6bbef32017-08-14 14:16:06 -0700342 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
343 // If error-prone is enabled, add an additional rule to compile the java files into
344 // a separate set of classes (so that they don't overwrite the normal ones and require
345 // a rebuild when error-prone is turned off). Add the classes as a dependency to
346 // the jar command so the two compiles can run in parallel.
347 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
348 // enable error-prone without affecting the output class files.
349 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags, deps)
350 extraJarDeps = append(extraJarDeps, errorprone)
351 }
352
Colin Cross8cf13342015-04-10 15:41:49 -0700353 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700354 }
355
Colin Crosse8dc34a2017-07-19 11:22:16 -0700356 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700357 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700358
Colin Cross635c3b02016-05-18 15:37:25 -0700359 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700360
361 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
362 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
363
364 // Combine classes + resources into classes-full-debug.jar
Colin Crossc6bbef32017-08-14 14:16:06 -0700365 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest, extraJarDeps)
Colin Cross2fe66872015-03-30 17:20:39 -0700366 if ctx.Failed() {
367 return
368 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700369
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700370 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700371 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700372 // Transform classes-full-debug.jar into classes-jarjar.jar
373 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
374 if ctx.Failed() {
375 return
376 }
Colin Cross20978302015-04-10 17:05:07 -0700377
Colin Cross74d73e22017-08-02 11:05:49 -0700378 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700379 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700380 }
381
Colin Cross20978302015-04-10 17:05:07 -0700382 j.resourceJarSpecs = resourceJarSpecs
383 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700384 j.classpathFile = outputFile
385
Colin Cross540eff82017-06-22 17:01:52 -0700386 if j.deviceProperties.Dex && len(srcFiles) > 0 {
387 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700388 if false /* emma enabled */ {
389 // If you instrument class files that have local variable debug information in
390 // them emma does not correctly maintain the local variable table.
391 // This will cause an error when you try to convert the class files for Android.
392 // The workaround here is to build different dex file here based on emma switch
393 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
394 // option to remove local variable information
395 dxFlags = append(dxFlags, "--no-locals")
396 }
397
Colin Cross1332b002015-04-07 17:11:30 -0700398 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700399 dxFlags = append(dxFlags, "--no-optimize")
400 }
401
Colin Cross1332b002015-04-07 17:11:30 -0700402 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700403 dxFlags = append(dxFlags,
404 "--debug",
405 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700406 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700407 "--dump-width=1000")
408 }
409
Colin Cross595a4062017-08-31 12:30:37 -0700410 var minSdkVersion string
411 switch j.deviceProperties.Sdk_version {
412 case "", "current", "test_current", "system_current":
413 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
414 default:
415 minSdkVersion = j.deviceProperties.Sdk_version
416 }
417
418 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
419
Colin Cross2fe66872015-03-30 17:20:39 -0700420 flags.dxFlags = strings.Join(dxFlags, " ")
421
422 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700423 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700424 if ctx.Failed() {
425 return
426 }
427
428 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700429 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700430 }
Colin Crossb7a63242015-04-16 14:09:14 -0700431 ctx.CheckbuildFile(outputFile)
432 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700433}
434
Colin Crossf506d872017-07-19 15:53:04 -0700435var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700436
Colin Cross74d73e22017-08-02 11:05:49 -0700437func (j *Module) ClasspathFiles() android.Paths {
438 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700439}
440
Colin Cross46c9b8b2017-06-22 16:51:17 -0700441func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700442 return j.classJarSpecs
443}
444
Colin Cross46c9b8b2017-06-22 16:51:17 -0700445func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700446 return j.resourceJarSpecs
447}
448
Colin Cross46c9b8b2017-06-22 16:51:17 -0700449func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700450 return j.exportAidlIncludeDirs
451}
452
Colin Cross46c9b8b2017-06-22 16:51:17 -0700453var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700454
Colin Cross46c9b8b2017-06-22 16:51:17 -0700455func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700456 return j.logtagsSrcs
457}
458
Colin Cross2fe66872015-03-30 17:20:39 -0700459//
460// Java libraries (.jar file)
461//
462
Colin Crossf506d872017-07-19 15:53:04 -0700463type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700464 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700465}
466
Colin Crossf506d872017-07-19 15:53:04 -0700467func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700468 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700469
Colin Cross5c517922017-08-31 12:29:17 -0700470 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700471}
472
Colin Crossf506d872017-07-19 15:53:04 -0700473func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700474 j.deps(ctx)
475}
476
Colin Crossf506d872017-07-19 15:53:04 -0700477func LibraryFactory() android.Module {
478 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700479
Colin Cross540eff82017-06-22 17:01:52 -0700480 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700481
Colin Cross36242852017-06-23 15:06:31 -0700482 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700483 &module.Module.properties,
484 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700485
Colin Cross89536d42017-07-07 14:35:50 -0700486 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700487 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700488}
489
Colin Crossf506d872017-07-19 15:53:04 -0700490func LibraryHostFactory() android.Module {
491 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700492
Colin Cross36242852017-06-23 15:06:31 -0700493 module.AddProperties(&module.Module.properties)
494
Colin Cross89536d42017-07-07 14:35:50 -0700495 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700496 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700497}
498
499//
500// Java Binaries (.jar file plus wrapper script)
501//
502
Colin Crossf506d872017-07-19 15:53:04 -0700503type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700504 // installable script to execute the resulting jar
505 Wrapper string
506}
507
Colin Crossf506d872017-07-19 15:53:04 -0700508type Binary struct {
509 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700510
Colin Crossf506d872017-07-19 15:53:04 -0700511 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700512
513 wrapperFile android.ModuleSrcPath
514 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700515}
516
Colin Crossf506d872017-07-19 15:53:04 -0700517func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
518 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700519
520 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
521 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700522 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700523 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
524 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700525}
526
Colin Crossf506d872017-07-19 15:53:04 -0700527func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700528 j.deps(ctx)
529}
530
Colin Crossf506d872017-07-19 15:53:04 -0700531func BinaryFactory() android.Module {
532 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700533
Colin Cross540eff82017-06-22 17:01:52 -0700534 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700535
Colin Cross36242852017-06-23 15:06:31 -0700536 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700537 &module.Module.properties,
538 &module.Module.deviceProperties,
539 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700540
Colin Cross89536d42017-07-07 14:35:50 -0700541 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700542 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700543}
544
Colin Crossf506d872017-07-19 15:53:04 -0700545func BinaryHostFactory() android.Module {
546 module := &Binary{}
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.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700554 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700555}
556
557//
558// Java prebuilts
559//
560
Colin Cross74d73e22017-08-02 11:05:49 -0700561type ImportProperties struct {
562 Jars []string
563}
564
565type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700566 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700567 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700568
Colin Cross74d73e22017-08-02 11:05:49 -0700569 properties ImportProperties
570
571 classpathFiles android.Paths
572 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700573 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700574}
575
Colin Cross74d73e22017-08-02 11:05:49 -0700576func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700577 return &j.prebuilt
578}
579
Colin Cross74d73e22017-08-02 11:05:49 -0700580func (j *Import) PrebuiltSrcs() []string {
581 return j.properties.Jars
582}
583
584func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700585 return j.prebuilt.Name(j.ModuleBase.Name())
586}
587
Colin Cross74d73e22017-08-02 11:05:49 -0700588func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700589}
590
Colin Cross74d73e22017-08-02 11:05:49 -0700591func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
592 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700593
Colin Cross74d73e22017-08-02 11:05:49 -0700594 for i, prebuilt := range j.classpathFiles {
595 subdir := "extracted" + strconv.Itoa(i)
596 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
597 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
598 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
599 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700600
Colin Crossc6bbef32017-08-14 14:16:06 -0700601 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
Colin Cross74d73e22017-08-02 11:05:49 -0700602
Colin Cross5c517922017-08-31 12:29:17 -0700603 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Cross74d73e22017-08-02 11:05:49 -0700604 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700605}
606
Colin Cross74d73e22017-08-02 11:05:49 -0700607var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700608
Colin Cross74d73e22017-08-02 11:05:49 -0700609func (j *Import) ClasspathFiles() android.Paths {
610 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700611}
612
Colin Cross74d73e22017-08-02 11:05:49 -0700613func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700614 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700615}
616
Colin Cross74d73e22017-08-02 11:05:49 -0700617func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700618 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700619}
620
Colin Cross74d73e22017-08-02 11:05:49 -0700621func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700622 return nil
623}
624
Colin Cross74d73e22017-08-02 11:05:49 -0700625var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700626
Colin Cross74d73e22017-08-02 11:05:49 -0700627func ImportFactory() android.Module {
628 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700629
Colin Cross74d73e22017-08-02 11:05:49 -0700630 module.AddProperties(&module.properties)
631
632 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700633 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
634 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700635}
636
Colin Cross74d73e22017-08-02 11:05:49 -0700637func ImportFactoryHost() android.Module {
638 module := &Import{}
639
640 module.AddProperties(&module.properties)
641
642 android.InitPrebuiltModule(module, &module.properties.Jars)
643 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
644 return module
645}
646
Colin Crossaa8630b2015-04-13 13:52:22 -0700647//
648// SDK java prebuilts (.jar containing resources plus framework.aidl)
649//
650
651type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700652 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700653 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700654}
655
656var _ sdkDependency = (*sdkPrebuilt)(nil)
657
Colin Cross7d5136f2015-05-11 13:39:40 -0700658type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700659 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700660}
661
Colin Crossaa8630b2015-04-13 13:52:22 -0700662type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700663 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700664
Colin Cross7d5136f2015-05-11 13:39:40 -0700665 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700666
Colin Cross635c3b02016-05-18 15:37:25 -0700667 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700668}
669
Colin Cross635c3b02016-05-18 15:37:25 -0700670func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700671 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700672
Colin Cross635c3b02016-05-18 15:37:25 -0700673 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700674}
675
Colin Cross635c3b02016-05-18 15:37:25 -0700676func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700677 return j.aidlPreprocessed
678}
679
Colin Cross36242852017-06-23 15:06:31 -0700680func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700681 module := &sdkPrebuilt{}
682
Colin Cross74d73e22017-08-02 11:05:49 -0700683 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700684
Colin Cross74d73e22017-08-02 11:05:49 -0700685 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700686 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
687 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700688}
689
Colin Cross2fe66872015-03-30 17:20:39 -0700690func inList(s string, l []string) bool {
691 for _, e := range l {
692 if e == s {
693 return true
694 }
695 }
696 return false
697}
Colin Cross89536d42017-07-07 14:35:50 -0700698
699//
700// Defaults
701//
702type Defaults struct {
703 android.ModuleBase
704 android.DefaultsModuleBase
705}
706
707func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
708}
709
710func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
711}
712
713func defaultsFactory() android.Module {
714 return DefaultsFactory()
715}
716
717func DefaultsFactory(props ...interface{}) android.Module {
718 module := &Defaults{}
719
720 module.AddProperties(props...)
721 module.AddProperties(
722 &CompilerProperties{},
723 &CompilerDeviceProperties{},
724 )
725
726 android.InitDefaultsModule(module)
727
728 return module
729}