blob: 43f97acda2c579181228099e94f2f38af9d9f17c [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 Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070031 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Crossf506d872017-07-19 15:53:04 -070038 android.RegisterModuleType("java_library", LibraryFactory)
39 android.RegisterModuleType("java_library_static", LibraryFactory)
40 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070043 android.RegisterModuleType("java_import", ImportFactory)
44 android.RegisterModuleType("java_import_host", ImportFactoryHost)
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 Cross2c429dc2017-08-31 16:45:16 -070099
100 // If set to false, don't allow this module to be installed. Defaults to true.
101 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700102
103 // List of modules to use as annotation processors
104 Annotation_processors []string
105
106 // List of classes to pass to javac to use as annotation processors
107 Annotation_processor_classes []string
Colin Cross540eff82017-06-22 17:01:52 -0700108}
109
Colin Cross89536d42017-07-07 14:35:50 -0700110type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700111 // list of module-specific flags that will be used for dex compiles
112 Dxflags []string `android:"arch_variant"`
113
Colin Cross7d5136f2015-05-11 13:39:40 -0700114 // if not blank, set to the version of the sdk to compile against
115 Sdk_version string
116
117 // Set for device java libraries, and for host versions of device java libraries
118 // built for testing
119 Dex bool `blueprint:"mutated"`
120
Colin Cross7d5136f2015-05-11 13:39:40 -0700121 // directories to pass to aidl tool
122 Aidl_includes []string
123
124 // directories that should be added as include directories
125 // for any aidl sources of modules that depend on this module
126 Export_aidl_include_dirs []string
127}
128
Colin Cross46c9b8b2017-06-22 16:51:17 -0700129// Module contains the properties and members used by all java module types
130type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700131 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700132 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700133
Colin Cross89536d42017-07-07 14:35:50 -0700134 properties CompilerProperties
135 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700136
137 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700138 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700139
Colin Cross6ade34f2017-09-15 13:00:47 -0700140 // output file containing classes.dex
141 dexJarFile android.Path
142
143 // output files containing resources
144 resourceJarFiles android.Paths
145
Colin Crossb7a63242015-04-16 14:09:14 -0700146 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700147 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700148
Colin Cross635c3b02016-05-18 15:37:25 -0700149 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700150
Colin Cross635c3b02016-05-18 15:37:25 -0700151 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700152
Colin Crossb7a63242015-04-16 14:09:14 -0700153 // filelists of extra source files that should be included in the javac command line,
154 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700155 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700156
Colin Cross2fe66872015-03-30 17:20:39 -0700157 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700158 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700159}
160
Colin Crossf506d872017-07-19 15:53:04 -0700161type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700162 ClasspathFiles() android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700163 ResourceJarFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700164 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700165}
166
Colin Cross89536d42017-07-07 14:35:50 -0700167func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
168 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
169 android.InitDefaultableModule(module)
170}
171
Colin Crossbe1da472017-07-07 15:59:46 -0700172type dependencyTag struct {
173 blueprint.BaseDependencyTag
174 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700175}
176
Colin Crossbe1da472017-07-07 15:59:46 -0700177var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700178 staticLibTag = dependencyTag{name: "staticlib"}
179 libTag = dependencyTag{name: "javalib"}
180 bootClasspathTag = dependencyTag{name: "bootclasspath"}
181 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700182)
Colin Cross2fe66872015-03-30 17:20:39 -0700183
Colin Crossfc3674a2017-09-18 17:41:52 -0700184type sdkDep struct {
185 useModule, useFiles, useDefaultLibs bool
186 module string
187 jar android.Path
188 aidl android.Path
189}
190
191func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
192 switch v {
193 case "", "current", "system_current", "test_current":
194 // OK
195 default:
196 if _, err := strconv.Atoi(v); err != nil {
197 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
198 return sdkDep{}
199 }
200 }
201
202 toFile := func(v string) sdkDep {
203 dir := filepath.Join("prebuilts/sdk", v)
204 jar := filepath.Join(dir, "android.jar")
205 aidl := filepath.Join(dir, "framework.aidl")
206 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
207 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
208 if !jarPath.Valid() {
209 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
210 return sdkDep{}
211 }
212 if !aidlPath.Valid() {
213 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
214 return sdkDep{}
215 }
216 return sdkDep{
217 useFiles: true,
218 jar: jarPath.Path(),
219 aidl: aidlPath.Path(),
220 }
221 }
222
223 toModule := func(m string) sdkDep {
224 return sdkDep{
225 useModule: true,
226 module: m,
227 }
228 }
229
230 if ctx.AConfig().UnbundledBuild() {
231 if v == "" {
232 if ctx, ok := ctx.(android.ModuleContext); ok {
233 ctx.AddMissingDependencies([]string{"sdk_version_must_be_set_for_modules_used_in_unbundled_builds"})
234 }
235 return sdkDep{}
236 }
237 return toFile(v)
238 }
239
240 switch v {
241 case "":
242 return sdkDep{
243 useDefaultLibs: true,
244 }
245 case "current":
246 return toModule("android_stubs_current")
247 case "system_current":
248 return toModule("android_system_stubs_current")
249 case "test_current":
250 return toModule("android_test_stubs_current")
251 default:
252 return toFile(v)
253 }
254}
255
Colin Crossbe1da472017-07-07 15:59:46 -0700256func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700257 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700258 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700259 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
260 if sdkDep.useDefaultLibs {
Colin Cross227d4362017-08-30 14:14:52 -0700261 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
262 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossfc3674a2017-09-18 17:41:52 -0700263 }
264 if sdkDep.useModule {
265 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700266 }
267 } else {
268 if j.deviceProperties.Dex {
Colin Cross227d4362017-08-30 14:14:52 -0700269 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
Colin Crossbe1da472017-07-07 15:59:46 -0700270 }
Colin Cross2fe66872015-03-30 17:20:39 -0700271 }
272 }
Colin Crossf506d872017-07-19 15:53:04 -0700273 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
274 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700275 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700276
277 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross2fe66872015-03-30 17:20:39 -0700278}
279
Colin Cross46c9b8b2017-06-22 16:51:17 -0700280func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700281 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700282
Colin Cross540eff82017-06-22 17:01:52 -0700283 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700284
285 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700286 if aidlPreprocess.Valid() {
287 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700288 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700289 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700290 }
291
Colin Cross635c3b02016-05-18 15:37:25 -0700292 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
293 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
294 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700295 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
296 flags = append(flags, "-I"+src.String())
297 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700298
Colin Crossf03c82b2015-04-13 13:53:40 -0700299 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700300}
301
Colin Cross32f676a2017-09-06 13:41:06 -0700302type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700303 classpath android.Paths
304 bootClasspath android.Paths
305 staticJars android.Paths
306 staticJarResources android.Paths
307 aidlIncludeDirs android.Paths
308 srcFileLists android.Paths
309 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700310}
Colin Cross2fe66872015-03-30 17:20:39 -0700311
Colin Cross32f676a2017-09-06 13:41:06 -0700312func (j *Module) collectDeps(ctx android.ModuleContext) deps {
313 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700314
315 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
316 if sdkDep.useFiles {
317 deps.classpath = append(deps.classpath, sdkDep.jar)
318 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
319 }
320
Colin Cross2fe66872015-03-30 17:20:39 -0700321 ctx.VisitDirectDeps(func(module blueprint.Module) {
322 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700323 tag := ctx.OtherModuleDependencyTag(module)
324
Colin Crossf506d872017-07-19 15:53:04 -0700325 dep, _ := module.(Dependency)
326 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700327 switch tag {
328 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700329 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700330 default:
331 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700332 }
Colin Crossec7a0422017-07-07 14:47:12 -0700333 return
334 }
335
Colin Crossbe1da472017-07-07 15:59:46 -0700336 switch tag {
337 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700338 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700339 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700340 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700341 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700342 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
343 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700344 deps.staticJarResources = append(deps.staticJarResources, dep.ResourceJarFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700345 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700346 if ctx.ModuleName() == "framework" {
347 // framework.jar has a one-off dependency on the R.java and Manifest.java files
348 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700349 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700350 }
Colin Crossbe1da472017-07-07 15:59:46 -0700351 default:
352 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700353 }
Colin Crossbe1da472017-07-07 15:59:46 -0700354
Colin Cross32f676a2017-09-06 13:41:06 -0700355 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700356 })
357
Colin Cross32f676a2017-09-06 13:41:06 -0700358 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700359}
360
Colin Cross46c9b8b2017-06-22 16:51:17 -0700361func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700362
Colin Cross540eff82017-06-22 17:01:52 -0700363 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700364
Colin Cross32f676a2017-09-06 13:41:06 -0700365 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700366
Colin Crossf03c82b2015-04-13 13:53:40 -0700367 var flags javaBuilderFlags
368
369 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700370 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
371 javacFlags = config.StripJavac9Flags(javacFlags)
372 }
Colin Cross64162712017-08-08 13:17:59 -0700373
374 if j.properties.Java_version != nil {
375 flags.javaVersion = *j.properties.Java_version
376 } else {
377 flags.javaVersion = "${config.DefaultJavaVersion}"
378 }
379
Colin Cross6ade34f2017-09-15 13:00:47 -0700380 var extraDeps android.Paths
381
382 flags.bootClasspath.AddPaths(deps.bootClasspath)
383 flags.classpath.AddPaths(deps.classpath)
384
Colin Crossf03c82b2015-04-13 13:53:40 -0700385 if len(javacFlags) > 0 {
386 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
387 flags.javacFlags = "$javacFlags"
388 }
389
Colin Cross32f676a2017-09-06 13:41:06 -0700390 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700391 if len(aidlFlags) > 0 {
392 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
393 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700394 }
395
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700396 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700397
Colin Crossf05fe972015-04-10 17:45:20 -0700398 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700399
Colin Cross0607cf72015-04-28 13:28:51 -0700400 ctx.VisitDirectDeps(func(module blueprint.Module) {
401 if gen, ok := module.(genrule.SourceFileGenerator); ok {
402 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
403 }
404 })
405
Colin Cross32f676a2017-09-06 13:41:06 -0700406 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700407
Colin Crossc6bbef32017-08-14 14:16:06 -0700408 var extraJarDeps android.Paths
409
Colin Cross0a6e0072017-08-30 14:24:55 -0700410 var jars android.Paths
411
Colin Cross8cf13342015-04-10 15:41:49 -0700412 if len(srcFiles) > 0 {
413 // Compile java sources into .class files
Colin Cross32f676a2017-09-06 13:41:06 -0700414 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
Colin Cross8cf13342015-04-10 15:41:49 -0700415 if ctx.Failed() {
416 return
417 }
418
Colin Crossc6bbef32017-08-14 14:16:06 -0700419 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
420 // If error-prone is enabled, add an additional rule to compile the java files into
421 // a separate set of classes (so that they don't overwrite the normal ones and require
422 // a rebuild when error-prone is turned off). Add the classes as a dependency to
423 // the jar command so the two compiles can run in parallel.
424 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
425 // enable error-prone without affecting the output class files.
Colin Cross32f676a2017-09-06 13:41:06 -0700426 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
Colin Crossc6bbef32017-08-14 14:16:06 -0700427 extraJarDeps = append(extraJarDeps, errorprone)
428 }
429
Colin Cross0a6e0072017-08-30 14:24:55 -0700430 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700431 }
432
Colin Cross0a6e0072017-08-30 14:24:55 -0700433 resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs)
Colin Cross635acc92017-09-12 22:50:46 -0700434 if len(resourceJarSpecs) > 0 {
Colin Cross0a6e0072017-08-30 14:24:55 -0700435 // Combine classes + resources into classes-full-debug.jar
Colin Cross635acc92017-09-12 22:50:46 -0700436 resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, extraJarDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700437 if ctx.Failed() {
438 return
439 }
Colin Cross20978302015-04-10 17:05:07 -0700440
Colin Cross6ade34f2017-09-15 13:00:47 -0700441 j.resourceJarFiles = append(j.resourceJarFiles, resourceJar)
Colin Cross0a6e0072017-08-30 14:24:55 -0700442 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700443 }
444
Colin Cross6ade34f2017-09-15 13:00:47 -0700445 // Propagate the resources from the transitive closure of static dependencies for copying
446 // into dex jars
447 j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...)
448
449 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700450 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700451
Colin Cross635acc92017-09-12 22:50:46 -0700452 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
453
Colin Cross0a6e0072017-08-30 14:24:55 -0700454 // Combine the classes built from sources, any manifests, and any static libraries into
455 // classes-combined.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700456 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700457
458 if j.properties.Jarjar_rules != nil {
459 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
460 // Transform classes-combined.jar into classes-jarjar.jar
461 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
462 if ctx.Failed() {
463 return
464 }
465 }
466
Colin Cross2fe66872015-03-30 17:20:39 -0700467 j.classpathFile = outputFile
468
Colin Crossa713a6f2017-09-20 18:04:44 -0700469 // TODO(ccross): handle hostdex
470 if ctx.Device() && len(srcFiles) > 0 {
Colin Cross540eff82017-06-22 17:01:52 -0700471 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700472 if false /* emma enabled */ {
473 // If you instrument class files that have local variable debug information in
474 // them emma does not correctly maintain the local variable table.
475 // This will cause an error when you try to convert the class files for Android.
476 // The workaround here is to build different dex file here based on emma switch
477 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
478 // option to remove local variable information
479 dxFlags = append(dxFlags, "--no-locals")
480 }
481
Colin Cross1332b002015-04-07 17:11:30 -0700482 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700483 dxFlags = append(dxFlags, "--no-optimize")
484 }
485
Colin Cross1332b002015-04-07 17:11:30 -0700486 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700487 dxFlags = append(dxFlags,
488 "--debug",
489 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700490 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700491 "--dump-width=1000")
492 }
493
Colin Cross595a4062017-08-31 12:30:37 -0700494 var minSdkVersion string
495 switch j.deviceProperties.Sdk_version {
496 case "", "current", "test_current", "system_current":
497 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
498 default:
499 minSdkVersion = j.deviceProperties.Sdk_version
500 }
501
502 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
503
Colin Cross2fe66872015-03-30 17:20:39 -0700504 flags.dxFlags = strings.Join(dxFlags, " ")
505
Colin Cross6ade34f2017-09-15 13:00:47 -0700506 desugarFlags := []string{
507 "--min_sdk_version " + minSdkVersion,
508 "--desugar_try_with_resources_if_needed=false",
509 "--allow_empty_bootclasspath",
510 }
511
512 if inList("--core-library", dxFlags) {
513 desugarFlags = append(desugarFlags, "--core_library")
514 }
515
516 flags.desugarFlags = strings.Join(desugarFlags, " ")
517
518 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700519 if ctx.Failed() {
520 return
521 }
522
Colin Cross6ade34f2017-09-15 13:00:47 -0700523 // TODO(ccross): For now, use the desugared jar as the classpath file. Eventually this
524 // might cause problems because desugar wants non-desugared jars in its class path.
525 j.classpathFile = desugarJar
526
527 // Compile classes.jar into classes.dex
528 dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
529 if ctx.Failed() {
530 return
531 }
532
533 jars := android.Paths{dexJarFile}
534 jars = append(jars, j.resourceJarFiles...)
535
536 outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true)
537
538 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700539 }
Colin Crossb7a63242015-04-16 14:09:14 -0700540 ctx.CheckbuildFile(outputFile)
541 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700542}
543
Colin Crossf506d872017-07-19 15:53:04 -0700544var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700545
Colin Cross74d73e22017-08-02 11:05:49 -0700546func (j *Module) ClasspathFiles() android.Paths {
547 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700548}
549
Colin Cross6ade34f2017-09-15 13:00:47 -0700550func (j *Module) ResourceJarFiles() android.Paths {
551 return j.resourceJarFiles
552}
553
Colin Cross46c9b8b2017-06-22 16:51:17 -0700554func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700555 return j.exportAidlIncludeDirs
556}
557
Colin Cross46c9b8b2017-06-22 16:51:17 -0700558var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700559
Colin Cross46c9b8b2017-06-22 16:51:17 -0700560func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700561 return j.logtagsSrcs
562}
563
Colin Cross2fe66872015-03-30 17:20:39 -0700564//
565// Java libraries (.jar file)
566//
567
Colin Crossf506d872017-07-19 15:53:04 -0700568type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700569 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700570}
571
Colin Crossf506d872017-07-19 15:53:04 -0700572func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700573 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700574
Colin Cross2c429dc2017-08-31 16:45:16 -0700575 if j.properties.Installable == nil || *j.properties.Installable == true {
576 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
577 ctx.ModuleName()+".jar", j.outputFile)
578 }
Colin Crossb7a63242015-04-16 14:09:14 -0700579}
580
Colin Crossf506d872017-07-19 15:53:04 -0700581func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700582 j.deps(ctx)
583}
584
Colin Crossf506d872017-07-19 15:53:04 -0700585func LibraryFactory() android.Module {
586 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700587
Colin Cross540eff82017-06-22 17:01:52 -0700588 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700589
Colin Cross36242852017-06-23 15:06:31 -0700590 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700591 &module.Module.properties,
592 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700593
Colin Cross89536d42017-07-07 14:35:50 -0700594 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700595 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700596}
597
Colin Crossf506d872017-07-19 15:53:04 -0700598func LibraryHostFactory() android.Module {
599 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700600
Colin Cross36242852017-06-23 15:06:31 -0700601 module.AddProperties(&module.Module.properties)
602
Colin Cross89536d42017-07-07 14:35:50 -0700603 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700604 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700605}
606
607//
608// Java Binaries (.jar file plus wrapper script)
609//
610
Colin Crossf506d872017-07-19 15:53:04 -0700611type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700612 // installable script to execute the resulting jar
613 Wrapper string
614}
615
Colin Crossf506d872017-07-19 15:53:04 -0700616type Binary struct {
617 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700618
Colin Crossf506d872017-07-19 15:53:04 -0700619 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700620
621 wrapperFile android.ModuleSrcPath
622 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700623}
624
Colin Crossf506d872017-07-19 15:53:04 -0700625func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
626 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700627
628 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
629 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700630 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700631 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
632 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700633}
634
Colin Crossf506d872017-07-19 15:53:04 -0700635func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700636 j.deps(ctx)
637}
638
Colin Crossf506d872017-07-19 15:53:04 -0700639func BinaryFactory() android.Module {
640 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700641
Colin Cross540eff82017-06-22 17:01:52 -0700642 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700643
Colin Cross36242852017-06-23 15:06:31 -0700644 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700645 &module.Module.properties,
646 &module.Module.deviceProperties,
647 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700648
Colin Cross89536d42017-07-07 14:35:50 -0700649 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700650 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700651}
652
Colin Crossf506d872017-07-19 15:53:04 -0700653func BinaryHostFactory() android.Module {
654 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700655
Colin Cross36242852017-06-23 15:06:31 -0700656 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700657 &module.Module.properties,
658 &module.Module.deviceProperties,
659 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700660
Colin Cross89536d42017-07-07 14:35:50 -0700661 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700662 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700663}
664
665//
666// Java prebuilts
667//
668
Colin Cross74d73e22017-08-02 11:05:49 -0700669type ImportProperties struct {
670 Jars []string
671}
672
673type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700674 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700675 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700676
Colin Cross74d73e22017-08-02 11:05:49 -0700677 properties ImportProperties
678
Colin Cross0a6e0072017-08-30 14:24:55 -0700679 classpathFiles android.Paths
680 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700681}
682
Colin Cross74d73e22017-08-02 11:05:49 -0700683func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700684 return &j.prebuilt
685}
686
Colin Cross74d73e22017-08-02 11:05:49 -0700687func (j *Import) PrebuiltSrcs() []string {
688 return j.properties.Jars
689}
690
691func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700692 return j.prebuilt.Name(j.ModuleBase.Name())
693}
694
Colin Cross74d73e22017-08-02 11:05:49 -0700695func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700696}
697
Colin Cross74d73e22017-08-02 11:05:49 -0700698func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
699 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700700
Colin Cross635acc92017-09-12 22:50:46 -0700701 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700702}
703
Colin Cross74d73e22017-08-02 11:05:49 -0700704var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700705
Colin Cross74d73e22017-08-02 11:05:49 -0700706func (j *Import) ClasspathFiles() android.Paths {
707 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700708}
709
Colin Cross6ade34f2017-09-15 13:00:47 -0700710func (j *Import) ResourceJarFiles() android.Paths {
711 // resources are in the ClasspathFiles
712 return nil
713}
714
Colin Cross74d73e22017-08-02 11:05:49 -0700715func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700716 return nil
717}
718
Colin Cross74d73e22017-08-02 11:05:49 -0700719var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700720
Colin Cross74d73e22017-08-02 11:05:49 -0700721func ImportFactory() android.Module {
722 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700723
Colin Cross74d73e22017-08-02 11:05:49 -0700724 module.AddProperties(&module.properties)
725
726 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700727 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
728 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700729}
730
Colin Cross74d73e22017-08-02 11:05:49 -0700731func ImportFactoryHost() android.Module {
732 module := &Import{}
733
734 module.AddProperties(&module.properties)
735
736 android.InitPrebuiltModule(module, &module.properties.Jars)
737 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
738 return module
739}
740
Colin Cross2fe66872015-03-30 17:20:39 -0700741func inList(s string, l []string) bool {
742 for _, e := range l {
743 if e == s {
744 return true
745 }
746 }
747 return false
748}
Colin Cross89536d42017-07-07 14:35:50 -0700749
750//
751// Defaults
752//
753type Defaults struct {
754 android.ModuleBase
755 android.DefaultsModuleBase
756}
757
758func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
759}
760
761func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
762}
763
764func defaultsFactory() android.Module {
765 return DefaultsFactory()
766}
767
768func DefaultsFactory(props ...interface{}) android.Module {
769 module := &Defaults{}
770
771 module.AddProperties(props...)
772 module.AddProperties(
773 &CompilerProperties{},
774 &CompilerDeviceProperties{},
775 )
776
777 android.InitDefaultsModule(module)
778
779 return module
780}