blob: ac88020cc3e370b1dc7de26d8bf4988987b8943c [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
22 "fmt"
Colin Cross74d73e22017-08-02 11:05:49 -070023 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross2fe66872015-03-30 17:20:39 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070029 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070030 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross89536d42017-07-07 14:35:50 -070034 android.RegisterModuleType("java_defaults", defaultsFactory)
35
Colin Crossf506d872017-07-19 15:53:04 -070036 android.RegisterModuleType("java_library", LibraryFactory)
37 android.RegisterModuleType("java_library_static", LibraryFactory)
38 android.RegisterModuleType("java_library_host", LibraryHostFactory)
39 android.RegisterModuleType("java_binary", BinaryFactory)
40 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070041 android.RegisterModuleType("java_import", ImportFactory)
42 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Crosse8dc34a2017-07-19 11:22:16 -070043 android.RegisterModuleType("android_prebuilt_sdk", SdkPrebuiltFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070045
Colin Cross798bfce2016-10-12 14:28:16 -070046 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070047}
48
Colin Cross2fe66872015-03-30 17:20:39 -070049// TODO:
50// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070051// Proto
52// Renderscript
53// Post-jar passes:
54// Proguard
Colin Crossba211132017-06-22 15:36:39 -070055// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070056// Jarjar
57// Dex
58// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070059// DroidDoc
60// Findbugs
61
Colin Cross89536d42017-07-07 14:35:50 -070062type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070063 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
64 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070065 Srcs []string `android:"arch_variant"`
66
67 // list of source files that should not be used to build the Java module.
68 // This is most useful in the arch/multilib variants to remove non-common files
69 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070070
71 // list of directories containing Java resources
Colin Crosse8dc34a2017-07-19 11:22:16 -070072 Resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070073
Colin Crosse8dc34a2017-07-19 11:22:16 -070074 // list of directories that should be excluded from resource_dirs
75 Exclude_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070076
Paul Duffin2b67e3b2016-11-30 16:13:09 +000077 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070078 // ext, and framework for device targets)
79 No_standard_libraries bool
80
81 // list of module-specific flags that will be used for javac compiles
82 Javacflags []string `android:"arch_variant"`
83
Colin Cross7d5136f2015-05-11 13:39:40 -070084 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070085 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070086
87 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070088 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070089
90 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070092
Colin Cross540eff82017-06-22 17:01:52 -070093 // if not blank, run jarjar using the specified rules file
94 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -070095
96 // If not blank, set the java version passed to javac as -source and -target
97 Java_version *string
Colin Cross540eff82017-06-22 17:01:52 -070098}
99
Colin Cross89536d42017-07-07 14:35:50 -0700100type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700101 // list of module-specific flags that will be used for dex compiles
102 Dxflags []string `android:"arch_variant"`
103
Colin Cross7d5136f2015-05-11 13:39:40 -0700104 // if not blank, set to the version of the sdk to compile against
105 Sdk_version string
106
107 // Set for device java libraries, and for host versions of device java libraries
108 // built for testing
109 Dex bool `blueprint:"mutated"`
110
Colin Cross7d5136f2015-05-11 13:39:40 -0700111 // directories to pass to aidl tool
112 Aidl_includes []string
113
114 // directories that should be added as include directories
115 // for any aidl sources of modules that depend on this module
116 Export_aidl_include_dirs []string
117}
118
Colin Cross46c9b8b2017-06-22 16:51:17 -0700119// Module contains the properties and members used by all java module types
120type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700121 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700122 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700123
Colin Cross89536d42017-07-07 14:35:50 -0700124 properties CompilerProperties
125 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700126
127 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700128 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700129
Colin Crossb7a63242015-04-16 14:09:14 -0700130 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700131 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700132
Colin Cross2fe66872015-03-30 17:20:39 -0700133 // jarSpecs suitable for inserting classes from a static library into another jar
134 classJarSpecs []jarSpec
135
136 // jarSpecs suitable for inserting resources from a static library into another jar
137 resourceJarSpecs []jarSpec
138
Colin Cross635c3b02016-05-18 15:37:25 -0700139 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700140
Colin Cross635c3b02016-05-18 15:37:25 -0700141 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700142
Colin Crossb7a63242015-04-16 14:09:14 -0700143 // filelists of extra source files that should be included in the javac command line,
144 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700145 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700146
Colin Cross2fe66872015-03-30 17:20:39 -0700147 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700148 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700149}
150
Colin Crossf506d872017-07-19 15:53:04 -0700151type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700152 ClasspathFiles() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700153 ClassJarSpecs() []jarSpec
154 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700155 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700156}
157
Colin Cross89536d42017-07-07 14:35:50 -0700158func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
159 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
160 android.InitDefaultableModule(module)
161}
162
Colin Crossbe1da472017-07-07 15:59:46 -0700163type dependencyTag struct {
164 blueprint.BaseDependencyTag
165 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700166}
167
Colin Crossbe1da472017-07-07 15:59:46 -0700168var (
Colin Crossf506d872017-07-19 15:53:04 -0700169 staticLibTag = dependencyTag{name: "staticlib"}
170 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700171 bootClasspathTag = dependencyTag{name: "bootclasspath"}
172 frameworkResTag = dependencyTag{name: "framework-res"}
173 sdkDependencyTag = dependencyTag{name: "sdk"}
174)
Colin Cross2fe66872015-03-30 17:20:39 -0700175
Colin Crossbe1da472017-07-07 15:59:46 -0700176func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700177 if !j.properties.No_standard_libraries {
Colin Crossbe1da472017-07-07 15:59:46 -0700178 if ctx.Device() {
179 switch j.deviceProperties.Sdk_version {
180 case "":
181 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
182 case "current":
183 // TODO: !TARGET_BUILD_APPS
184 // TODO: export preprocessed framework.aidl from android_stubs_current
185 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
186 case "system_current":
187 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
188 default:
189 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
190 }
191 } else {
192 if j.deviceProperties.Dex {
193 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
194 }
Colin Cross2fe66872015-03-30 17:20:39 -0700195 }
Colin Crossbe1da472017-07-07 15:59:46 -0700196
Colin Cross540eff82017-06-22 17:01:52 -0700197 if ctx.Device() && j.deviceProperties.Sdk_version == "" {
Colin Crossf506d872017-07-19 15:53:04 -0700198 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossefb9ebe2015-04-16 14:08:06 -0700199 }
Colin Cross2fe66872015-03-30 17:20:39 -0700200 }
Colin Crossf506d872017-07-19 15:53:04 -0700201 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
202 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700203}
204
Colin Cross46c9b8b2017-06-22 16:51:17 -0700205func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700206 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700207
Colin Cross540eff82017-06-22 17:01:52 -0700208 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700209
210 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700211 if aidlPreprocess.Valid() {
212 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700213 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700214 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700215 }
216
Colin Cross635c3b02016-05-18 15:37:25 -0700217 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
218 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
219 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700220 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
221 flags = append(flags, "-I"+src.String())
222 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700223
Colin Crossf03c82b2015-04-13 13:53:40 -0700224 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700225}
226
Colin Cross46c9b8b2017-06-22 16:51:17 -0700227func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross74d73e22017-08-02 11:05:49 -0700228 bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700229 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700230
231 ctx.VisitDirectDeps(func(module blueprint.Module) {
232 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700233 tag := ctx.OtherModuleDependencyTag(module)
234
Colin Crossf506d872017-07-19 15:53:04 -0700235 dep, _ := module.(Dependency)
236 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700237 switch tag {
238 case android.DefaultsDepTag, android.SourceDepTag:
239 default:
240 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700241 }
Colin Crossec7a0422017-07-07 14:47:12 -0700242 return
243 }
244
Colin Crossbe1da472017-07-07 15:59:46 -0700245 switch tag {
246 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700247 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700248 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700249 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700250 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700251 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700252 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
253 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700254 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700255 if ctx.ModuleName() == "framework" {
256 // framework.jar has a one-off dependency on the R.java and Manifest.java files
257 // generated by framework-res.apk
258 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
259 }
Colin Crossbe1da472017-07-07 15:59:46 -0700260 case sdkDependencyTag:
261 sdkDep := module.(sdkDependency)
Colin Crossec7a0422017-07-07 14:47:12 -0700262 if sdkDep.AidlPreprocessed().Valid() {
263 if aidlPreprocess.Valid() {
264 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
265 aidlPreprocess, sdkDep.AidlPreprocessed())
266 } else {
267 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700268 }
269 }
Colin Crossbe1da472017-07-07 15:59:46 -0700270 default:
271 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700272 }
Colin Crossbe1da472017-07-07 15:59:46 -0700273
Colin Crossf506d872017-07-19 15:53:04 -0700274 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700275 })
276
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700277 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
278 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700279}
280
Colin Cross46c9b8b2017-06-22 16:51:17 -0700281func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700282
Colin Cross540eff82017-06-22 17:01:52 -0700283 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700284
285 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700286 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700287
Colin Crossf03c82b2015-04-13 13:53:40 -0700288 var flags javaBuilderFlags
289
290 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700291
292 if j.properties.Java_version != nil {
293 flags.javaVersion = *j.properties.Java_version
294 } else {
295 flags.javaVersion = "${config.DefaultJavaVersion}"
296 }
297
Colin Crossf03c82b2015-04-13 13:53:40 -0700298 if len(javacFlags) > 0 {
299 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
300 flags.javacFlags = "$javacFlags"
301 }
302
303 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
304 if len(aidlFlags) > 0 {
305 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
306 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700307 }
308
Colin Crossf506d872017-07-19 15:53:04 -0700309 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700310
Colin Cross74d73e22017-08-02 11:05:49 -0700311 if len(bootClasspath) > 0 {
312 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
313 deps = append(deps, bootClasspath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700314 }
315
316 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700317 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700318 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700319 }
320
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700321 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700322
Colin Crossf05fe972015-04-10 17:45:20 -0700323 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700324
Colin Cross0607cf72015-04-28 13:28:51 -0700325 ctx.VisitDirectDeps(func(module blueprint.Module) {
326 if gen, ok := module.(genrule.SourceFileGenerator); ok {
327 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
328 }
329 })
330
Colin Crossb7a63242015-04-16 14:09:14 -0700331 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
332
Colin Cross8cf13342015-04-10 15:41:49 -0700333 if len(srcFiles) > 0 {
334 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700335 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700336 if ctx.Failed() {
337 return
338 }
339
340 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700341 }
342
Colin Crosse8dc34a2017-07-19 11:22:16 -0700343 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700344 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700345
Colin Cross635c3b02016-05-18 15:37:25 -0700346 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700347
348 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
349 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
350
351 // Combine classes + resources into classes-full-debug.jar
352 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
353 if ctx.Failed() {
354 return
355 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700356
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700357 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700358 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700359 // Transform classes-full-debug.jar into classes-jarjar.jar
360 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
361 if ctx.Failed() {
362 return
363 }
Colin Cross20978302015-04-10 17:05:07 -0700364
Colin Cross74d73e22017-08-02 11:05:49 -0700365 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700366 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700367 }
368
Colin Cross20978302015-04-10 17:05:07 -0700369 j.resourceJarSpecs = resourceJarSpecs
370 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700371 j.classpathFile = outputFile
372
Colin Cross540eff82017-06-22 17:01:52 -0700373 if j.deviceProperties.Dex && len(srcFiles) > 0 {
374 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700375 if false /* emma enabled */ {
376 // If you instrument class files that have local variable debug information in
377 // them emma does not correctly maintain the local variable table.
378 // This will cause an error when you try to convert the class files for Android.
379 // The workaround here is to build different dex file here based on emma switch
380 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
381 // option to remove local variable information
382 dxFlags = append(dxFlags, "--no-locals")
383 }
384
Colin Cross1332b002015-04-07 17:11:30 -0700385 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700386 dxFlags = append(dxFlags, "--no-optimize")
387 }
388
Colin Cross1332b002015-04-07 17:11:30 -0700389 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700390 dxFlags = append(dxFlags,
391 "--debug",
392 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700393 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700394 "--dump-width=1000")
395 }
396
397 flags.dxFlags = strings.Join(dxFlags, " ")
398
399 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700400 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700401 if ctx.Failed() {
402 return
403 }
404
405 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700406 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700407 }
Colin Crossb7a63242015-04-16 14:09:14 -0700408 ctx.CheckbuildFile(outputFile)
409 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700410}
411
Colin Crossf506d872017-07-19 15:53:04 -0700412var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700413
Colin Cross74d73e22017-08-02 11:05:49 -0700414func (j *Module) ClasspathFiles() android.Paths {
415 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700416}
417
Colin Cross46c9b8b2017-06-22 16:51:17 -0700418func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700419 return j.classJarSpecs
420}
421
Colin Cross46c9b8b2017-06-22 16:51:17 -0700422func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700423 return j.resourceJarSpecs
424}
425
Colin Cross46c9b8b2017-06-22 16:51:17 -0700426func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700427 return j.exportAidlIncludeDirs
428}
429
Colin Cross46c9b8b2017-06-22 16:51:17 -0700430var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700431
Colin Cross46c9b8b2017-06-22 16:51:17 -0700432func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700433 return j.logtagsSrcs
434}
435
Colin Cross2fe66872015-03-30 17:20:39 -0700436//
437// Java libraries (.jar file)
438//
439
Colin Crossf506d872017-07-19 15:53:04 -0700440type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700441 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700442}
443
Colin Crossf506d872017-07-19 15:53:04 -0700444func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700445 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700446
Colin Cross635c3b02016-05-18 15:37:25 -0700447 j.installFile = ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700448}
449
Colin Crossf506d872017-07-19 15:53:04 -0700450func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700451 j.deps(ctx)
452}
453
Colin Crossf506d872017-07-19 15:53:04 -0700454func LibraryFactory() android.Module {
455 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700456
Colin Cross540eff82017-06-22 17:01:52 -0700457 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700458
Colin Cross36242852017-06-23 15:06:31 -0700459 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700460 &module.Module.properties,
461 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700462
Colin Cross89536d42017-07-07 14:35:50 -0700463 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700464 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700465}
466
Colin Crossf506d872017-07-19 15:53:04 -0700467func LibraryHostFactory() android.Module {
468 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700469
Colin Cross36242852017-06-23 15:06:31 -0700470 module.AddProperties(&module.Module.properties)
471
Colin Cross89536d42017-07-07 14:35:50 -0700472 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700473 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700474}
475
476//
477// Java Binaries (.jar file plus wrapper script)
478//
479
Colin Crossf506d872017-07-19 15:53:04 -0700480type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700481 // installable script to execute the resulting jar
482 Wrapper string
483}
484
Colin Crossf506d872017-07-19 15:53:04 -0700485type Binary struct {
486 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700487
Colin Crossf506d872017-07-19 15:53:04 -0700488 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700489
490 wrapperFile android.ModuleSrcPath
491 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700492}
493
Colin Crossf506d872017-07-19 15:53:04 -0700494func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
495 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700496
497 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
498 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700499 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
500 j.binaryFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"),
501 j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700502}
503
Colin Crossf506d872017-07-19 15:53:04 -0700504func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700505 j.deps(ctx)
506}
507
Colin Crossf506d872017-07-19 15:53:04 -0700508func BinaryFactory() android.Module {
509 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700510
Colin Cross540eff82017-06-22 17:01:52 -0700511 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700512
Colin Cross36242852017-06-23 15:06:31 -0700513 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700514 &module.Module.properties,
515 &module.Module.deviceProperties,
516 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700517
Colin Cross89536d42017-07-07 14:35:50 -0700518 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700519 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700520}
521
Colin Crossf506d872017-07-19 15:53:04 -0700522func BinaryHostFactory() android.Module {
523 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700524
Colin Cross36242852017-06-23 15:06:31 -0700525 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700526 &module.Module.properties,
527 &module.Module.deviceProperties,
528 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700529
Colin Cross89536d42017-07-07 14:35:50 -0700530 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700531 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700532}
533
534//
535// Java prebuilts
536//
537
Colin Cross74d73e22017-08-02 11:05:49 -0700538type ImportProperties struct {
539 Jars []string
540}
541
542type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700543 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700544 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700545
Colin Cross74d73e22017-08-02 11:05:49 -0700546 properties ImportProperties
547
548 classpathFiles android.Paths
549 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700550 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700551}
552
Colin Cross74d73e22017-08-02 11:05:49 -0700553func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700554 return &j.prebuilt
555}
556
Colin Cross74d73e22017-08-02 11:05:49 -0700557func (j *Import) PrebuiltSrcs() []string {
558 return j.properties.Jars
559}
560
561func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700562 return j.prebuilt.Name(j.ModuleBase.Name())
563}
564
Colin Cross74d73e22017-08-02 11:05:49 -0700565func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700566}
567
Colin Cross74d73e22017-08-02 11:05:49 -0700568func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
569 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700570
Colin Cross74d73e22017-08-02 11:05:49 -0700571 for i, prebuilt := range j.classpathFiles {
572 subdir := "extracted" + strconv.Itoa(i)
573 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
574 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
575 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
576 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700577
Colin Cross74d73e22017-08-02 11:05:49 -0700578 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{})
579
580 ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"),
581 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700582}
583
Colin Cross74d73e22017-08-02 11:05:49 -0700584var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700585
Colin Cross74d73e22017-08-02 11:05:49 -0700586func (j *Import) ClasspathFiles() android.Paths {
587 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700588}
589
Colin Cross74d73e22017-08-02 11:05:49 -0700590func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700591 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700592}
593
Colin Cross74d73e22017-08-02 11:05:49 -0700594func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700595 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700596}
597
Colin Cross74d73e22017-08-02 11:05:49 -0700598func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700599 return nil
600}
601
Colin Cross74d73e22017-08-02 11:05:49 -0700602var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700603
Colin Cross74d73e22017-08-02 11:05:49 -0700604func ImportFactory() android.Module {
605 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700606
Colin Cross74d73e22017-08-02 11:05:49 -0700607 module.AddProperties(&module.properties)
608
609 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700610 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
611 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700612}
613
Colin Cross74d73e22017-08-02 11:05:49 -0700614func ImportFactoryHost() android.Module {
615 module := &Import{}
616
617 module.AddProperties(&module.properties)
618
619 android.InitPrebuiltModule(module, &module.properties.Jars)
620 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
621 return module
622}
623
Colin Crossaa8630b2015-04-13 13:52:22 -0700624//
625// SDK java prebuilts (.jar containing resources plus framework.aidl)
626//
627
628type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700629 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700630 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700631}
632
633var _ sdkDependency = (*sdkPrebuilt)(nil)
634
Colin Cross7d5136f2015-05-11 13:39:40 -0700635type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700636 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700637}
638
Colin Crossaa8630b2015-04-13 13:52:22 -0700639type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700640 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700641
Colin Cross7d5136f2015-05-11 13:39:40 -0700642 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700643
Colin Cross635c3b02016-05-18 15:37:25 -0700644 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700645}
646
Colin Cross635c3b02016-05-18 15:37:25 -0700647func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700648 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700649
Colin Cross635c3b02016-05-18 15:37:25 -0700650 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700651}
652
Colin Cross635c3b02016-05-18 15:37:25 -0700653func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700654 return j.aidlPreprocessed
655}
656
Colin Cross36242852017-06-23 15:06:31 -0700657func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700658 module := &sdkPrebuilt{}
659
Colin Cross74d73e22017-08-02 11:05:49 -0700660 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700661
Colin Cross74d73e22017-08-02 11:05:49 -0700662 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700663 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
664 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700665}
666
Colin Cross2fe66872015-03-30 17:20:39 -0700667func inList(s string, l []string) bool {
668 for _, e := range l {
669 if e == s {
670 return true
671 }
672 }
673 return false
674}
Colin Cross89536d42017-07-07 14:35:50 -0700675
676//
677// Defaults
678//
679type Defaults struct {
680 android.ModuleBase
681 android.DefaultsModuleBase
682}
683
684func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
685}
686
687func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
688}
689
690func defaultsFactory() android.Module {
691 return DefaultsFactory()
692}
693
694func DefaultsFactory(props ...interface{}) android.Module {
695 module := &Defaults{}
696
697 module.AddProperties(props...)
698 module.AddProperties(
699 &CompilerProperties{},
700 &CompilerDeviceProperties{},
701 )
702
703 android.InitDefaultsModule(module)
704
705 return module
706}