blob: 7349065000509db4cd27b5314bcf74ef1d19c92e [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 Cross798bfce2016-10-12 14:28:16 -070033 android.RegisterModuleType("java_library", JavaLibraryFactory)
34 android.RegisterModuleType("java_library_static", JavaLibraryFactory)
35 android.RegisterModuleType("java_library_host", JavaLibraryHostFactory)
36 android.RegisterModuleType("java_binary", JavaBinaryFactory)
37 android.RegisterModuleType("java_binary_host", JavaBinaryHostFactory)
38 android.RegisterModuleType("prebuilt_java_library", JavaPrebuiltFactory)
39 android.RegisterModuleType("prebuilt_sdk", SdkPrebuiltFactory)
40 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070041
Colin Cross798bfce2016-10-12 14:28:16 -070042 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070043}
44
Colin Cross2fe66872015-03-30 17:20:39 -070045// TODO:
46// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070047// Proto
48// Renderscript
49// Post-jar passes:
50// Proguard
Colin Crossba211132017-06-22 15:36:39 -070051// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070052// Jarjar
53// Dex
54// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070055// DroidDoc
56// Findbugs
57
Colin Cross46c9b8b2017-06-22 16:51:17 -070058type compilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070059 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
60 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070061 Srcs []string `android:"arch_variant"`
62
63 // list of source files that should not be used to build the Java module.
64 // This is most useful in the arch/multilib variants to remove non-common files
65 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070066
67 // list of directories containing Java resources
68 Java_resource_dirs []string `android:"arch_variant"`
69
Dan Willemsen2ef08f42015-06-30 18:15:24 -070070 // list of directories that should be excluded from java_resource_dirs
71 Exclude_java_resource_dirs []string `android:"arch_variant"`
72
Paul Duffin2b67e3b2016-11-30 16:13:09 +000073 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070074 // ext, and framework for device targets)
75 No_standard_libraries bool
76
77 // list of module-specific flags that will be used for javac compiles
78 Javacflags []string `android:"arch_variant"`
79
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // list of of java libraries that will be in the classpath
81 Java_libs []string `android:"arch_variant"`
82
83 // list of java libraries that will be compiled into the resulting jar
84 Java_static_libs []string `android:"arch_variant"`
85
86 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070088
Colin Cross540eff82017-06-22 17:01:52 -070089 // if not blank, run jarjar using the specified rules file
90 Jarjar_rules *string
91}
92
93type compilerDeviceProperties struct {
94 // list of module-specific flags that will be used for dex compiles
95 Dxflags []string `android:"arch_variant"`
96
Colin Cross7d5136f2015-05-11 13:39:40 -070097 // if not blank, set to the version of the sdk to compile against
98 Sdk_version string
99
100 // Set for device java libraries, and for host versions of device java libraries
101 // built for testing
102 Dex bool `blueprint:"mutated"`
103
Colin Cross7d5136f2015-05-11 13:39:40 -0700104 // directories to pass to aidl tool
105 Aidl_includes []string
106
107 // directories that should be added as include directories
108 // for any aidl sources of modules that depend on this module
109 Export_aidl_include_dirs []string
110}
111
Colin Cross46c9b8b2017-06-22 16:51:17 -0700112// Module contains the properties and members used by all java module types
113type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700114 android.ModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700115
Colin Cross540eff82017-06-22 17:01:52 -0700116 properties compilerProperties
117 deviceProperties compilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700118
119 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700120 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700121
Colin Crossb7a63242015-04-16 14:09:14 -0700122 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700123 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700124
Colin Cross2fe66872015-03-30 17:20:39 -0700125 // jarSpecs suitable for inserting classes from a static library into another jar
126 classJarSpecs []jarSpec
127
128 // jarSpecs suitable for inserting resources from a static library into another jar
129 resourceJarSpecs []jarSpec
130
Colin Cross635c3b02016-05-18 15:37:25 -0700131 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700132
Colin Cross635c3b02016-05-18 15:37:25 -0700133 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700134
Colin Crossb7a63242015-04-16 14:09:14 -0700135 // filelists of extra source files that should be included in the javac command line,
136 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700137 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700138
Colin Cross2fe66872015-03-30 17:20:39 -0700139 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700140 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700141}
142
Colin Cross2fe66872015-03-30 17:20:39 -0700143type JavaDependency interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700144 ClasspathFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700145 ClassJarSpecs() []jarSpec
146 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700147 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700148}
149
Colin Crossbe1da472017-07-07 15:59:46 -0700150type dependencyTag struct {
151 blueprint.BaseDependencyTag
152 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700153}
154
Colin Crossbe1da472017-07-07 15:59:46 -0700155var (
156 javaStaticLibTag = dependencyTag{name: "staticlib"}
157 javaLibTag = dependencyTag{name: "javalib"}
158 bootClasspathTag = dependencyTag{name: "bootclasspath"}
159 frameworkResTag = dependencyTag{name: "framework-res"}
160 sdkDependencyTag = dependencyTag{name: "sdk"}
161)
Colin Cross2fe66872015-03-30 17:20:39 -0700162
Colin Crossbe1da472017-07-07 15:59:46 -0700163func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700164 if !j.properties.No_standard_libraries {
Colin Crossbe1da472017-07-07 15:59:46 -0700165 if ctx.Device() {
166 switch j.deviceProperties.Sdk_version {
167 case "":
168 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
169 case "current":
170 // TODO: !TARGET_BUILD_APPS
171 // TODO: export preprocessed framework.aidl from android_stubs_current
172 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
173 case "system_current":
174 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
175 default:
176 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
177 }
178 } else {
179 if j.deviceProperties.Dex {
180 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
181 }
Colin Cross2fe66872015-03-30 17:20:39 -0700182 }
Colin Crossbe1da472017-07-07 15:59:46 -0700183
Colin Cross540eff82017-06-22 17:01:52 -0700184 if ctx.Device() && j.deviceProperties.Sdk_version == "" {
Colin Crossbe1da472017-07-07 15:59:46 -0700185 ctx.AddDependency(ctx.Module(), javaLibTag, config.DefaultLibraries...)
Colin Crossefb9ebe2015-04-16 14:08:06 -0700186 }
Colin Cross2fe66872015-03-30 17:20:39 -0700187 }
Colin Crossbe1da472017-07-07 15:59:46 -0700188 ctx.AddDependency(ctx.Module(), javaLibTag, j.properties.Java_libs...)
189 ctx.AddDependency(ctx.Module(), javaStaticLibTag, j.properties.Java_static_libs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700190}
191
Colin Cross46c9b8b2017-06-22 16:51:17 -0700192func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700193 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700194
Colin Cross540eff82017-06-22 17:01:52 -0700195 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700196
197 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198 if aidlPreprocess.Valid() {
199 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700200 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700201 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700202 }
203
Colin Cross635c3b02016-05-18 15:37:25 -0700204 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
205 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
206 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700207 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
208 flags = append(flags, "-I"+src.String())
209 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700210
Colin Crossf03c82b2015-04-13 13:53:40 -0700211 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700212}
213
Colin Cross46c9b8b2017-06-22 16:51:17 -0700214func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross635c3b02016-05-18 15:37:25 -0700215 bootClasspath android.OptionalPath, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
216 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700217
218 ctx.VisitDirectDeps(func(module blueprint.Module) {
219 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700220 tag := ctx.OtherModuleDependencyTag(module)
221
222 javaDep, _ := module.(JavaDependency)
223 if javaDep == nil {
224 switch tag {
225 case android.DefaultsDepTag, android.SourceDepTag:
226 default:
227 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700228 }
Colin Crossec7a0422017-07-07 14:47:12 -0700229 return
230 }
231
Colin Crossbe1da472017-07-07 15:59:46 -0700232 switch tag {
233 case bootClasspathTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700234 bootClasspath = android.OptionalPathForPath(javaDep.ClasspathFile())
Colin Crossbe1da472017-07-07 15:59:46 -0700235 case javaLibTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700236 classpath = append(classpath, javaDep.ClasspathFile())
Colin Crossbe1da472017-07-07 15:59:46 -0700237 case javaStaticLibTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700238 classpath = append(classpath, javaDep.ClasspathFile())
239 classJarSpecs = append(classJarSpecs, javaDep.ClassJarSpecs()...)
240 resourceJarSpecs = append(resourceJarSpecs, javaDep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700241 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700242 if ctx.ModuleName() == "framework" {
243 // framework.jar has a one-off dependency on the R.java and Manifest.java files
244 // generated by framework-res.apk
245 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
246 }
Colin Crossbe1da472017-07-07 15:59:46 -0700247 case sdkDependencyTag:
248 sdkDep := module.(sdkDependency)
Colin Crossec7a0422017-07-07 14:47:12 -0700249 if sdkDep.AidlPreprocessed().Valid() {
250 if aidlPreprocess.Valid() {
251 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
252 aidlPreprocess, sdkDep.AidlPreprocessed())
253 } else {
254 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700255 }
256 }
Colin Crossbe1da472017-07-07 15:59:46 -0700257 default:
258 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700259 }
Colin Crossbe1da472017-07-07 15:59:46 -0700260
261 aidlIncludeDirs = append(aidlIncludeDirs, javaDep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700262 })
263
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700264 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
265 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700266}
267
Colin Cross46c9b8b2017-06-22 16:51:17 -0700268func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700269
Colin Cross540eff82017-06-22 17:01:52 -0700270 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700271
272 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700273 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700274
Colin Crossf03c82b2015-04-13 13:53:40 -0700275 var flags javaBuilderFlags
276
277 javacFlags := j.properties.Javacflags
278 if len(javacFlags) > 0 {
279 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
280 flags.javacFlags = "$javacFlags"
281 }
282
283 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
284 if len(aidlFlags) > 0 {
285 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
286 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700287 }
288
Colin Cross635c3b02016-05-18 15:37:25 -0700289 var javacDeps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700290
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700291 if bootClasspath.Valid() {
292 flags.bootClasspath = "-bootclasspath " + bootClasspath.String()
293 javacDeps = append(javacDeps, bootClasspath.Path())
Colin Cross2fe66872015-03-30 17:20:39 -0700294 }
295
296 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700297 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Cross2fe66872015-03-30 17:20:39 -0700298 javacDeps = append(javacDeps, classpath...)
299 }
300
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700301 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700302
Colin Crossf05fe972015-04-10 17:45:20 -0700303 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700304
Colin Cross0607cf72015-04-28 13:28:51 -0700305 ctx.VisitDirectDeps(func(module blueprint.Module) {
306 if gen, ok := module.(genrule.SourceFileGenerator); ok {
307 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
308 }
309 })
310
Colin Crossb7a63242015-04-16 14:09:14 -0700311 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
312
Colin Cross8cf13342015-04-10 15:41:49 -0700313 if len(srcFiles) > 0 {
314 // Compile java sources into .class files
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700315 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, javacDeps)
Colin Cross8cf13342015-04-10 15:41:49 -0700316 if ctx.Failed() {
317 return
318 }
319
320 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700321 }
322
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700323 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700324 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700325
Colin Cross635c3b02016-05-18 15:37:25 -0700326 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700327
328 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
329 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
330
331 // Combine classes + resources into classes-full-debug.jar
332 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
333 if ctx.Failed() {
334 return
335 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700336
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700337 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700338 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700339 // Transform classes-full-debug.jar into classes-jarjar.jar
340 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
341 if ctx.Failed() {
342 return
343 }
Colin Cross20978302015-04-10 17:05:07 -0700344
345 classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile)
346 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700347 }
348
Colin Cross20978302015-04-10 17:05:07 -0700349 j.resourceJarSpecs = resourceJarSpecs
350 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700351 j.classpathFile = outputFile
352
Colin Cross540eff82017-06-22 17:01:52 -0700353 if j.deviceProperties.Dex && len(srcFiles) > 0 {
354 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700355 if false /* emma enabled */ {
356 // If you instrument class files that have local variable debug information in
357 // them emma does not correctly maintain the local variable table.
358 // This will cause an error when you try to convert the class files for Android.
359 // The workaround here is to build different dex file here based on emma switch
360 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
361 // option to remove local variable information
362 dxFlags = append(dxFlags, "--no-locals")
363 }
364
Colin Cross1332b002015-04-07 17:11:30 -0700365 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700366 dxFlags = append(dxFlags, "--no-optimize")
367 }
368
Colin Cross1332b002015-04-07 17:11:30 -0700369 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700370 dxFlags = append(dxFlags,
371 "--debug",
372 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700373 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700374 "--dump-width=1000")
375 }
376
377 flags.dxFlags = strings.Join(dxFlags, " ")
378
379 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700380 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700381 if ctx.Failed() {
382 return
383 }
384
385 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700386 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700387 }
Colin Crossb7a63242015-04-16 14:09:14 -0700388 ctx.CheckbuildFile(outputFile)
389 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700390}
391
392var _ JavaDependency = (*JavaLibrary)(nil)
393
Colin Cross46c9b8b2017-06-22 16:51:17 -0700394func (j *Module) ClasspathFile() android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700395 return j.classpathFile
396}
397
Colin Cross46c9b8b2017-06-22 16:51:17 -0700398func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700399 return j.classJarSpecs
400}
401
Colin Cross46c9b8b2017-06-22 16:51:17 -0700402func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700403 return j.resourceJarSpecs
404}
405
Colin Cross46c9b8b2017-06-22 16:51:17 -0700406func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700407 return j.exportAidlIncludeDirs
408}
409
Colin Cross46c9b8b2017-06-22 16:51:17 -0700410var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700411
Colin Cross46c9b8b2017-06-22 16:51:17 -0700412func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700413 return j.logtagsSrcs
414}
415
Colin Cross2fe66872015-03-30 17:20:39 -0700416//
417// Java libraries (.jar file)
418//
419
420type JavaLibrary struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700421 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700422}
423
Colin Cross46c9b8b2017-06-22 16:51:17 -0700424func (j *JavaLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
425 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700426
Colin Cross635c3b02016-05-18 15:37:25 -0700427 j.installFile = ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700428}
429
Colin Cross46c9b8b2017-06-22 16:51:17 -0700430func (j *JavaLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
431 j.deps(ctx)
432}
433
Colin Cross36242852017-06-23 15:06:31 -0700434func JavaLibraryFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700435 module := &JavaLibrary{}
436
Colin Cross540eff82017-06-22 17:01:52 -0700437 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700438
Colin Cross36242852017-06-23 15:06:31 -0700439 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700440 &module.Module.properties,
441 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700442
443 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
444 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700445}
446
Colin Cross36242852017-06-23 15:06:31 -0700447func JavaLibraryHostFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700448 module := &JavaLibrary{}
449
Colin Cross36242852017-06-23 15:06:31 -0700450 module.AddProperties(&module.Module.properties)
451
452 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
453 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700454}
455
456//
457// Java Binaries (.jar file plus wrapper script)
458//
459
Colin Cross7d5136f2015-05-11 13:39:40 -0700460type javaBinaryProperties struct {
461 // installable script to execute the resulting jar
462 Wrapper string
463}
464
Colin Cross2fe66872015-03-30 17:20:39 -0700465type JavaBinary struct {
466 JavaLibrary
467
Colin Cross7d5136f2015-05-11 13:39:40 -0700468 binaryProperties javaBinaryProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700469}
470
Colin Cross46c9b8b2017-06-22 16:51:17 -0700471func (j *JavaBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
472 j.JavaLibrary.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700473
474 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
475 // another build rule before the jar has been installed.
Colin Cross635c3b02016-05-18 15:37:25 -0700476 ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper),
Colin Cross2fe66872015-03-30 17:20:39 -0700477 j.installFile)
478}
479
Colin Cross46c9b8b2017-06-22 16:51:17 -0700480func (j *JavaBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
481 j.deps(ctx)
482}
483
Colin Cross36242852017-06-23 15:06:31 -0700484func JavaBinaryFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700485 module := &JavaBinary{}
486
Colin Cross540eff82017-06-22 17:01:52 -0700487 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700488
Colin Cross36242852017-06-23 15:06:31 -0700489 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700490 &module.Module.properties,
491 &module.Module.deviceProperties,
492 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700493
494 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
495 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700496}
497
Colin Cross36242852017-06-23 15:06:31 -0700498func JavaBinaryHostFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700499 module := &JavaBinary{}
500
Colin Cross36242852017-06-23 15:06:31 -0700501 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700502 &module.Module.properties,
503 &module.Module.deviceProperties,
504 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700505
506 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
507 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700508}
509
510//
511// Java prebuilts
512//
513
514type JavaPrebuilt struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700515 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700516 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700517
Colin Cross635c3b02016-05-18 15:37:25 -0700518 classpathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700519 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700520}
521
Colin Crossec7a0422017-07-07 14:47:12 -0700522func (j *JavaPrebuilt) Prebuilt() *android.Prebuilt {
523 return &j.prebuilt
524}
525
Colin Cross1e676be2016-10-12 14:38:15 -0700526func (j *JavaPrebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
527}
528
Colin Cross635c3b02016-05-18 15:37:25 -0700529func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossbe1da472017-07-07 15:59:46 -0700530 prebuilt := j.prebuilt.Path(ctx)
Colin Crosse1d62a82015-04-03 16:53:05 -0700531
532 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
533
534 j.classpathFile = prebuilt
535 j.classJarSpecs = []jarSpec{classJarSpec}
536 j.resourceJarSpecs = []jarSpec{resourceJarSpec}
Colin Cross635c3b02016-05-18 15:37:25 -0700537 ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.classpathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700538}
539
540var _ JavaDependency = (*JavaPrebuilt)(nil)
541
Colin Cross635c3b02016-05-18 15:37:25 -0700542func (j *JavaPrebuilt) ClasspathFile() android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700543 return j.classpathFile
544}
545
546func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700547 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700548}
549
550func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700551 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700552}
553
Colin Cross635c3b02016-05-18 15:37:25 -0700554func (j *JavaPrebuilt) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700555 return nil
556}
557
Colin Cross36242852017-06-23 15:06:31 -0700558func JavaPrebuiltFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700559 module := &JavaPrebuilt{}
560
Colin Crossbe1da472017-07-07 15:59:46 -0700561 module.AddProperties(&module.prebuilt.Properties)
Colin Cross36242852017-06-23 15:06:31 -0700562
563 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
564 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700565}
566
Colin Crossaa8630b2015-04-13 13:52:22 -0700567//
568// SDK java prebuilts (.jar containing resources plus framework.aidl)
569//
570
571type sdkDependency interface {
572 JavaDependency
Colin Cross635c3b02016-05-18 15:37:25 -0700573 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700574}
575
576var _ sdkDependency = (*sdkPrebuilt)(nil)
577
Colin Cross7d5136f2015-05-11 13:39:40 -0700578type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700579 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700580}
581
Colin Crossaa8630b2015-04-13 13:52:22 -0700582type sdkPrebuilt struct {
583 JavaPrebuilt
584
Colin Cross7d5136f2015-05-11 13:39:40 -0700585 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700586
Colin Cross635c3b02016-05-18 15:37:25 -0700587 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700588}
589
Colin Cross635c3b02016-05-18 15:37:25 -0700590func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossaa8630b2015-04-13 13:52:22 -0700591 j.JavaPrebuilt.GenerateAndroidBuildActions(ctx)
592
Colin Cross635c3b02016-05-18 15:37:25 -0700593 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700594}
595
Colin Cross635c3b02016-05-18 15:37:25 -0700596func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700597 return j.aidlPreprocessed
598}
599
Colin Cross36242852017-06-23 15:06:31 -0700600func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700601 module := &sdkPrebuilt{}
602
Colin Cross36242852017-06-23 15:06:31 -0700603 module.AddProperties(
Colin Crossbe1da472017-07-07 15:59:46 -0700604 &module.prebuilt.Properties,
Colin Cross36242852017-06-23 15:06:31 -0700605 &module.sdkProperties)
606
607 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
608 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700609}
610
Colin Cross2fe66872015-03-30 17:20:39 -0700611func inList(s string, l []string) bool {
612 for _, e := range l {
613 if e == s {
614 return true
615 }
616 }
617 return false
618}