blob: 37d959bc61b2e6eff79fed64136c52006dd0a5ef [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
95}
96
Colin Cross89536d42017-07-07 14:35:50 -070097type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -070098 // list of module-specific flags that will be used for dex compiles
99 Dxflags []string `android:"arch_variant"`
100
Colin Cross7d5136f2015-05-11 13:39:40 -0700101 // if not blank, set to the version of the sdk to compile against
102 Sdk_version string
103
104 // Set for device java libraries, and for host versions of device java libraries
105 // built for testing
106 Dex bool `blueprint:"mutated"`
107
Colin Cross7d5136f2015-05-11 13:39:40 -0700108 // directories to pass to aidl tool
109 Aidl_includes []string
110
111 // directories that should be added as include directories
112 // for any aidl sources of modules that depend on this module
113 Export_aidl_include_dirs []string
114}
115
Colin Cross46c9b8b2017-06-22 16:51:17 -0700116// Module contains the properties and members used by all java module types
117type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700118 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700119 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700120
Colin Cross89536d42017-07-07 14:35:50 -0700121 properties CompilerProperties
122 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700123
124 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700125 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700126
Colin Crossb7a63242015-04-16 14:09:14 -0700127 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700128 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700129
Colin Cross2fe66872015-03-30 17:20:39 -0700130 // jarSpecs suitable for inserting classes from a static library into another jar
131 classJarSpecs []jarSpec
132
133 // jarSpecs suitable for inserting resources from a static library into another jar
134 resourceJarSpecs []jarSpec
135
Colin Cross635c3b02016-05-18 15:37:25 -0700136 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700137
Colin Cross635c3b02016-05-18 15:37:25 -0700138 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700139
Colin Crossb7a63242015-04-16 14:09:14 -0700140 // filelists of extra source files that should be included in the javac command line,
141 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700142 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700143
Colin Cross2fe66872015-03-30 17:20:39 -0700144 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700145 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700146}
147
Colin Crossf506d872017-07-19 15:53:04 -0700148type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700149 ClasspathFiles() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700150 ClassJarSpecs() []jarSpec
151 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700152 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700153}
154
Colin Cross89536d42017-07-07 14:35:50 -0700155func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
156 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
157 android.InitDefaultableModule(module)
158}
159
Colin Crossbe1da472017-07-07 15:59:46 -0700160type dependencyTag struct {
161 blueprint.BaseDependencyTag
162 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700163}
164
Colin Crossbe1da472017-07-07 15:59:46 -0700165var (
Colin Crossf506d872017-07-19 15:53:04 -0700166 staticLibTag = dependencyTag{name: "staticlib"}
167 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700168 bootClasspathTag = dependencyTag{name: "bootclasspath"}
169 frameworkResTag = dependencyTag{name: "framework-res"}
170 sdkDependencyTag = dependencyTag{name: "sdk"}
171)
Colin Cross2fe66872015-03-30 17:20:39 -0700172
Colin Crossbe1da472017-07-07 15:59:46 -0700173func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross2fe66872015-03-30 17:20:39 -0700174 if !j.properties.No_standard_libraries {
Colin Crossbe1da472017-07-07 15:59:46 -0700175 if ctx.Device() {
176 switch j.deviceProperties.Sdk_version {
177 case "":
178 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
179 case "current":
180 // TODO: !TARGET_BUILD_APPS
181 // TODO: export preprocessed framework.aidl from android_stubs_current
182 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
183 case "system_current":
184 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
185 default:
186 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
187 }
188 } else {
189 if j.deviceProperties.Dex {
190 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
191 }
Colin Cross2fe66872015-03-30 17:20:39 -0700192 }
Colin Crossbe1da472017-07-07 15:59:46 -0700193
Colin Cross540eff82017-06-22 17:01:52 -0700194 if ctx.Device() && j.deviceProperties.Sdk_version == "" {
Colin Crossf506d872017-07-19 15:53:04 -0700195 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossefb9ebe2015-04-16 14:08:06 -0700196 }
Colin Cross2fe66872015-03-30 17:20:39 -0700197 }
Colin Crossf506d872017-07-19 15:53:04 -0700198 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
199 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700200}
201
Colin Cross46c9b8b2017-06-22 16:51:17 -0700202func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700203 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700204
Colin Cross540eff82017-06-22 17:01:52 -0700205 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700206
207 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208 if aidlPreprocess.Valid() {
209 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700210 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700211 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700212 }
213
Colin Cross635c3b02016-05-18 15:37:25 -0700214 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
215 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
216 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700217 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
218 flags = append(flags, "-I"+src.String())
219 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700220
Colin Crossf03c82b2015-04-13 13:53:40 -0700221 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700222}
223
Colin Cross46c9b8b2017-06-22 16:51:17 -0700224func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross74d73e22017-08-02 11:05:49 -0700225 bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700226 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700227
228 ctx.VisitDirectDeps(func(module blueprint.Module) {
229 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700230 tag := ctx.OtherModuleDependencyTag(module)
231
Colin Crossf506d872017-07-19 15:53:04 -0700232 dep, _ := module.(Dependency)
233 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700234 switch tag {
235 case android.DefaultsDepTag, android.SourceDepTag:
236 default:
237 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700238 }
Colin Crossec7a0422017-07-07 14:47:12 -0700239 return
240 }
241
Colin Crossbe1da472017-07-07 15:59:46 -0700242 switch tag {
243 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700244 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700245 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700246 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700247 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700248 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700249 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
250 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700251 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700252 if ctx.ModuleName() == "framework" {
253 // framework.jar has a one-off dependency on the R.java and Manifest.java files
254 // generated by framework-res.apk
255 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
256 }
Colin Crossbe1da472017-07-07 15:59:46 -0700257 case sdkDependencyTag:
258 sdkDep := module.(sdkDependency)
Colin Crossec7a0422017-07-07 14:47:12 -0700259 if sdkDep.AidlPreprocessed().Valid() {
260 if aidlPreprocess.Valid() {
261 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
262 aidlPreprocess, sdkDep.AidlPreprocessed())
263 } else {
264 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700265 }
266 }
Colin Crossbe1da472017-07-07 15:59:46 -0700267 default:
268 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700269 }
Colin Crossbe1da472017-07-07 15:59:46 -0700270
Colin Crossf506d872017-07-19 15:53:04 -0700271 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700272 })
273
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700274 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
275 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700276}
277
Colin Cross46c9b8b2017-06-22 16:51:17 -0700278func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700279
Colin Cross540eff82017-06-22 17:01:52 -0700280 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700281
282 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700283 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700284
Colin Crossf03c82b2015-04-13 13:53:40 -0700285 var flags javaBuilderFlags
286
287 javacFlags := j.properties.Javacflags
288 if len(javacFlags) > 0 {
289 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
290 flags.javacFlags = "$javacFlags"
291 }
292
293 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
294 if len(aidlFlags) > 0 {
295 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
296 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700297 }
298
Colin Crossf506d872017-07-19 15:53:04 -0700299 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700300
Colin Cross74d73e22017-08-02 11:05:49 -0700301 if len(bootClasspath) > 0 {
302 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
303 deps = append(deps, bootClasspath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700304 }
305
306 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700307 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700308 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700309 }
310
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700311 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700312
Colin Crossf05fe972015-04-10 17:45:20 -0700313 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700314
Colin Cross0607cf72015-04-28 13:28:51 -0700315 ctx.VisitDirectDeps(func(module blueprint.Module) {
316 if gen, ok := module.(genrule.SourceFileGenerator); ok {
317 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
318 }
319 })
320
Colin Crossb7a63242015-04-16 14:09:14 -0700321 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
322
Colin Cross8cf13342015-04-10 15:41:49 -0700323 if len(srcFiles) > 0 {
324 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700325 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700326 if ctx.Failed() {
327 return
328 }
329
330 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700331 }
332
Colin Crosse8dc34a2017-07-19 11:22:16 -0700333 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700334 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700335
Colin Cross635c3b02016-05-18 15:37:25 -0700336 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700337
338 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
339 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
340
341 // Combine classes + resources into classes-full-debug.jar
342 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
343 if ctx.Failed() {
344 return
345 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700346
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700347 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700348 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700349 // Transform classes-full-debug.jar into classes-jarjar.jar
350 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
351 if ctx.Failed() {
352 return
353 }
Colin Cross20978302015-04-10 17:05:07 -0700354
Colin Cross74d73e22017-08-02 11:05:49 -0700355 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700356 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700357 }
358
Colin Cross20978302015-04-10 17:05:07 -0700359 j.resourceJarSpecs = resourceJarSpecs
360 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700361 j.classpathFile = outputFile
362
Colin Cross540eff82017-06-22 17:01:52 -0700363 if j.deviceProperties.Dex && len(srcFiles) > 0 {
364 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700365 if false /* emma enabled */ {
366 // If you instrument class files that have local variable debug information in
367 // them emma does not correctly maintain the local variable table.
368 // This will cause an error when you try to convert the class files for Android.
369 // The workaround here is to build different dex file here based on emma switch
370 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
371 // option to remove local variable information
372 dxFlags = append(dxFlags, "--no-locals")
373 }
374
Colin Cross1332b002015-04-07 17:11:30 -0700375 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700376 dxFlags = append(dxFlags, "--no-optimize")
377 }
378
Colin Cross1332b002015-04-07 17:11:30 -0700379 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700380 dxFlags = append(dxFlags,
381 "--debug",
382 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700383 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700384 "--dump-width=1000")
385 }
386
387 flags.dxFlags = strings.Join(dxFlags, " ")
388
389 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700390 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700391 if ctx.Failed() {
392 return
393 }
394
395 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700396 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700397 }
Colin Crossb7a63242015-04-16 14:09:14 -0700398 ctx.CheckbuildFile(outputFile)
399 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700400}
401
Colin Crossf506d872017-07-19 15:53:04 -0700402var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700403
Colin Cross74d73e22017-08-02 11:05:49 -0700404func (j *Module) ClasspathFiles() android.Paths {
405 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700406}
407
Colin Cross46c9b8b2017-06-22 16:51:17 -0700408func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700409 return j.classJarSpecs
410}
411
Colin Cross46c9b8b2017-06-22 16:51:17 -0700412func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700413 return j.resourceJarSpecs
414}
415
Colin Cross46c9b8b2017-06-22 16:51:17 -0700416func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700417 return j.exportAidlIncludeDirs
418}
419
Colin Cross46c9b8b2017-06-22 16:51:17 -0700420var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700421
Colin Cross46c9b8b2017-06-22 16:51:17 -0700422func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700423 return j.logtagsSrcs
424}
425
Colin Cross2fe66872015-03-30 17:20:39 -0700426//
427// Java libraries (.jar file)
428//
429
Colin Crossf506d872017-07-19 15:53:04 -0700430type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700431 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700432}
433
Colin Crossf506d872017-07-19 15:53:04 -0700434func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700435 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700436
Colin Cross635c3b02016-05-18 15:37:25 -0700437 j.installFile = ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700438}
439
Colin Crossf506d872017-07-19 15:53:04 -0700440func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700441 j.deps(ctx)
442}
443
Colin Crossf506d872017-07-19 15:53:04 -0700444func LibraryFactory() android.Module {
445 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700446
Colin Cross540eff82017-06-22 17:01:52 -0700447 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700448
Colin Cross36242852017-06-23 15:06:31 -0700449 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700450 &module.Module.properties,
451 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700452
Colin Cross89536d42017-07-07 14:35:50 -0700453 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700454 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700455}
456
Colin Crossf506d872017-07-19 15:53:04 -0700457func LibraryHostFactory() android.Module {
458 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700459
Colin Cross36242852017-06-23 15:06:31 -0700460 module.AddProperties(&module.Module.properties)
461
Colin Cross89536d42017-07-07 14:35:50 -0700462 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700463 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700464}
465
466//
467// Java Binaries (.jar file plus wrapper script)
468//
469
Colin Crossf506d872017-07-19 15:53:04 -0700470type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700471 // installable script to execute the resulting jar
472 Wrapper string
473}
474
Colin Crossf506d872017-07-19 15:53:04 -0700475type Binary struct {
476 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700477
Colin Crossf506d872017-07-19 15:53:04 -0700478 binaryProperties binaryProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700479}
480
Colin Crossf506d872017-07-19 15:53:04 -0700481func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
482 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700483
484 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
485 // another build rule before the jar has been installed.
Colin Cross635c3b02016-05-18 15:37:25 -0700486 ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper),
Colin Cross2fe66872015-03-30 17:20:39 -0700487 j.installFile)
488}
489
Colin Crossf506d872017-07-19 15:53:04 -0700490func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700491 j.deps(ctx)
492}
493
Colin Crossf506d872017-07-19 15:53:04 -0700494func BinaryFactory() android.Module {
495 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700496
Colin Cross540eff82017-06-22 17:01:52 -0700497 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700498
Colin Cross36242852017-06-23 15:06:31 -0700499 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700500 &module.Module.properties,
501 &module.Module.deviceProperties,
502 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700503
Colin Cross89536d42017-07-07 14:35:50 -0700504 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700505 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700506}
507
Colin Crossf506d872017-07-19 15:53:04 -0700508func BinaryHostFactory() android.Module {
509 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700510
Colin Cross36242852017-06-23 15:06:31 -0700511 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700512 &module.Module.properties,
513 &module.Module.deviceProperties,
514 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700515
Colin Cross89536d42017-07-07 14:35:50 -0700516 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700517 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700518}
519
520//
521// Java prebuilts
522//
523
Colin Cross74d73e22017-08-02 11:05:49 -0700524type ImportProperties struct {
525 Jars []string
526}
527
528type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700529 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700530 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700531
Colin Cross74d73e22017-08-02 11:05:49 -0700532 properties ImportProperties
533
534 classpathFiles android.Paths
535 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700536 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700537}
538
Colin Cross74d73e22017-08-02 11:05:49 -0700539func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700540 return &j.prebuilt
541}
542
Colin Cross74d73e22017-08-02 11:05:49 -0700543func (j *Import) PrebuiltSrcs() []string {
544 return j.properties.Jars
545}
546
547func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700548 return j.prebuilt.Name(j.ModuleBase.Name())
549}
550
Colin Cross74d73e22017-08-02 11:05:49 -0700551func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700552}
553
Colin Cross74d73e22017-08-02 11:05:49 -0700554func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
555 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700556
Colin Cross74d73e22017-08-02 11:05:49 -0700557 for i, prebuilt := range j.classpathFiles {
558 subdir := "extracted" + strconv.Itoa(i)
559 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
560 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
561 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
562 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700563
Colin Cross74d73e22017-08-02 11:05:49 -0700564 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{})
565
566 ctx.InstallFileName(android.PathForModuleInstall(ctx, "framework"),
567 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700568}
569
Colin Cross74d73e22017-08-02 11:05:49 -0700570var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700571
Colin Cross74d73e22017-08-02 11:05:49 -0700572func (j *Import) ClasspathFiles() android.Paths {
573 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700574}
575
Colin Cross74d73e22017-08-02 11:05:49 -0700576func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700577 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700578}
579
Colin Cross74d73e22017-08-02 11:05:49 -0700580func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700581 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700582}
583
Colin Cross74d73e22017-08-02 11:05:49 -0700584func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700585 return nil
586}
587
Colin Cross74d73e22017-08-02 11:05:49 -0700588var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700589
Colin Cross74d73e22017-08-02 11:05:49 -0700590func ImportFactory() android.Module {
591 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700592
Colin Cross74d73e22017-08-02 11:05:49 -0700593 module.AddProperties(&module.properties)
594
595 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700596 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
597 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700598}
599
Colin Cross74d73e22017-08-02 11:05:49 -0700600func ImportFactoryHost() android.Module {
601 module := &Import{}
602
603 module.AddProperties(&module.properties)
604
605 android.InitPrebuiltModule(module, &module.properties.Jars)
606 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
607 return module
608}
609
Colin Crossaa8630b2015-04-13 13:52:22 -0700610//
611// SDK java prebuilts (.jar containing resources plus framework.aidl)
612//
613
614type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700615 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700616 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700617}
618
619var _ sdkDependency = (*sdkPrebuilt)(nil)
620
Colin Cross7d5136f2015-05-11 13:39:40 -0700621type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700622 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700623}
624
Colin Crossaa8630b2015-04-13 13:52:22 -0700625type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700626 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700627
Colin Cross7d5136f2015-05-11 13:39:40 -0700628 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700629
Colin Cross635c3b02016-05-18 15:37:25 -0700630 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700631}
632
Colin Cross635c3b02016-05-18 15:37:25 -0700633func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700634 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700635
Colin Cross635c3b02016-05-18 15:37:25 -0700636 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700637}
638
Colin Cross635c3b02016-05-18 15:37:25 -0700639func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700640 return j.aidlPreprocessed
641}
642
Colin Cross36242852017-06-23 15:06:31 -0700643func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700644 module := &sdkPrebuilt{}
645
Colin Cross74d73e22017-08-02 11:05:49 -0700646 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700647
Colin Cross74d73e22017-08-02 11:05:49 -0700648 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700649 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
650 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700651}
652
Colin Cross2fe66872015-03-30 17:20:39 -0700653func inList(s string, l []string) bool {
654 for _, e := range l {
655 if e == s {
656 return true
657 }
658 }
659 return false
660}
Colin Cross89536d42017-07-07 14:35:50 -0700661
662//
663// Defaults
664//
665type Defaults struct {
666 android.ModuleBase
667 android.DefaultsModuleBase
668}
669
670func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
671}
672
673func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
674}
675
676func defaultsFactory() android.Module {
677 return DefaultsFactory()
678}
679
680func DefaultsFactory(props ...interface{}) android.Module {
681 module := &Defaults{}
682
683 module.AddProperties(props...)
684 module.AddProperties(
685 &CompilerProperties{},
686 &CompilerDeviceProperties{},
687 )
688
689 android.InitDefaultsModule(module)
690
691 return module
692}