blob: a8093e7b211389266250011587a5c1467b49e64f [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 Cross7f9036c2017-08-30 13:27:57 -0700203
204 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross2fe66872015-03-30 17:20:39 -0700205}
206
Colin Cross46c9b8b2017-06-22 16:51:17 -0700207func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700208 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700209
Colin Cross540eff82017-06-22 17:01:52 -0700210 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700211
212 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700213 if aidlPreprocess.Valid() {
214 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700215 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700216 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700217 }
218
Colin Cross635c3b02016-05-18 15:37:25 -0700219 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
220 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
221 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700222 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
223 flags = append(flags, "-I"+src.String())
224 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700225
Colin Crossf03c82b2015-04-13 13:53:40 -0700226 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700227}
228
Colin Cross46c9b8b2017-06-22 16:51:17 -0700229func (j *Module) collectDeps(ctx android.ModuleContext) (classpath android.Paths,
Colin Cross74d73e22017-08-02 11:05:49 -0700230 bootClasspath android.Paths, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700231 aidlIncludeDirs android.Paths, srcFileLists android.Paths) {
Colin Cross2fe66872015-03-30 17:20:39 -0700232
233 ctx.VisitDirectDeps(func(module blueprint.Module) {
234 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700235 tag := ctx.OtherModuleDependencyTag(module)
236
Colin Crossf506d872017-07-19 15:53:04 -0700237 dep, _ := module.(Dependency)
238 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700239 switch tag {
240 case android.DefaultsDepTag, android.SourceDepTag:
241 default:
242 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700243 }
Colin Crossec7a0422017-07-07 14:47:12 -0700244 return
245 }
246
Colin Crossbe1da472017-07-07 15:59:46 -0700247 switch tag {
248 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700249 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700250 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700251 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700252 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700253 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700254 classJarSpecs = append(classJarSpecs, dep.ClassJarSpecs()...)
255 resourceJarSpecs = append(resourceJarSpecs, dep.ResourceJarSpecs()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700256 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700257 if ctx.ModuleName() == "framework" {
258 // framework.jar has a one-off dependency on the R.java and Manifest.java files
259 // generated by framework-res.apk
260 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
261 }
Colin Crossbe1da472017-07-07 15:59:46 -0700262 case sdkDependencyTag:
263 sdkDep := module.(sdkDependency)
Colin Cross227d4362017-08-30 14:14:52 -0700264 bootClasspath = append(bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700265 if sdkDep.AidlPreprocessed().Valid() {
266 if aidlPreprocess.Valid() {
267 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
268 aidlPreprocess, sdkDep.AidlPreprocessed())
269 } else {
270 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700271 }
272 }
Colin Crossbe1da472017-07-07 15:59:46 -0700273 default:
274 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700275 }
Colin Crossbe1da472017-07-07 15:59:46 -0700276
Colin Crossf506d872017-07-19 15:53:04 -0700277 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700278 })
279
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700280 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
281 aidlIncludeDirs, srcFileLists
Colin Cross2fe66872015-03-30 17:20:39 -0700282}
283
Colin Cross46c9b8b2017-06-22 16:51:17 -0700284func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700285
Colin Cross540eff82017-06-22 17:01:52 -0700286 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700287
288 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
Colin Crosse7a9f3f2015-04-13 14:02:52 -0700289 aidlIncludeDirs, srcFileLists := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700290
Colin Crossf03c82b2015-04-13 13:53:40 -0700291 var flags javaBuilderFlags
292
293 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700294
295 if j.properties.Java_version != nil {
296 flags.javaVersion = *j.properties.Java_version
297 } else {
298 flags.javaVersion = "${config.DefaultJavaVersion}"
299 }
300
Colin Crossf03c82b2015-04-13 13:53:40 -0700301 if len(javacFlags) > 0 {
302 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
303 flags.javacFlags = "$javacFlags"
304 }
305
306 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
307 if len(aidlFlags) > 0 {
308 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
309 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700310 }
311
Colin Crossf506d872017-07-19 15:53:04 -0700312 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700313
Colin Cross74d73e22017-08-02 11:05:49 -0700314 if len(bootClasspath) > 0 {
315 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
316 deps = append(deps, bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700317 } else if ctx.Device() {
318 // Explicitly clear the bootclasspath for device builds
319 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700320 }
321
322 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700323 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700324 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700325 }
326
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700327 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700328
Colin Crossf05fe972015-04-10 17:45:20 -0700329 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700330
Colin Cross0607cf72015-04-28 13:28:51 -0700331 ctx.VisitDirectDeps(func(module blueprint.Module) {
332 if gen, ok := module.(genrule.SourceFileGenerator); ok {
333 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
334 }
335 })
336
Colin Crossb7a63242015-04-16 14:09:14 -0700337 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
338
Colin Crossc6bbef32017-08-14 14:16:06 -0700339 var extraJarDeps android.Paths
340
Colin Cross8cf13342015-04-10 15:41:49 -0700341 if len(srcFiles) > 0 {
342 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700343 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700344 if ctx.Failed() {
345 return
346 }
347
Colin Crossc6bbef32017-08-14 14:16:06 -0700348 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
349 // If error-prone is enabled, add an additional rule to compile the java files into
350 // a separate set of classes (so that they don't overwrite the normal ones and require
351 // a rebuild when error-prone is turned off). Add the classes as a dependency to
352 // the jar command so the two compiles can run in parallel.
353 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
354 // enable error-prone without affecting the output class files.
355 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags, deps)
356 extraJarDeps = append(extraJarDeps, errorprone)
357 }
358
Colin Cross8cf13342015-04-10 15:41:49 -0700359 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700360 }
361
Colin Crosse8dc34a2017-07-19 11:22:16 -0700362 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs),
Colin Cross276284f2015-04-20 13:51:48 -0700363 resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700364
Colin Cross635c3b02016-05-18 15:37:25 -0700365 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700366
367 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
368 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
369
370 // Combine classes + resources into classes-full-debug.jar
Colin Crossc6bbef32017-08-14 14:16:06 -0700371 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest, extraJarDeps)
Colin Cross2fe66872015-03-30 17:20:39 -0700372 if ctx.Failed() {
373 return
374 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700375
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700376 if j.properties.Jarjar_rules != nil {
Colin Cross635c3b02016-05-18 15:37:25 -0700377 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross65bf4f22015-04-03 16:54:17 -0700378 // Transform classes-full-debug.jar into classes-jarjar.jar
379 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
380 if ctx.Failed() {
381 return
382 }
Colin Cross20978302015-04-10 17:05:07 -0700383
Colin Cross74d73e22017-08-02 11:05:49 -0700384 classes, _ := TransformPrebuiltJarToClasses(ctx, "jarjar_extracted", outputFile)
Colin Cross20978302015-04-10 17:05:07 -0700385 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700386 }
387
Colin Cross20978302015-04-10 17:05:07 -0700388 j.resourceJarSpecs = resourceJarSpecs
389 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700390 j.classpathFile = outputFile
391
Colin Cross540eff82017-06-22 17:01:52 -0700392 if j.deviceProperties.Dex && len(srcFiles) > 0 {
393 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700394 if false /* emma enabled */ {
395 // If you instrument class files that have local variable debug information in
396 // them emma does not correctly maintain the local variable table.
397 // This will cause an error when you try to convert the class files for Android.
398 // The workaround here is to build different dex file here based on emma switch
399 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
400 // option to remove local variable information
401 dxFlags = append(dxFlags, "--no-locals")
402 }
403
Colin Cross1332b002015-04-07 17:11:30 -0700404 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700405 dxFlags = append(dxFlags, "--no-optimize")
406 }
407
Colin Cross1332b002015-04-07 17:11:30 -0700408 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700409 dxFlags = append(dxFlags,
410 "--debug",
411 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700412 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700413 "--dump-width=1000")
414 }
415
Colin Cross595a4062017-08-31 12:30:37 -0700416 var minSdkVersion string
417 switch j.deviceProperties.Sdk_version {
418 case "", "current", "test_current", "system_current":
419 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
420 default:
421 minSdkVersion = j.deviceProperties.Sdk_version
422 }
423
424 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
425
Colin Cross2fe66872015-03-30 17:20:39 -0700426 flags.dxFlags = strings.Join(dxFlags, " ")
427
428 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700429 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700430 if ctx.Failed() {
431 return
432 }
433
434 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700435 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700436 }
Colin Crossb7a63242015-04-16 14:09:14 -0700437 ctx.CheckbuildFile(outputFile)
438 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700439}
440
Colin Crossf506d872017-07-19 15:53:04 -0700441var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700442
Colin Cross74d73e22017-08-02 11:05:49 -0700443func (j *Module) ClasspathFiles() android.Paths {
444 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700445}
446
Colin Cross46c9b8b2017-06-22 16:51:17 -0700447func (j *Module) ClassJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700448 return j.classJarSpecs
449}
450
Colin Cross46c9b8b2017-06-22 16:51:17 -0700451func (j *Module) ResourceJarSpecs() []jarSpec {
Colin Cross2fe66872015-03-30 17:20:39 -0700452 return j.resourceJarSpecs
453}
454
Colin Cross46c9b8b2017-06-22 16:51:17 -0700455func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700456 return j.exportAidlIncludeDirs
457}
458
Colin Cross46c9b8b2017-06-22 16:51:17 -0700459var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700460
Colin Cross46c9b8b2017-06-22 16:51:17 -0700461func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700462 return j.logtagsSrcs
463}
464
Colin Cross2fe66872015-03-30 17:20:39 -0700465//
466// Java libraries (.jar file)
467//
468
Colin Crossf506d872017-07-19 15:53:04 -0700469type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700470 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700471}
472
Colin Crossf506d872017-07-19 15:53:04 -0700473func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700474 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700475
Colin Cross5c517922017-08-31 12:29:17 -0700476 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile)
Colin Crossb7a63242015-04-16 14:09:14 -0700477}
478
Colin Crossf506d872017-07-19 15:53:04 -0700479func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700480 j.deps(ctx)
481}
482
Colin Crossf506d872017-07-19 15:53:04 -0700483func LibraryFactory() android.Module {
484 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700485
Colin Cross540eff82017-06-22 17:01:52 -0700486 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700487
Colin Cross36242852017-06-23 15:06:31 -0700488 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700489 &module.Module.properties,
490 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700491
Colin Cross89536d42017-07-07 14:35:50 -0700492 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700493 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700494}
495
Colin Crossf506d872017-07-19 15:53:04 -0700496func LibraryHostFactory() android.Module {
497 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700498
Colin Cross36242852017-06-23 15:06:31 -0700499 module.AddProperties(&module.Module.properties)
500
Colin Cross89536d42017-07-07 14:35:50 -0700501 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700502 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700503}
504
505//
506// Java Binaries (.jar file plus wrapper script)
507//
508
Colin Crossf506d872017-07-19 15:53:04 -0700509type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700510 // installable script to execute the resulting jar
511 Wrapper string
512}
513
Colin Crossf506d872017-07-19 15:53:04 -0700514type Binary struct {
515 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700516
Colin Crossf506d872017-07-19 15:53:04 -0700517 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700518
519 wrapperFile android.ModuleSrcPath
520 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700521}
522
Colin Crossf506d872017-07-19 15:53:04 -0700523func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
524 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700525
526 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
527 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700528 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700529 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
530 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700531}
532
Colin Crossf506d872017-07-19 15:53:04 -0700533func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700534 j.deps(ctx)
535}
536
Colin Crossf506d872017-07-19 15:53:04 -0700537func BinaryFactory() android.Module {
538 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700539
Colin Cross540eff82017-06-22 17:01:52 -0700540 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700541
Colin Cross36242852017-06-23 15:06:31 -0700542 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700543 &module.Module.properties,
544 &module.Module.deviceProperties,
545 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700546
Colin Cross89536d42017-07-07 14:35:50 -0700547 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700548 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700549}
550
Colin Crossf506d872017-07-19 15:53:04 -0700551func BinaryHostFactory() android.Module {
552 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700553
Colin Cross36242852017-06-23 15:06:31 -0700554 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700555 &module.Module.properties,
556 &module.Module.deviceProperties,
557 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700558
Colin Cross89536d42017-07-07 14:35:50 -0700559 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700560 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700561}
562
563//
564// Java prebuilts
565//
566
Colin Cross74d73e22017-08-02 11:05:49 -0700567type ImportProperties struct {
568 Jars []string
569}
570
571type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700572 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700573 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700574
Colin Cross74d73e22017-08-02 11:05:49 -0700575 properties ImportProperties
576
577 classpathFiles android.Paths
578 combinedClasspathFile android.Path
Colin Crosse1d62a82015-04-03 16:53:05 -0700579 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700580}
581
Colin Cross74d73e22017-08-02 11:05:49 -0700582func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700583 return &j.prebuilt
584}
585
Colin Cross74d73e22017-08-02 11:05:49 -0700586func (j *Import) PrebuiltSrcs() []string {
587 return j.properties.Jars
588}
589
590func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700591 return j.prebuilt.Name(j.ModuleBase.Name())
592}
593
Colin Cross74d73e22017-08-02 11:05:49 -0700594func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700595}
596
Colin Cross74d73e22017-08-02 11:05:49 -0700597func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
598 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700599
Colin Cross74d73e22017-08-02 11:05:49 -0700600 for i, prebuilt := range j.classpathFiles {
601 subdir := "extracted" + strconv.Itoa(i)
602 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, subdir, prebuilt)
603 j.classJarSpecs = append(j.classJarSpecs, classJarSpec)
604 j.resourceJarSpecs = append(j.resourceJarSpecs, resourceJarSpec)
605 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700606
Colin Crossc6bbef32017-08-14 14:16:06 -0700607 j.combinedClasspathFile = TransformClassesToJar(ctx, j.classJarSpecs, android.OptionalPath{}, nil)
Colin Cross74d73e22017-08-02 11:05:49 -0700608
Colin Cross5c517922017-08-31 12:29:17 -0700609 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Cross74d73e22017-08-02 11:05:49 -0700610 ctx.ModuleName()+".jar", j.combinedClasspathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700611}
612
Colin Cross74d73e22017-08-02 11:05:49 -0700613var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700614
Colin Cross74d73e22017-08-02 11:05:49 -0700615func (j *Import) ClasspathFiles() android.Paths {
616 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700617}
618
Colin Cross74d73e22017-08-02 11:05:49 -0700619func (j *Import) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700620 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700621}
622
Colin Cross74d73e22017-08-02 11:05:49 -0700623func (j *Import) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700624 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700625}
626
Colin Cross74d73e22017-08-02 11:05:49 -0700627func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700628 return nil
629}
630
Colin Cross74d73e22017-08-02 11:05:49 -0700631var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700632
Colin Cross74d73e22017-08-02 11:05:49 -0700633func ImportFactory() android.Module {
634 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700635
Colin Cross74d73e22017-08-02 11:05:49 -0700636 module.AddProperties(&module.properties)
637
638 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700639 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
640 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700641}
642
Colin Cross74d73e22017-08-02 11:05:49 -0700643func ImportFactoryHost() android.Module {
644 module := &Import{}
645
646 module.AddProperties(&module.properties)
647
648 android.InitPrebuiltModule(module, &module.properties.Jars)
649 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
650 return module
651}
652
Colin Crossaa8630b2015-04-13 13:52:22 -0700653//
654// SDK java prebuilts (.jar containing resources plus framework.aidl)
655//
656
657type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700658 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700659 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700660}
661
662var _ sdkDependency = (*sdkPrebuilt)(nil)
663
Colin Cross7d5136f2015-05-11 13:39:40 -0700664type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700665 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700666}
667
Colin Crossaa8630b2015-04-13 13:52:22 -0700668type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700669 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700670
Colin Cross7d5136f2015-05-11 13:39:40 -0700671 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700672
Colin Cross635c3b02016-05-18 15:37:25 -0700673 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700674}
675
Colin Cross635c3b02016-05-18 15:37:25 -0700676func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700677 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700678
Colin Cross635c3b02016-05-18 15:37:25 -0700679 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700680}
681
Colin Cross635c3b02016-05-18 15:37:25 -0700682func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700683 return j.aidlPreprocessed
684}
685
Colin Cross36242852017-06-23 15:06:31 -0700686func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700687 module := &sdkPrebuilt{}
688
Colin Cross74d73e22017-08-02 11:05:49 -0700689 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700690
Colin Cross74d73e22017-08-02 11:05:49 -0700691 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700692 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
693 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700694}
695
Colin Cross2fe66872015-03-30 17:20:39 -0700696func inList(s string, l []string) bool {
697 for _, e := range l {
698 if e == s {
699 return true
700 }
701 }
702 return false
703}
Colin Cross89536d42017-07-07 14:35:50 -0700704
705//
706// Defaults
707//
708type Defaults struct {
709 android.ModuleBase
710 android.DefaultsModuleBase
711}
712
713func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
714}
715
716func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
717}
718
719func defaultsFactory() android.Module {
720 return DefaultsFactory()
721}
722
723func DefaultsFactory(props ...interface{}) android.Module {
724 module := &Defaults{}
725
726 module.AddProperties(props...)
727 module.AddProperties(
728 &CompilerProperties{},
729 &CompilerDeviceProperties{},
730 )
731
732 android.InitDefaultsModule(module)
733
734 return module
735}