blob: f7191bfbcf2beae7e352c8b8f999cd2786268227 [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 Cross2fe66872015-03-30 17:20:39 -070023 "strings"
24
25 "github.com/google/blueprint"
Colin Cross2fe66872015-03-30 17:20:39 -070026
Colin Cross635c3b02016-05-18 15:37:25 -070027 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070028 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070029 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070030)
31
Colin Cross463a90e2015-06-17 14:20:06 -070032func init() {
Colin Cross89536d42017-07-07 14:35:50 -070033 android.RegisterModuleType("java_defaults", defaultsFactory)
34
Colin Crossf506d872017-07-19 15:53:04 -070035 android.RegisterModuleType("java_library", LibraryFactory)
36 android.RegisterModuleType("java_library_static", LibraryFactory)
37 android.RegisterModuleType("java_library_host", LibraryHostFactory)
38 android.RegisterModuleType("java_binary", BinaryFactory)
39 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
40 android.RegisterModuleType("java_prebuilt_library", PrebuiltFactory)
Colin Crosse8dc34a2017-07-19 11:22:16 -070041 android.RegisterModuleType("android_prebuilt_sdk", SdkPrebuiltFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070042 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070045}
46
Colin Cross2fe66872015-03-30 17:20:39 -070047// TODO:
48// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070049// Proto
50// Renderscript
51// Post-jar passes:
52// Proguard
Colin Crossba211132017-06-22 15:36:39 -070053// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070054// Jarjar
55// Dex
56// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070057// DroidDoc
58// Findbugs
59
Colin Cross89536d42017-07-07 14:35:50 -070060type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070061 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
62 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070063 Srcs []string `android:"arch_variant"`
64
65 // list of source files that should not be used to build the Java module.
66 // This is most useful in the arch/multilib variants to remove non-common files
67 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
69 // list of directories containing Java resources
Colin Crosse8dc34a2017-07-19 11:22:16 -070070 Resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
Colin Crosse8dc34a2017-07-19 11:22:16 -070072 // list of directories that should be excluded from resource_dirs
73 Exclude_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070074
Paul Duffin2b67e3b2016-11-30 16:13:09 +000075 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070076 // ext, and framework for device targets)
77 No_standard_libraries bool
78
79 // list of module-specific flags that will be used for javac compiles
80 Javacflags []string `android:"arch_variant"`
81
Colin Cross7d5136f2015-05-11 13:39:40 -070082 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070083 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070084
85 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070086 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070090
Colin Cross540eff82017-06-22 17:01:52 -070091 // if not blank, run jarjar using the specified rules file
92 Jarjar_rules *string
93}
94
Colin Cross89536d42017-07-07 14:35:50 -070095type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -070096 // list of module-specific flags that will be used for dex compiles
97 Dxflags []string `android:"arch_variant"`
98
Colin Cross7d5136f2015-05-11 13:39:40 -070099 // if not blank, set to the version of the sdk to compile against
100 Sdk_version string
101
102 // Set for device java libraries, and for host versions of device java libraries
103 // built for testing
104 Dex bool `blueprint:"mutated"`
105
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 // directories to pass to aidl tool
107 Aidl_includes []string
108
109 // directories that should be added as include directories
110 // for any aidl sources of modules that depend on this module
111 Export_aidl_include_dirs []string
112}
113
Colin Cross46c9b8b2017-06-22 16:51:17 -0700114// Module contains the properties and members used by all java module types
115type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700116 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700117 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700118
Colin Cross89536d42017-07-07 14:35:50 -0700119 properties CompilerProperties
120 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700121
122 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700123 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700124
Colin Crossb7a63242015-04-16 14:09:14 -0700125 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700126 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700127
Colin Cross2fe66872015-03-30 17:20:39 -0700128 // jarSpecs suitable for inserting classes from a static library into another jar
129 classJarSpecs []jarSpec
130
131 // jarSpecs suitable for inserting resources from a static library into another jar
132 resourceJarSpecs []jarSpec
133
Colin Cross635c3b02016-05-18 15:37:25 -0700134 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700135
Colin Cross635c3b02016-05-18 15:37:25 -0700136 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700137
Colin Crossb7a63242015-04-16 14:09:14 -0700138 // filelists of extra source files that should be included in the javac command line,
139 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700140 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700141
Colin Cross2fe66872015-03-30 17:20:39 -0700142 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700143 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700144}
145
Colin Crossf506d872017-07-19 15:53:04 -0700146type Dependency interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700147 ClasspathFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700148 ClassJarSpecs() []jarSpec
149 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700150 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700151}
152
Colin Cross89536d42017-07-07 14:35:50 -0700153func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
154 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
155 android.InitDefaultableModule(module)
156}
157
Colin Crossbe1da472017-07-07 15:59:46 -0700158type dependencyTag struct {
159 blueprint.BaseDependencyTag
160 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700161}
162
Colin Crossbe1da472017-07-07 15:59:46 -0700163var (
Colin Crossf506d872017-07-19 15:53:04 -0700164 staticLibTag = dependencyTag{name: "staticlib"}
165 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700166 bootClasspathTag = dependencyTag{name: "bootclasspath"}
167 frameworkResTag = dependencyTag{name: "framework-res"}
168 sdkDependencyTag = dependencyTag{name: "sdk"}
169)
Colin Cross2fe66872015-03-30 17:20:39 -0700170
Colin Crossbe1da472017-07-07 15:59:46 -0700171func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700172 if !j.properties.No_standard_libraries {
Colin Crossbe1da472017-07-07 15:59:46 -0700173 if ctx.Device() {
174 switch j.deviceProperties.Sdk_version {
175 case "":
176 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
177 case "current":
178 // TODO: !TARGET_BUILD_APPS
179 // TODO: export preprocessed framework.aidl from android_stubs_current
180 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
181 case "system_current":
182 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
183 default:
184 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
185 }
186 } else {
187 if j.deviceProperties.Dex {
188 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
189 }
Colin Cross2fe66872015-03-30 17:20:39 -0700190 }
Colin Crossbe1da472017-07-07 15:59:46 -0700191
Colin Cross540eff82017-06-22 17:01:52 -0700192 if ctx.Device() && j.deviceProperties.Sdk_version == "" {
Colin Crossf506d872017-07-19 15:53:04 -0700193 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossefb9ebe2015-04-16 14:08:06 -0700194 }
Colin Cross2fe66872015-03-30 17:20:39 -0700195 }
Colin Crossf506d872017-07-19 15:53:04 -0700196 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
197 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700198}
199
Colin Cross46c9b8b2017-06-22 16:51:17 -0700200func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700201 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700202
Colin Cross540eff82017-06-22 17:01:52 -0700203 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700204
205 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700206 if aidlPreprocess.Valid() {
207 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700208 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700209 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700210 }
211
Colin Cross635c3b02016-05-18 15:37:25 -0700212 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
213 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
214 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700215 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
216 flags = append(flags, "-I"+src.String())
217 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700218
Colin Crossf03c82b2015-04-13 13:53:40 -0700219 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700220}
221
Colin Cross46c9b8b2017-06-22 16:51:17 -0700222func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross635c3b02016-05-18 15:37:25 -0700223 bootClasspath android.OptionalPath, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
224 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700225
226 ctx.VisitDirectDeps(func(module blueprint.Module) {
227 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700228 tag := ctx.OtherModuleDependencyTag(module)
229
Colin Crossf506d872017-07-19 15:53:04 -0700230 dep, _ := module.(Dependency)
231 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700232 switch tag {
233 case android.DefaultsDepTag, android.SourceDepTag:
234 default:
235 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700236 }
Colin Crossec7a0422017-07-07 14:47:12 -0700237 return
238 }
239
Colin Crossbe1da472017-07-07 15:59:46 -0700240 switch tag {
241 case bootClasspathTag:
Colin Crossf506d872017-07-19 15:53:04 -0700242 bootClasspath = android.OptionalPathForPath(dep.ClasspathFile())
243 case libTag:
244 classpath = append(classpath, dep.ClasspathFile())
245 case staticLibTag:
246 classpath = append(classpath, dep.ClasspathFile())
247 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
248 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700249 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700250 if ctx.ModuleName() == "framework" {
251 // framework.jar has a one-off dependency on the R.java and Manifest.java files
252 // generated by framework-res.apk
253 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
254 }
Colin Crossbe1da472017-07-07 15:59:46 -0700255 case sdkDependencyTag:
256 sdkDep := module.(sdkDependency)
Colin Crossec7a0422017-07-07 14:47:12 -0700257 if sdkDep.AidlPreprocessed().Valid() {
258 if aidlPreprocess.Valid() {
259 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
260 aidlPreprocess, sdkDep.AidlPreprocessed())
261 } else {
262 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700263 }
264 }
Colin Crossbe1da472017-07-07 15:59:46 -0700265 default:
266 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700267 }
Colin Crossbe1da472017-07-07 15:59:46 -0700268
Colin Crossf506d872017-07-19 15:53:04 -0700269 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700270 })
271
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700272 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
273 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700274}
275
Colin Cross46c9b8b2017-06-22 16:51:17 -0700276func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700277
Colin Cross540eff82017-06-22 17:01:52 -0700278 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700279
280 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700281 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700282
Colin Crossf03c82b2015-04-13 13:53:40 -0700283 var flags javaBuilderFlags
284
285 javacFlags := j.properties.Javacflags
286 if len(javacFlags) > 0 {
287 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
288 flags.javacFlags = "$javacFlags"
289 }
290
291 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
292 if len(aidlFlags) > 0 {
293 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
294 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700295 }
296
Colin Crossf506d872017-07-19 15:53:04 -0700297 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700298
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700299 if bootClasspath.Valid() {
300 flags.bootClasspath = "-bootclasspath " + bootClasspath.String()
Colin Crossf506d872017-07-19 15:53:04 -0700301 deps = append(deps, bootClasspath.Path())
Colin Cross2fe66872015-03-30 17:20:39 -0700302 }
303
304 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700305 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700306 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700307 }
308
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700309 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700310
Colin Crossf05fe972015-04-10 17:45:20 -0700311 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700312
Colin Cross0607cf72015-04-28 13:28:51 -0700313 ctx.VisitDirectDeps(func(module blueprint.Module) {
314 if gen, ok := module.(genrule.SourceFileGenerator); ok {
315 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
316 }
317 })
318
Colin Crossb7a63242015-04-16 14:09:14 -0700319 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
320
Colin Cross8cf13342015-04-10 15:41:49 -0700321 if len(srcFiles) > 0 {
322 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700323 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700324 if ctx.Failed() {
325 return
326 }
327
328 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700329 }
330
Colin Crosse8dc34a2017-07-19 11:22:16 -0700331 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700332 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700333
Colin Cross635c3b02016-05-18 15:37:25 -0700334 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700335
336 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
337 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
338
339 // Combine classes + resources into classes-full-debug.jar
340 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
341 if ctx.Failed() {
342 return
343 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700344
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700345 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700346 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700347 // Transform classes-full-debug.jar into classes-jarjar.jar
348 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
349 if ctx.Failed() {
350 return
351 }
Colin Cross20978302015-04-10 17:05:07 -0700352
353 classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile)
354 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700355 }
356
Colin Cross20978302015-04-10 17:05:07 -0700357 j.resourceJarSpecs = resourceJarSpecs
358 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700359 j.classpathFile = outputFile
360
Colin Cross540eff82017-06-22 17:01:52 -0700361 if j.deviceProperties.Dex && len(srcFiles) > 0 {
362 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700363 if false /* emma enabled */ {
364 // If you instrument class files that have local variable debug information in
365 // them emma does not correctly maintain the local variable table.
366 // This will cause an error when you try to convert the class files for Android.
367 // The workaround here is to build different dex file here based on emma switch
368 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
369 // option to remove local variable information
370 dxFlags = append(dxFlags, "--no-locals")
371 }
372
Colin Cross1332b002015-04-07 17:11:30 -0700373 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700374 dxFlags = append(dxFlags, "--no-optimize")
375 }
376
Colin Cross1332b002015-04-07 17:11:30 -0700377 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700378 dxFlags = append(dxFlags,
379 "--debug",
380 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700381 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700382 "--dump-width=1000")
383 }
384
385 flags.dxFlags = strings.Join(dxFlags, " ")
386
387 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700388 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700389 if ctx.Failed() {
390 return
391 }
392
393 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700394 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700395 }
Colin Crossb7a63242015-04-16 14:09:14 -0700396 ctx.CheckbuildFile(outputFile)
397 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700398}
399
Colin Crossf506d872017-07-19 15:53:04 -0700400var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700401
Colin Cross46c9b8b2017-06-22 16:51:17 -0700402func (j *Module) ClasspathFile() android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700403 return j.classpathFile
404}
405
Colin Cross46c9b8b2017-06-22 16:51:17 -0700406func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700407 return j.classJarSpecs
408}
409
Colin Cross46c9b8b2017-06-22 16:51:17 -0700410func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700411 return j.resourceJarSpecs
412}
413
Colin Cross46c9b8b2017-06-22 16:51:17 -0700414func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700415 return j.exportAidlIncludeDirs
416}
417
Colin Cross46c9b8b2017-06-22 16:51:17 -0700418var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700419
Colin Cross46c9b8b2017-06-22 16:51:17 -0700420func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700421 return j.logtagsSrcs
422}
423
Colin Cross2fe66872015-03-30 17:20:39 -0700424//
425// Java libraries (.jar file)
426//
427
Colin Crossf506d872017-07-19 15:53:04 -0700428type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700429 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700430}
431
Colin Crossf506d872017-07-19 15:53:04 -0700432func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700433 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700434
Colin Cross635c3b02016-05-18 15:37:25 -0700435 j.installFile = ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700436}
437
Colin Crossf506d872017-07-19 15:53:04 -0700438func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700439 j.deps(ctx)
440}
441
Colin Crossf506d872017-07-19 15:53:04 -0700442func LibraryFactory() android.Module {
443 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700444
Colin Cross540eff82017-06-22 17:01:52 -0700445 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700446
Colin Cross36242852017-06-23 15:06:31 -0700447 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700448 &module.Module.properties,
449 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700450
Colin Cross89536d42017-07-07 14:35:50 -0700451 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700452 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700453}
454
Colin Crossf506d872017-07-19 15:53:04 -0700455func LibraryHostFactory() android.Module {
456 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700457
Colin Cross36242852017-06-23 15:06:31 -0700458 module.AddProperties(&module.Module.properties)
459
Colin Cross89536d42017-07-07 14:35:50 -0700460 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700461 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700462}
463
464//
465// Java Binaries (.jar file plus wrapper script)
466//
467
Colin Crossf506d872017-07-19 15:53:04 -0700468type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700469 // installable script to execute the resulting jar
470 Wrapper string
471}
472
Colin Crossf506d872017-07-19 15:53:04 -0700473type Binary struct {
474 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700475
Colin Crossf506d872017-07-19 15:53:04 -0700476 binaryProperties binaryProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700477}
478
Colin Crossf506d872017-07-19 15:53:04 -0700479func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
480 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700481
482 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
483 // another build rule before the jar has been installed.
Colin Cross635c3b02016-05-18 15:37:25 -0700484 ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper),
Colin Cross2fe66872015-03-30 17:20:39 -0700485 j.installFile)
486}
487
Colin Crossf506d872017-07-19 15:53:04 -0700488func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700489 j.deps(ctx)
490}
491
Colin Crossf506d872017-07-19 15:53:04 -0700492func BinaryFactory() android.Module {
493 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700494
Colin Cross540eff82017-06-22 17:01:52 -0700495 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700496
Colin Cross36242852017-06-23 15:06:31 -0700497 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700498 &module.Module.properties,
499 &module.Module.deviceProperties,
500 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700501
Colin Cross89536d42017-07-07 14:35:50 -0700502 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700503 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700504}
505
Colin Crossf506d872017-07-19 15:53:04 -0700506func BinaryHostFactory() android.Module {
507 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700508
Colin Cross36242852017-06-23 15:06:31 -0700509 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700510 &module.Module.properties,
511 &module.Module.deviceProperties,
512 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700513
Colin Cross89536d42017-07-07 14:35:50 -0700514 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700515 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700516}
517
518//
519// Java prebuilts
520//
521
Colin Crossf506d872017-07-19 15:53:04 -0700522type Prebuilt struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700523 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700524 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700525
Colin Cross635c3b02016-05-18 15:37:25 -0700526 classpathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700527 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700528}
529
Colin Crossf506d872017-07-19 15:53:04 -0700530func (j *Prebuilt) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700531 return &j.prebuilt
532}
533
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700534func (j *Prebuilt) Name() string {
535 return j.prebuilt.Name(j.ModuleBase.Name())
536}
537
Colin Crossf506d872017-07-19 15:53:04 -0700538func (j *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700539}
540
Colin Crossf506d872017-07-19 15:53:04 -0700541func (j *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossbe1da472017-07-07 15:59:46 -0700542 prebuilt := j.prebuilt.Path(ctx)
Colin Crosse1d62a82015-04-03 16:53:05 -0700543
544 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
545
546 j.classpathFile = prebuilt
547 j.classJarSpecs = []jarSpec{classJarSpec}
548 j.resourceJarSpecs = []jarSpec{resourceJarSpec}
Colin Cross635c3b02016-05-18 15:37:25 -0700549 ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.classpathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700550}
551
Colin Crossf506d872017-07-19 15:53:04 -0700552var _ Dependency = (*Prebuilt)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700553
Colin Crossf506d872017-07-19 15:53:04 -0700554func (j *Prebuilt) ClasspathFile() android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700555 return j.classpathFile
556}
557
Colin Crossf506d872017-07-19 15:53:04 -0700558func (j *Prebuilt) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700559 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700560}
561
Colin Crossf506d872017-07-19 15:53:04 -0700562func (j *Prebuilt) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700563 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700564}
565
Colin Crossf506d872017-07-19 15:53:04 -0700566func (j *Prebuilt) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700567 return nil
568}
569
Colin Crossf506d872017-07-19 15:53:04 -0700570func PrebuiltFactory() android.Module {
571 module := &Prebuilt{}
Colin Cross2fe66872015-03-30 17:20:39 -0700572
Colin Crossbe1da472017-07-07 15:59:46 -0700573 module.AddProperties(&module.prebuilt.Properties)
Colin Cross36242852017-06-23 15:06:31 -0700574
575 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
576 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700577}
578
Colin Crossaa8630b2015-04-13 13:52:22 -0700579//
580// SDK java prebuilts (.jar containing resources plus framework.aidl)
581//
582
583type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700584 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700585 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700586}
587
588var _ sdkDependency = (*sdkPrebuilt)(nil)
589
Colin Cross7d5136f2015-05-11 13:39:40 -0700590type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700591 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700592}
593
Colin Crossaa8630b2015-04-13 13:52:22 -0700594type sdkPrebuilt struct {
Colin Crossf506d872017-07-19 15:53:04 -0700595 Prebuilt
Colin Crossaa8630b2015-04-13 13:52:22 -0700596
Colin Cross7d5136f2015-05-11 13:39:40 -0700597 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700598
Colin Cross635c3b02016-05-18 15:37:25 -0700599 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700600}
601
Colin Cross635c3b02016-05-18 15:37:25 -0700602func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossf506d872017-07-19 15:53:04 -0700603 j.Prebuilt.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700604
Colin Cross635c3b02016-05-18 15:37:25 -0700605 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700606}
607
Colin Cross635c3b02016-05-18 15:37:25 -0700608func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700609 return j.aidlPreprocessed
610}
611
Colin Cross36242852017-06-23 15:06:31 -0700612func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700613 module := &sdkPrebuilt{}
614
Colin Cross36242852017-06-23 15:06:31 -0700615 module.AddProperties(
Colin Crossbe1da472017-07-07 15:59:46 -0700616 &module.prebuilt.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700617 &module.sdkProperties)
618
619 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
620 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700621}
622
Colin Cross2fe66872015-03-30 17:20:39 -0700623func inList(s string, l []string) bool {
624 for _, e := range l {
625 if e == s {
626 return true
627 }
628 }
629 return false
630}
Colin Cross89536d42017-07-07 14:35:50 -0700631
632//
633// Defaults
634//
635type Defaults struct {
636 android.ModuleBase
637 android.DefaultsModuleBase
638}
639
640func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
641}
642
643func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
644}
645
646func defaultsFactory() android.Module {
647 return DefaultsFactory()
648}
649
650func DefaultsFactory(props ...interface{}) android.Module {
651 module := &Defaults{}
652
653 module.AddProperties(props...)
654 module.AddProperties(
655 &CompilerProperties{},
656 &CompilerDeviceProperties{},
657 )
658
659 android.InitDefaultsModule(module)
660
661 return module
662}