blob: e2e15d46348a6c69a1a1193273e45fae226ec6ac [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
410 flags.dxFlags = strings.Join(dxFlags, " ")
411
412 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700413 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700414 if ctx.Failed() {
415 return
416 }
417
418 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700419 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700420 }
Colin Crossb7a63242015-04-16 14:09:14 -0700421 ctx.CheckbuildFile(outputFile)
422 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700423}
424
Colin Crossf506d872017-07-19 15:53:04 -0700425var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700426
Colin Cross74d73e22017-08-02 11:05:49 -0700427func (j *Module) ClasspathFiles() android.Paths {
428 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700429}
430
Colin Cross46c9b8b2017-06-22 16:51:17 -0700431func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700432 return j.classJarSpecs
433}
434
Colin Cross46c9b8b2017-06-22 16:51:17 -0700435func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700436 return j.resourceJarSpecs
437}
438
Colin Cross46c9b8b2017-06-22 16:51:17 -0700439func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700440 return j.exportAidlIncludeDirs
441}
442
Colin Cross46c9b8b2017-06-22 16:51:17 -0700443var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700444
Colin Cross46c9b8b2017-06-22 16:51:17 -0700445func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700446 return j.logtagsSrcs
447}
448
Colin Cross2fe66872015-03-30 17:20:39 -0700449//
450// Java libraries (.jar file)
451//
452
Colin Crossf506d872017-07-19 15:53:04 -0700453type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700454 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700455}
456
Colin Crossf506d872017-07-19 15:53:04 -0700457func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700458 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700459
Colin Cross635c3b02016-05-18 15:37:25 -0700460 j.installFile = ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700461}
462
Colin Crossf506d872017-07-19 15:53:04 -0700463func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700464 j.deps(ctx)
465}
466
Colin Crossf506d872017-07-19 15:53:04 -0700467func LibraryFactory() android.Module {
468 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700469
Colin Cross540eff82017-06-22 17:01:52 -0700470 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700471
Colin Cross36242852017-06-23 15:06:31 -0700472 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700473 &module.Module.properties,
474 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700475
Colin Cross89536d42017-07-07 14:35:50 -0700476 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700477 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700478}
479
Colin Crossf506d872017-07-19 15:53:04 -0700480func LibraryHostFactory() android.Module {
481 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700482
Colin Cross36242852017-06-23 15:06:31 -0700483 module.AddProperties(&module.Module.properties)
484
Colin Cross89536d42017-07-07 14:35:50 -0700485 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700486 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700487}
488
489//
490// Java Binaries (.jar file plus wrapper script)
491//
492
Colin Crossf506d872017-07-19 15:53:04 -0700493type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700494 // installable script to execute the resulting jar
495 Wrapper string
496}
497
Colin Crossf506d872017-07-19 15:53:04 -0700498type Binary struct {
499 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700500
Colin Crossf506d872017-07-19 15:53:04 -0700501 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700502
503 wrapperFile android.ModuleSrcPath
504 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700505}
506
Colin Crossf506d872017-07-19 15:53:04 -0700507func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
508 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700509
510 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
511 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700512 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
513 j.binaryFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"),
514 j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700515}
516
Colin Crossf506d872017-07-19 15:53:04 -0700517func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700518 j.deps(ctx)
519}
520
Colin Crossf506d872017-07-19 15:53:04 -0700521func BinaryFactory() android.Module {
522 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700523
Colin Cross540eff82017-06-22 17:01:52 -0700524 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700525
Colin Cross36242852017-06-23 15:06:31 -0700526 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700527 &module.Module.properties,
528 &module.Module.deviceProperties,
529 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700530
Colin Cross89536d42017-07-07 14:35:50 -0700531 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700532 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700533}
534
Colin Crossf506d872017-07-19 15:53:04 -0700535func BinaryHostFactory() android.Module {
536 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700537
Colin Cross36242852017-06-23 15:06:31 -0700538 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700539 &module.Module.properties,
540 &module.Module.deviceProperties,
541 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700542
Colin Cross89536d42017-07-07 14:35:50 -0700543 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700544 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700545}
546
547//
548// Java prebuilts
549//
550
Colin Cross74d73e22017-08-02 11:05:49 -0700551type ImportProperties struct {
552 Jars []string
553}
554
555type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700556 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700557 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700558
Colin Cross74d73e22017-08-02 11:05:49 -0700559 properties ImportProperties
560
561 classpathFiles android.Paths
562 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700563 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700564}
565
Colin Cross74d73e22017-08-02 11:05:49 -0700566func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700567 return &j.prebuilt
568}
569
Colin Cross74d73e22017-08-02 11:05:49 -0700570func (j *Import) PrebuiltSrcs() []string {
571 return j.properties.Jars
572}
573
574func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700575 return j.prebuilt.Name(j.ModuleBase.Name())
576}
577
Colin Cross74d73e22017-08-02 11:05:49 -0700578func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700579}
580
Colin Cross74d73e22017-08-02 11:05:49 -0700581func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
582 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700583
Colin Cross74d73e22017-08-02 11:05:49 -0700584 for i, prebuilt := range j.classpathFiles {
585 subdir := "extracted" + strconv.Itoa(i)
586 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
587 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
588 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
589 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700590
Colin Crossc6bbef32017-08-14 14:16:06 -0700591 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
Colin Cross74d73e22017-08-02 11:05:49 -0700592
593 ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"),
594 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700595}
596
Colin Cross74d73e22017-08-02 11:05:49 -0700597var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700598
Colin Cross74d73e22017-08-02 11:05:49 -0700599func (j *Import) ClasspathFiles() android.Paths {
600 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700601}
602
Colin Cross74d73e22017-08-02 11:05:49 -0700603func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700604 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700605}
606
Colin Cross74d73e22017-08-02 11:05:49 -0700607func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700608 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700609}
610
Colin Cross74d73e22017-08-02 11:05:49 -0700611func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700612 return nil
613}
614
Colin Cross74d73e22017-08-02 11:05:49 -0700615var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700616
Colin Cross74d73e22017-08-02 11:05:49 -0700617func ImportFactory() android.Module {
618 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700619
Colin Cross74d73e22017-08-02 11:05:49 -0700620 module.AddProperties(&module.properties)
621
622 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700623 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
624 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700625}
626
Colin Cross74d73e22017-08-02 11:05:49 -0700627func ImportFactoryHost() android.Module {
628 module := &Import{}
629
630 module.AddProperties(&module.properties)
631
632 android.InitPrebuiltModule(module, &module.properties.Jars)
633 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
634 return module
635}
636
Colin Crossaa8630b2015-04-13 13:52:22 -0700637//
638// SDK java prebuilts (.jar containing resources plus framework.aidl)
639//
640
641type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700642 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700643 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700644}
645
646var _ sdkDependency = (*sdkPrebuilt)(nil)
647
Colin Cross7d5136f2015-05-11 13:39:40 -0700648type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700649 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700650}
651
Colin Crossaa8630b2015-04-13 13:52:22 -0700652type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700653 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700654
Colin Cross7d5136f2015-05-11 13:39:40 -0700655 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700656
Colin Cross635c3b02016-05-18 15:37:25 -0700657 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700658}
659
Colin Cross635c3b02016-05-18 15:37:25 -0700660func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700661 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700662
Colin Cross635c3b02016-05-18 15:37:25 -0700663 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700664}
665
Colin Cross635c3b02016-05-18 15:37:25 -0700666func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700667 return j.aidlPreprocessed
668}
669
Colin Cross36242852017-06-23 15:06:31 -0700670func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700671 module := &sdkPrebuilt{}
672
Colin Cross74d73e22017-08-02 11:05:49 -0700673 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700674
Colin Cross74d73e22017-08-02 11:05:49 -0700675 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700676 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
677 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700678}
679
Colin Cross2fe66872015-03-30 17:20:39 -0700680func inList(s string, l []string) bool {
681 for _, e := range l {
682 if e == s {
683 return true
684 }
685 }
686 return false
687}
Colin Cross89536d42017-07-07 14:35:50 -0700688
689//
690// Defaults
691//
692type Defaults struct {
693 android.ModuleBase
694 android.DefaultsModuleBase
695}
696
697func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
698}
699
700func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
701}
702
703func defaultsFactory() android.Module {
704 return DefaultsFactory()
705}
706
707func DefaultsFactory(props ...interface{}) android.Module {
708 module := &Defaults{}
709
710 module.AddProperties(props...)
711 module.AddProperties(
712 &CompilerProperties{},
713 &CompilerDeviceProperties{},
714 )
715
716 android.InitDefaultsModule(module)
717
718 return module
719}