blob: ac484c54b7016463cf9dad478e2276c3f4252eee [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 Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070030 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Cross89536d42017-07-07 14:35:50 -070035 android.RegisterModuleType("java_defaults", defaultsFactory)
36
Colin Crossf506d872017-07-19 15:53:04 -070037 android.RegisterModuleType("java_library", LibraryFactory)
38 android.RegisterModuleType("java_library_static", LibraryFactory)
39 android.RegisterModuleType("java_library_host", LibraryHostFactory)
40 android.RegisterModuleType("java_binary", BinaryFactory)
41 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070042 android.RegisterModuleType("java_import", ImportFactory)
43 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Crosse8dc34a2017-07-19 11:22:16 -070044 android.RegisterModuleType("android_prebuilt_sdk", SdkPrebuiltFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070045 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070046
Colin Cross798bfce2016-10-12 14:28:16 -070047 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070048}
49
Colin Cross2fe66872015-03-30 17:20:39 -070050// TODO:
51// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070052// Proto
53// Renderscript
54// Post-jar passes:
55// Proguard
Colin Crossba211132017-06-22 15:36:39 -070056// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070057// Jarjar
58// Dex
59// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070060// DroidDoc
61// Findbugs
62
Colin Cross89536d42017-07-07 14:35:50 -070063type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070064 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
65 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070066 Srcs []string `android:"arch_variant"`
67
68 // list of source files that should not be used to build the Java module.
69 // This is most useful in the arch/multilib variants to remove non-common files
70 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
72 // list of directories containing Java resources
Colin Crosse8dc34a2017-07-19 11:22:16 -070073 Resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070074
Colin Crosse8dc34a2017-07-19 11:22:16 -070075 // list of directories that should be excluded from resource_dirs
76 Exclude_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070077
Paul Duffin2b67e3b2016-11-30 16:13:09 +000078 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070080 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070081
82 // list of module-specific flags that will be used for javac compiles
83 Javacflags []string `android:"arch_variant"`
84
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070086 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070089 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070090
91 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070093
Colin Cross540eff82017-06-22 17:01:52 -070094 // if not blank, run jarjar using the specified rules file
95 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -070096
97 // If not blank, set the java version passed to javac as -source and -target
98 Java_version *string
Colin Cross540eff82017-06-22 17:01:52 -070099}
100
Colin Cross89536d42017-07-07 14:35:50 -0700101type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700102 // list of module-specific flags that will be used for dex compiles
103 Dxflags []string `android:"arch_variant"`
104
Colin Cross7d5136f2015-05-11 13:39:40 -0700105 // if not blank, set to the version of the sdk to compile against
106 Sdk_version string
107
108 // Set for device java libraries, and for host versions of device java libraries
109 // built for testing
110 Dex bool `blueprint:"mutated"`
111
Colin Cross7d5136f2015-05-11 13:39:40 -0700112 // directories to pass to aidl tool
113 Aidl_includes []string
114
115 // directories that should be added as include directories
116 // for any aidl sources of modules that depend on this module
117 Export_aidl_include_dirs []string
118}
119
Colin Cross46c9b8b2017-06-22 16:51:17 -0700120// Module contains the properties and members used by all java module types
121type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700122 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700123 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700124
Colin Cross89536d42017-07-07 14:35:50 -0700125 properties CompilerProperties
126 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700127
128 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700129 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700130
Colin Crossb7a63242015-04-16 14:09:14 -0700131 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700132 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700133
Colin Cross2fe66872015-03-30 17:20:39 -0700134 // jarSpecs suitable for inserting classes from a static library into another jar
135 classJarSpecs []jarSpec
136
137 // jarSpecs suitable for inserting resources from a static library into another jar
138 resourceJarSpecs []jarSpec
139
Colin Cross635c3b02016-05-18 15:37:25 -0700140 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700141
Colin Cross635c3b02016-05-18 15:37:25 -0700142 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700143
Colin Crossb7a63242015-04-16 14:09:14 -0700144 // filelists of extra source files that should be included in the javac command line,
145 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700146 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700147
Colin Cross2fe66872015-03-30 17:20:39 -0700148 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700149 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700150}
151
Colin Crossf506d872017-07-19 15:53:04 -0700152type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700153 ClasspathFiles() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700154 ClassJarSpecs() []jarSpec
155 ResourceJarSpecs() []jarSpec
Colin Cross635c3b02016-05-18 15:37:25 -0700156 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700157}
158
Colin Cross89536d42017-07-07 14:35:50 -0700159func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
160 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
161 android.InitDefaultableModule(module)
162}
163
Colin Crossbe1da472017-07-07 15:59:46 -0700164type dependencyTag struct {
165 blueprint.BaseDependencyTag
166 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700167}
168
Colin Crossbe1da472017-07-07 15:59:46 -0700169var (
Colin Crossf506d872017-07-19 15:53:04 -0700170 staticLibTag = dependencyTag{name: "staticlib"}
171 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700172 bootClasspathTag = dependencyTag{name: "bootclasspath"}
173 frameworkResTag = dependencyTag{name: "framework-res"}
174 sdkDependencyTag = dependencyTag{name: "sdk"}
175)
Colin Cross2fe66872015-03-30 17:20:39 -0700176
Colin Crossbe1da472017-07-07 15:59:46 -0700177func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700178 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700179 if ctx.Device() {
180 switch j.deviceProperties.Sdk_version {
181 case "":
Colin Cross227d4362017-08-30 14:14:52 -0700182 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
183 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700184 case "current":
185 // TODO: !TARGET_BUILD_APPS
186 // TODO: export preprocessed framework.aidl from android_stubs_current
187 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
Colin Cross227d4362017-08-30 14:14:52 -0700188 case "test_current":
189 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_test_stubs_current")
Colin Crossbe1da472017-07-07 15:59:46 -0700190 case "system_current":
191 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
192 default:
193 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
194 }
195 } else {
196 if j.deviceProperties.Dex {
Colin Cross227d4362017-08-30 14:14:52 -0700197 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
Colin Crossbe1da472017-07-07 15:59:46 -0700198 }
Colin Cross2fe66872015-03-30 17:20:39 -0700199 }
200 }
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 Cross227d4362017-08-30 14:14:52 -0700262 bootClasspath = append(bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700263 if sdkDep.AidlPreprocessed().Valid() {
264 if aidlPreprocess.Valid() {
265 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
266 aidlPreprocess, sdkDep.AidlPreprocessed())
267 } else {
268 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700269 }
270 }
Colin Crossbe1da472017-07-07 15:59:46 -0700271 default:
272 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700273 }
Colin Crossbe1da472017-07-07 15:59:46 -0700274
Colin Crossf506d872017-07-19 15:53:04 -0700275 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700276 })
277
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700278 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
279 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700280}
281
Colin Cross46c9b8b2017-06-22 16:51:17 -0700282func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700283
Colin Cross540eff82017-06-22 17:01:52 -0700284 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700285
286 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700287 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700288
Colin Crossf03c82b2015-04-13 13:53:40 -0700289 var flags javaBuilderFlags
290
291 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700292
293 if j.properties.Java_version != nil {
294 flags.javaVersion = *j.properties.Java_version
295 } else {
296 flags.javaVersion = "${config.DefaultJavaVersion}"
297 }
298
Colin Crossf03c82b2015-04-13 13:53:40 -0700299 if len(javacFlags) > 0 {
300 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
301 flags.javacFlags = "$javacFlags"
302 }
303
304 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
305 if len(aidlFlags) > 0 {
306 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
307 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700308 }
309
Colin Crossf506d872017-07-19 15:53:04 -0700310 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700311
Colin Cross74d73e22017-08-02 11:05:49 -0700312 if len(bootClasspath) > 0 {
313 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
314 deps = append(deps, bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700315 } else if ctx.Device() {
316 // Explicitly clear the bootclasspath for device builds
317 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700318 }
319
320 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700321 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700322 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700323 }
324
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700325 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700326
Colin Crossf05fe972015-04-10 17:45:20 -0700327 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700328
Colin Cross0607cf72015-04-28 13:28:51 -0700329 ctx.VisitDirectDeps(func(module blueprint.Module) {
330 if gen, ok := module.(genrule.SourceFileGenerator); ok {
331 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
332 }
333 })
334
Colin Crossb7a63242015-04-16 14:09:14 -0700335 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
336
Colin Crossc6bbef32017-08-14 14:16:06 -0700337 var extraJarDeps android.Paths
338
Colin Cross8cf13342015-04-10 15:41:49 -0700339 if len(srcFiles) > 0 {
340 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700341 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700342 if ctx.Failed() {
343 return
344 }
345
Colin Crossc6bbef32017-08-14 14:16:06 -0700346 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
347 // If error-prone is enabled, add an additional rule to compile the java files into
348 // a separate set of classes (so that they don't overwrite the normal ones and require
349 // a rebuild when error-prone is turned off). Add the classes as a dependency to
350 // the jar command so the two compiles can run in parallel.
351 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
352 // enable error-prone without affecting the output class files.
353 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags, deps)
354 extraJarDeps = append(extraJarDeps, errorprone)
355 }
356
Colin Cross8cf13342015-04-10 15:41:49 -0700357 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700358 }
359
Colin Crosse8dc34a2017-07-19 11:22:16 -0700360 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700361 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700362
Colin Cross635c3b02016-05-18 15:37:25 -0700363 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700364
365 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
366 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
367
368 // Combine classes + resources into classes-full-debug.jar
Colin Crossc6bbef32017-08-14 14:16:06 -0700369 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest, extraJarDeps)
Colin Cross2fe66872015-03-30 17:20:39 -0700370 if ctx.Failed() {
371 return
372 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700373
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700374 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700375 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700376 // Transform classes-full-debug.jar into classes-jarjar.jar
377 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
378 if ctx.Failed() {
379 return
380 }
Colin Cross20978302015-04-10 17:05:07 -0700381
Colin Cross74d73e22017-08-02 11:05:49 -0700382 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700383 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700384 }
385
Colin Cross20978302015-04-10 17:05:07 -0700386 j.resourceJarSpecs = resourceJarSpecs
387 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700388 j.classpathFile = outputFile
389
Colin Cross540eff82017-06-22 17:01:52 -0700390 if j.deviceProperties.Dex && len(srcFiles) > 0 {
391 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700392 if false /* emma enabled */ {
393 // If you instrument class files that have local variable debug information in
394 // them emma does not correctly maintain the local variable table.
395 // This will cause an error when you try to convert the class files for Android.
396 // The workaround here is to build different dex file here based on emma switch
397 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
398 // option to remove local variable information
399 dxFlags = append(dxFlags, "--no-locals")
400 }
401
Colin Cross1332b002015-04-07 17:11:30 -0700402 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700403 dxFlags = append(dxFlags, "--no-optimize")
404 }
405
Colin Cross1332b002015-04-07 17:11:30 -0700406 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700407 dxFlags = append(dxFlags,
408 "--debug",
409 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700410 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700411 "--dump-width=1000")
412 }
413
Colin Cross595a4062017-08-31 12:30:37 -0700414 var minSdkVersion string
415 switch j.deviceProperties.Sdk_version {
416 case "", "current", "test_current", "system_current":
417 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
418 default:
419 minSdkVersion = j.deviceProperties.Sdk_version
420 }
421
422 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
423
Colin Cross2fe66872015-03-30 17:20:39 -0700424 flags.dxFlags = strings.Join(dxFlags, " ")
425
426 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700427 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700428 if ctx.Failed() {
429 return
430 }
431
432 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700433 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700434 }
Colin Crossb7a63242015-04-16 14:09:14 -0700435 ctx.CheckbuildFile(outputFile)
436 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700437}
438
Colin Crossf506d872017-07-19 15:53:04 -0700439var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700440
Colin Cross74d73e22017-08-02 11:05:49 -0700441func (j *Module) ClasspathFiles() android.Paths {
442 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700443}
444
Colin Cross46c9b8b2017-06-22 16:51:17 -0700445func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700446 return j.classJarSpecs
447}
448
Colin Cross46c9b8b2017-06-22 16:51:17 -0700449func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700450 return j.resourceJarSpecs
451}
452
Colin Cross46c9b8b2017-06-22 16:51:17 -0700453func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700454 return j.exportAidlIncludeDirs
455}
456
Colin Cross46c9b8b2017-06-22 16:51:17 -0700457var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700458
Colin Cross46c9b8b2017-06-22 16:51:17 -0700459func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700460 return j.logtagsSrcs
461}
462
Colin Cross2fe66872015-03-30 17:20:39 -0700463//
464// Java libraries (.jar file)
465//
466
Colin Crossf506d872017-07-19 15:53:04 -0700467type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700468 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700469}
470
Colin Crossf506d872017-07-19 15:53:04 -0700471func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700472 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700473
Colin Cross5c517922017-08-31 12:29:17 -0700474 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700475}
476
Colin Crossf506d872017-07-19 15:53:04 -0700477func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700478 j.deps(ctx)
479}
480
Colin Crossf506d872017-07-19 15:53:04 -0700481func LibraryFactory() android.Module {
482 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700483
Colin Cross540eff82017-06-22 17:01:52 -0700484 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700485
Colin Cross36242852017-06-23 15:06:31 -0700486 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700487 &module.Module.properties,
488 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700489
Colin Cross89536d42017-07-07 14:35:50 -0700490 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700491 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700492}
493
Colin Crossf506d872017-07-19 15:53:04 -0700494func LibraryHostFactory() android.Module {
495 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700496
Colin Cross36242852017-06-23 15:06:31 -0700497 module.AddProperties(&module.Module.properties)
498
Colin Cross89536d42017-07-07 14:35:50 -0700499 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700500 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700501}
502
503//
504// Java Binaries (.jar file plus wrapper script)
505//
506
Colin Crossf506d872017-07-19 15:53:04 -0700507type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700508 // installable script to execute the resulting jar
509 Wrapper string
510}
511
Colin Crossf506d872017-07-19 15:53:04 -0700512type Binary struct {
513 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700514
Colin Crossf506d872017-07-19 15:53:04 -0700515 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700516
517 wrapperFile android.ModuleSrcPath
518 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700519}
520
Colin Crossf506d872017-07-19 15:53:04 -0700521func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
522 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700523
524 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
525 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700526 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700527 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
528 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700529}
530
Colin Crossf506d872017-07-19 15:53:04 -0700531func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700532 j.deps(ctx)
533}
534
Colin Crossf506d872017-07-19 15:53:04 -0700535func BinaryFactory() android.Module {
536 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700537
Colin Cross540eff82017-06-22 17:01:52 -0700538 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700539
Colin Cross36242852017-06-23 15:06:31 -0700540 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700541 &module.Module.properties,
542 &module.Module.deviceProperties,
543 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700544
Colin Cross89536d42017-07-07 14:35:50 -0700545 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700546 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700547}
548
Colin Crossf506d872017-07-19 15:53:04 -0700549func BinaryHostFactory() android.Module {
550 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700551
Colin Cross36242852017-06-23 15:06:31 -0700552 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700553 &module.Module.properties,
554 &module.Module.deviceProperties,
555 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700556
Colin Cross89536d42017-07-07 14:35:50 -0700557 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700558 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700559}
560
561//
562// Java prebuilts
563//
564
Colin Cross74d73e22017-08-02 11:05:49 -0700565type ImportProperties struct {
566 Jars []string
567}
568
569type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700570 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700571 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700572
Colin Cross74d73e22017-08-02 11:05:49 -0700573 properties ImportProperties
574
575 classpathFiles android.Paths
576 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700577 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700578}
579
Colin Cross74d73e22017-08-02 11:05:49 -0700580func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700581 return &j.prebuilt
582}
583
Colin Cross74d73e22017-08-02 11:05:49 -0700584func (j *Import) PrebuiltSrcs() []string {
585 return j.properties.Jars
586}
587
588func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700589 return j.prebuilt.Name(j.ModuleBase.Name())
590}
591
Colin Cross74d73e22017-08-02 11:05:49 -0700592func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700593}
594
Colin Cross74d73e22017-08-02 11:05:49 -0700595func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
596 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700597
Colin Cross74d73e22017-08-02 11:05:49 -0700598 for i, prebuilt := range j.classpathFiles {
599 subdir := "extracted" + strconv.Itoa(i)
600 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
601 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
602 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
603 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700604
Colin Crossc6bbef32017-08-14 14:16:06 -0700605 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
Colin Cross74d73e22017-08-02 11:05:49 -0700606
Colin Cross5c517922017-08-31 12:29:17 -0700607 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Cross74d73e22017-08-02 11:05:49 -0700608 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700609}
610
Colin Cross74d73e22017-08-02 11:05:49 -0700611var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700612
Colin Cross74d73e22017-08-02 11:05:49 -0700613func (j *Import) ClasspathFiles() android.Paths {
614 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700615}
616
Colin Cross74d73e22017-08-02 11:05:49 -0700617func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700618 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700619}
620
Colin Cross74d73e22017-08-02 11:05:49 -0700621func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700622 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700623}
624
Colin Cross74d73e22017-08-02 11:05:49 -0700625func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700626 return nil
627}
628
Colin Cross74d73e22017-08-02 11:05:49 -0700629var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700630
Colin Cross74d73e22017-08-02 11:05:49 -0700631func ImportFactory() android.Module {
632 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700633
Colin Cross74d73e22017-08-02 11:05:49 -0700634 module.AddProperties(&module.properties)
635
636 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700637 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
638 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700639}
640
Colin Cross74d73e22017-08-02 11:05:49 -0700641func ImportFactoryHost() android.Module {
642 module := &Import{}
643
644 module.AddProperties(&module.properties)
645
646 android.InitPrebuiltModule(module, &module.properties.Jars)
647 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
648 return module
649}
650
Colin Crossaa8630b2015-04-13 13:52:22 -0700651//
652// SDK java prebuilts (.jar containing resources plus framework.aidl)
653//
654
655type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700656 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700657 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700658}
659
660var _ sdkDependency = (*sdkPrebuilt)(nil)
661
Colin Cross7d5136f2015-05-11 13:39:40 -0700662type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700663 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700664}
665
Colin Crossaa8630b2015-04-13 13:52:22 -0700666type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700667 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700668
Colin Cross7d5136f2015-05-11 13:39:40 -0700669 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700670
Colin Cross635c3b02016-05-18 15:37:25 -0700671 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700672}
673
Colin Cross635c3b02016-05-18 15:37:25 -0700674func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700675 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700676
Colin Cross635c3b02016-05-18 15:37:25 -0700677 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700678}
679
Colin Cross635c3b02016-05-18 15:37:25 -0700680func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700681 return j.aidlPreprocessed
682}
683
Colin Cross36242852017-06-23 15:06:31 -0700684func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700685 module := &sdkPrebuilt{}
686
Colin Cross74d73e22017-08-02 11:05:49 -0700687 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700688
Colin Cross74d73e22017-08-02 11:05:49 -0700689 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700690 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
691 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700692}
693
Colin Cross2fe66872015-03-30 17:20:39 -0700694func inList(s string, l []string) bool {
695 for _, e := range l {
696 if e == s {
697 return true
698 }
699 }
700 return false
701}
Colin Cross89536d42017-07-07 14:35:50 -0700702
703//
704// Defaults
705//
706type Defaults struct {
707 android.ModuleBase
708 android.DefaultsModuleBase
709}
710
711func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
712}
713
714func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
715}
716
717func defaultsFactory() android.Module {
718 return DefaultsFactory()
719}
720
721func DefaultsFactory(props ...interface{}) android.Module {
722 module := &Defaults{}
723
724 module.AddProperties(props...)
725 module.AddProperties(
726 &CompilerProperties{},
727 &CompilerDeviceProperties{},
728 )
729
730 android.InitDefaultsModule(module)
731
732 return module
733}