blob: adb3d7bdb1f72e2b621b53c03a61f0d57ea5bd19 [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 Cross46c9b8b2017-06-22 16:51:17 -0700150func (j *Module) BootClasspath(ctx android.BaseContext) string {
Colin Cross2fe66872015-03-30 17:20:39 -0700151 if ctx.Device() {
Colin Cross540eff82017-06-22 17:01:52 -0700152 switch j.deviceProperties.Sdk_version {
153 case "":
Colin Cross2fe66872015-03-30 17:20:39 -0700154 return "core-libart"
Colin Cross540eff82017-06-22 17:01:52 -0700155 case "current":
Colin Cross2fe66872015-03-30 17:20:39 -0700156 // TODO: !TARGET_BUILD_APPS
Colin Crossc0b06f12015-04-08 13:03:43 -0700157 // TODO: export preprocessed framework.aidl from android_stubs_current
Colin Cross2fe66872015-03-30 17:20:39 -0700158 return "android_stubs_current"
Colin Cross540eff82017-06-22 17:01:52 -0700159 case "system_current":
Colin Cross2fe66872015-03-30 17:20:39 -0700160 return "android_system_stubs_current"
Colin Cross540eff82017-06-22 17:01:52 -0700161 default:
162 return "sdk_v" + j.deviceProperties.Sdk_version
Colin Cross2fe66872015-03-30 17:20:39 -0700163 }
164 } else {
Colin Cross540eff82017-06-22 17:01:52 -0700165 if j.deviceProperties.Dex {
Colin Cross2fe66872015-03-30 17:20:39 -0700166 return "core-libart"
167 } else {
168 return ""
169 }
170 }
171}
172
Colin Cross46c9b8b2017-06-22 16:51:17 -0700173func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700174 var deps []string
175
176 if !j.properties.No_standard_libraries {
177 bootClasspath := j.BootClasspath(ctx)
178 if bootClasspath != "" {
179 deps = append(deps, bootClasspath)
180 }
Colin Cross540eff82017-06-22 17:01:52 -0700181 if ctx.Device() && j.deviceProperties.Sdk_version == "" {
Colin Cross3e3e72d2017-06-22 17:20:19 -0700182 deps = append(deps, config.DefaultLibraries...)
Colin Crossefb9ebe2015-04-16 14:08:06 -0700183 }
Colin Cross2fe66872015-03-30 17:20:39 -0700184 }
185 deps = append(deps, j.properties.Java_libs...)
186 deps = append(deps, j.properties.Java_static_libs...)
187
Colin Cross46c9b8b2017-06-22 16:51:17 -0700188 ctx.AddDependency(ctx.Module(), nil, deps...)
Colin Cross2fe66872015-03-30 17:20:39 -0700189}
190
Colin Cross46c9b8b2017-06-22 16:51:17 -0700191func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700192 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700193
Colin Cross540eff82017-06-22 17:01:52 -0700194 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700195
196 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700197 if aidlPreprocess.Valid() {
198 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700199 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700200 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700201 }
202
Colin Cross635c3b02016-05-18 15:37:25 -0700203 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
204 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
205 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700206 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
207 flags = append(flags, "-I"+src.String())
208 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700209
Colin Crossf03c82b2015-04-13 13:53:40 -0700210 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700211}
212
Colin Cross46c9b8b2017-06-22 16:51:17 -0700213func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross635c3b02016-05-18 15:37:25 -0700214 bootClasspath android.OptionalPath, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
215 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700216
217 ctx.VisitDirectDeps(func(module blueprint.Module) {
218 otherName := ctx.OtherModuleName(module)
219 if javaDep, ok := module.(JavaDependency); ok {
Colin Cross6cbb1272015-04-08 11:23:01 -0700220 if otherName == j.BootClasspath(ctx) {
Colin Cross635c3b02016-05-18 15:37:25 -0700221 bootClasspath = android.OptionalPathForPath(javaDep.ClasspathFile())
Colin Cross3e3e72d2017-06-22 17:20:19 -0700222 } else if inList(otherName, config.DefaultLibraries) {
Colin Crossb7a63242015-04-16 14:09:14 -0700223 classpath = append(classpath, javaDep.ClasspathFile())
Colin Cross6cbb1272015-04-08 11:23:01 -0700224 } else if inList(otherName, j.properties.Java_libs) {
Colin Cross2fe66872015-03-30 17:20:39 -0700225 classpath = append(classpath, javaDep.ClasspathFile())
226 } else if inList(otherName, j.properties.Java_static_libs) {
227 classpath = append(classpath, javaDep.ClasspathFile())
228 classJarSpecs = append(classJarSpecs, javaDep.ClassJarSpecs()...)
229 resourceJarSpecs = append(resourceJarSpecs, javaDep.ResourceJarSpecs()...)
Colin Crossb7a63242015-04-16 14:09:14 -0700230 } else if otherName == "framework-res" {
231 if ctx.ModuleName() == "framework" {
232 // framework.jar has a one-off dependency on the R.java and Manifest.java files
233 // generated by framework-res.apk
Colin Cross46c9b8b2017-06-22 16:51:17 -0700234 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossb7a63242015-04-16 14:09:14 -0700235 }
Colin Cross2fe66872015-03-30 17:20:39 -0700236 } else {
237 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
238 }
Colin Crossaa8630b2015-04-13 13:52:22 -0700239 aidlIncludeDirs = append(aidlIncludeDirs, javaDep.AidlIncludeDirs()...)
240 if sdkDep, ok := module.(sdkDependency); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700241 if sdkDep.AidlPreprocessed().Valid() {
242 if aidlPreprocess.Valid() {
Colin Crossaa8630b2015-04-13 13:52:22 -0700243 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
244 aidlPreprocess, sdkDep.AidlPreprocessed())
245 } else {
246 aidlPreprocess = sdkDep.AidlPreprocessed()
247 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700248 }
249 }
Colin Cross2fe66872015-03-30 17:20:39 -0700250 }
251 })
252
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700253 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
254 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700255}
256
Colin Cross46c9b8b2017-06-22 16:51:17 -0700257func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700258
Colin Cross540eff82017-06-22 17:01:52 -0700259 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700260
261 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700262 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700263
Colin Crossf03c82b2015-04-13 13:53:40 -0700264 var flags javaBuilderFlags
265
266 javacFlags := j.properties.Javacflags
267 if len(javacFlags) > 0 {
268 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
269 flags.javacFlags = "$javacFlags"
270 }
271
272 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
273 if len(aidlFlags) > 0 {
274 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
275 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700276 }
277
Colin Cross635c3b02016-05-18 15:37:25 -0700278 var javacDeps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700279
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700280 if bootClasspath.Valid() {
281 flags.bootClasspath = "-bootclasspath " + bootClasspath.String()
282 javacDeps = append(javacDeps, bootClasspath.Path())
Colin Cross2fe66872015-03-30 17:20:39 -0700283 }
284
285 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700286 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Cross2fe66872015-03-30 17:20:39 -0700287 javacDeps = append(javacDeps, classpath...)
288 }
289
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700290 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700291
Colin Crossf05fe972015-04-10 17:45:20 -0700292 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700293
Colin Cross0607cf72015-04-28 13:28:51 -0700294 ctx.VisitDirectDeps(func(module blueprint.Module) {
295 if gen, ok := module.(genrule.SourceFileGenerator); ok {
296 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
297 }
298 })
299
Colin Crossb7a63242015-04-16 14:09:14 -0700300 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
301
Colin Cross8cf13342015-04-10 15:41:49 -0700302 if len(srcFiles) > 0 {
303 // Compile java sources into .class files
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700304 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, javacDeps)
Colin Cross8cf13342015-04-10 15:41:49 -0700305 if ctx.Failed() {
306 return
307 }
308
309 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700310 }
311
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700312 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700313 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700314
Colin Cross635c3b02016-05-18 15:37:25 -0700315 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700316
317 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
318 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
319
320 // Combine classes + resources into classes-full-debug.jar
321 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
322 if ctx.Failed() {
323 return
324 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700325
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700326 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700327 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700328 // Transform classes-full-debug.jar into classes-jarjar.jar
329 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
330 if ctx.Failed() {
331 return
332 }
Colin Cross20978302015-04-10 17:05:07 -0700333
334 classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile)
335 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700336 }
337
Colin Cross20978302015-04-10 17:05:07 -0700338 j.resourceJarSpecs = resourceJarSpecs
339 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700340 j.classpathFile = outputFile
341
Colin Cross540eff82017-06-22 17:01:52 -0700342 if j.deviceProperties.Dex && len(srcFiles) > 0 {
343 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700344 if false /* emma enabled */ {
345 // If you instrument class files that have local variable debug information in
346 // them emma does not correctly maintain the local variable table.
347 // This will cause an error when you try to convert the class files for Android.
348 // The workaround here is to build different dex file here based on emma switch
349 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
350 // option to remove local variable information
351 dxFlags = append(dxFlags, "--no-locals")
352 }
353
Colin Cross1332b002015-04-07 17:11:30 -0700354 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700355 dxFlags = append(dxFlags, "--no-optimize")
356 }
357
Colin Cross1332b002015-04-07 17:11:30 -0700358 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700359 dxFlags = append(dxFlags,
360 "--debug",
361 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700362 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700363 "--dump-width=1000")
364 }
365
366 flags.dxFlags = strings.Join(dxFlags, " ")
367
368 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700369 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700370 if ctx.Failed() {
371 return
372 }
373
374 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700375 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700376 }
Colin Crossb7a63242015-04-16 14:09:14 -0700377 ctx.CheckbuildFile(outputFile)
378 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700379}
380
381var _ JavaDependency = (*JavaLibrary)(nil)
382
Colin Cross46c9b8b2017-06-22 16:51:17 -0700383func (j *Module) ClasspathFile() android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700384 return j.classpathFile
385}
386
Colin Cross46c9b8b2017-06-22 16:51:17 -0700387func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700388 return j.classJarSpecs
389}
390
Colin Cross46c9b8b2017-06-22 16:51:17 -0700391func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700392 return j.resourceJarSpecs
393}
394
Colin Cross46c9b8b2017-06-22 16:51:17 -0700395func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700396 return j.exportAidlIncludeDirs
397}
398
Colin Cross46c9b8b2017-06-22 16:51:17 -0700399var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700400
Colin Cross46c9b8b2017-06-22 16:51:17 -0700401func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700402 return j.logtagsSrcs
403}
404
Colin Cross2fe66872015-03-30 17:20:39 -0700405//
406// Java libraries (.jar file)
407//
408
409type JavaLibrary struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700410 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700411}
412
Colin Cross46c9b8b2017-06-22 16:51:17 -0700413func (j *JavaLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
414 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700415
Colin Cross635c3b02016-05-18 15:37:25 -0700416 j.installFile = ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700417}
418
Colin Cross46c9b8b2017-06-22 16:51:17 -0700419func (j *JavaLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
420 j.deps(ctx)
421}
422
Colin Cross36242852017-06-23 15:06:31 -0700423func JavaLibraryFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700424 module := &JavaLibrary{}
425
Colin Cross540eff82017-06-22 17:01:52 -0700426 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700427
Colin Cross36242852017-06-23 15:06:31 -0700428 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700429 &module.Module.properties,
430 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700431
432 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
433 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700434}
435
Colin Cross36242852017-06-23 15:06:31 -0700436func JavaLibraryHostFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700437 module := &JavaLibrary{}
438
Colin Cross36242852017-06-23 15:06:31 -0700439 module.AddProperties(&module.Module.properties)
440
441 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
442 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700443}
444
445//
446// Java Binaries (.jar file plus wrapper script)
447//
448
Colin Cross7d5136f2015-05-11 13:39:40 -0700449type javaBinaryProperties struct {
450 // installable script to execute the resulting jar
451 Wrapper string
452}
453
Colin Cross2fe66872015-03-30 17:20:39 -0700454type JavaBinary struct {
455 JavaLibrary
456
Colin Cross7d5136f2015-05-11 13:39:40 -0700457 binaryProperties javaBinaryProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700458}
459
Colin Cross46c9b8b2017-06-22 16:51:17 -0700460func (j *JavaBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
461 j.JavaLibrary.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700462
463 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
464 // another build rule before the jar has been installed.
Colin Cross635c3b02016-05-18 15:37:25 -0700465 ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper),
Colin Cross2fe66872015-03-30 17:20:39 -0700466 j.installFile)
467}
468
Colin Cross46c9b8b2017-06-22 16:51:17 -0700469func (j *JavaBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
470 j.deps(ctx)
471}
472
Colin Cross36242852017-06-23 15:06:31 -0700473func JavaBinaryFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700474 module := &JavaBinary{}
475
Colin Cross540eff82017-06-22 17:01:52 -0700476 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700477
Colin Cross36242852017-06-23 15:06:31 -0700478 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700479 &module.Module.properties,
480 &module.Module.deviceProperties,
481 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700482
483 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
484 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700485}
486
Colin Cross36242852017-06-23 15:06:31 -0700487func JavaBinaryHostFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700488 module := &JavaBinary{}
489
Colin Cross36242852017-06-23 15:06:31 -0700490 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700491 &module.Module.properties,
492 &module.Module.deviceProperties,
493 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700494
495 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
496 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700497}
498
499//
500// Java prebuilts
501//
502
Colin Cross7d5136f2015-05-11 13:39:40 -0700503type javaPrebuiltProperties struct {
504 Srcs []string
505}
506
Colin Cross2fe66872015-03-30 17:20:39 -0700507type JavaPrebuilt struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700508 android.ModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700509
Colin Cross7d5136f2015-05-11 13:39:40 -0700510 properties javaPrebuiltProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700511
Colin Cross635c3b02016-05-18 15:37:25 -0700512 classpathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700513 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700514}
515
Colin Cross1e676be2016-10-12 14:38:15 -0700516func (j *JavaPrebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
517}
518
Colin Cross635c3b02016-05-18 15:37:25 -0700519func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700520 if len(j.properties.Srcs) != 1 {
521 ctx.ModuleErrorf("expected exactly one jar in srcs")
522 return
523 }
Colin Cross635c3b02016-05-18 15:37:25 -0700524 prebuilt := android.PathForModuleSrc(ctx, j.properties.Srcs[0])
Colin Crosse1d62a82015-04-03 16:53:05 -0700525
526 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
527
528 j.classpathFile = prebuilt
529 j.classJarSpecs = []jarSpec{classJarSpec}
530 j.resourceJarSpecs = []jarSpec{resourceJarSpec}
Colin Cross635c3b02016-05-18 15:37:25 -0700531 ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.classpathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700532}
533
534var _ JavaDependency = (*JavaPrebuilt)(nil)
535
Colin Cross635c3b02016-05-18 15:37:25 -0700536func (j *JavaPrebuilt) ClasspathFile() android.Path {
Colin Cross2fe66872015-03-30 17:20:39 -0700537 return j.classpathFile
538}
539
540func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700541 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700542}
543
544func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700545 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700546}
547
Colin Cross635c3b02016-05-18 15:37:25 -0700548func (j *JavaPrebuilt) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700549 return nil
550}
551
Colin Cross36242852017-06-23 15:06:31 -0700552func JavaPrebuiltFactory() android.Module {
Colin Cross2fe66872015-03-30 17:20:39 -0700553 module := &JavaPrebuilt{}
554
Colin Cross36242852017-06-23 15:06:31 -0700555 module.AddProperties(&module.properties)
556
557 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
558 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700559}
560
Colin Crossaa8630b2015-04-13 13:52:22 -0700561//
562// SDK java prebuilts (.jar containing resources plus framework.aidl)
563//
564
565type sdkDependency interface {
566 JavaDependency
Colin Cross635c3b02016-05-18 15:37:25 -0700567 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700568}
569
570var _ sdkDependency = (*sdkPrebuilt)(nil)
571
Colin Cross7d5136f2015-05-11 13:39:40 -0700572type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700573 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700574}
575
Colin Crossaa8630b2015-04-13 13:52:22 -0700576type sdkPrebuilt struct {
577 JavaPrebuilt
578
Colin Cross7d5136f2015-05-11 13:39:40 -0700579 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700580
Colin Cross635c3b02016-05-18 15:37:25 -0700581 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700582}
583
Colin Cross635c3b02016-05-18 15:37:25 -0700584func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossaa8630b2015-04-13 13:52:22 -0700585 j.JavaPrebuilt.GenerateAndroidBuildActions(ctx)
586
Colin Cross635c3b02016-05-18 15:37:25 -0700587 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700588}
589
Colin Cross635c3b02016-05-18 15:37:25 -0700590func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700591 return j.aidlPreprocessed
592}
593
Colin Cross36242852017-06-23 15:06:31 -0700594func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700595 module := &sdkPrebuilt{}
596
Colin Cross36242852017-06-23 15:06:31 -0700597 module.AddProperties(
598 &module.properties,
599 &module.sdkProperties)
600
601 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
602 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700603}
604
Colin Cross2fe66872015-03-30 17:20:39 -0700605func inList(s string, l []string) bool {
606 for _, e := range l {
607 if e == s {
608 return true
609 }
610 }
611 return false
612}