blob: 1ca392831ea0b2aa77eb99b49b7250e42a451c38 [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 Crossa60ead82017-10-02 18:10:21 -070038 android.RegisterModuleType("java_library", LibraryFactory(true))
39 android.RegisterModuleType("java_library_static", LibraryFactory(false))
Colin Crossf506d872017-07-19 15:53:04 -070040 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 Cross86a63ff2017-09-27 17:33:10 -070073 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070074
Colin Cross86a63ff2017-09-27 17:33:10 -070075 // list of directories that should be excluded from java_resource_dirs
76 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070077
Colin Cross0f37af02017-09-27 17:42:05 -070078 // list of files to use as Java resources
79 Java_resources []string `android:"arch_variant"`
80
81 // list of files that should be excluded from java_resources
82 Exclude_java_resources []string `android:"arch_variant"`
83
Colin Crossfa5eb232017-10-01 20:33:03 -070084 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070086 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070087
Colin Crossfa5eb232017-10-01 20:33:03 -070088 // don't build against the framework libraries (legacy-test, core-junit,
89 // ext, and framework for device targets)
90 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Colin Cross7d5136f2015-05-11 13:39:40 -070095 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross540eff82017-06-22 17:01:52 -0700104 // if not blank, run jarjar using the specified rules file
105 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -0700106
107 // If not blank, set the java version passed to javac as -source and -target
108 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700109
110 // If set to false, don't allow this module to be installed. Defaults to true.
111 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700112
Colin Cross0f37af02017-09-27 17:42:05 -0700113 // If set to true, include sources used to compile the module in to the final jar
114 Include_srcs *bool
115
Colin Cross32f676a2017-09-06 13:41:06 -0700116 // List of modules to use as annotation processors
117 Annotation_processors []string
118
119 // List of classes to pass to javac to use as annotation processors
120 Annotation_processor_classes []string
Colin Cross540eff82017-06-22 17:01:52 -0700121}
122
Colin Cross89536d42017-07-07 14:35:50 -0700123type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700124 // list of module-specific flags that will be used for dex compiles
125 Dxflags []string `android:"arch_variant"`
126
Colin Cross7d5136f2015-05-11 13:39:40 -0700127 // if not blank, set to the version of the sdk to compile against
128 Sdk_version string
129
130 // Set for device java libraries, and for host versions of device java libraries
131 // built for testing
132 Dex bool `blueprint:"mutated"`
133
Colin Cross7d5136f2015-05-11 13:39:40 -0700134 // directories to pass to aidl tool
135 Aidl_includes []string
136
137 // directories that should be added as include directories
138 // for any aidl sources of modules that depend on this module
139 Export_aidl_include_dirs []string
140}
141
Colin Cross46c9b8b2017-06-22 16:51:17 -0700142// Module contains the properties and members used by all java module types
143type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700144 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700145 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700146
Colin Cross89536d42017-07-07 14:35:50 -0700147 properties CompilerProperties
148 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700149
150 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700151 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700152
Colin Cross6ade34f2017-09-15 13:00:47 -0700153 // output file containing classes.dex
154 dexJarFile android.Path
155
156 // output files containing resources
157 resourceJarFiles android.Paths
158
Colin Crossb7a63242015-04-16 14:09:14 -0700159 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700160 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700161
Colin Cross635c3b02016-05-18 15:37:25 -0700162 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700163
Colin Cross635c3b02016-05-18 15:37:25 -0700164 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700165
Colin Crossb7a63242015-04-16 14:09:14 -0700166 // filelists of extra source files that should be included in the javac command line,
167 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700168 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700169
Colin Cross2fe66872015-03-30 17:20:39 -0700170 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700171 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700172}
173
Colin Crossf506d872017-07-19 15:53:04 -0700174type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700175 ClasspathFiles() android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700176 ResourceJarFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700177 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700178}
179
Colin Cross89536d42017-07-07 14:35:50 -0700180func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
181 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
182 android.InitDefaultableModule(module)
183}
184
Colin Crossbe1da472017-07-07 15:59:46 -0700185type dependencyTag struct {
186 blueprint.BaseDependencyTag
187 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700188}
189
Colin Crossbe1da472017-07-07 15:59:46 -0700190var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700191 staticLibTag = dependencyTag{name: "staticlib"}
192 libTag = dependencyTag{name: "javalib"}
193 bootClasspathTag = dependencyTag{name: "bootclasspath"}
194 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700195)
Colin Cross2fe66872015-03-30 17:20:39 -0700196
Colin Crossfc3674a2017-09-18 17:41:52 -0700197type sdkDep struct {
198 useModule, useFiles, useDefaultLibs bool
199 module string
200 jar android.Path
201 aidl android.Path
202}
203
204func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
205 switch v {
206 case "", "current", "system_current", "test_current":
207 // OK
208 default:
209 if _, err := strconv.Atoi(v); err != nil {
210 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
211 return sdkDep{}
212 }
213 }
214
215 toFile := func(v string) sdkDep {
216 dir := filepath.Join("prebuilts/sdk", v)
217 jar := filepath.Join(dir, "android.jar")
218 aidl := filepath.Join(dir, "framework.aidl")
219 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
220 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
221 if !jarPath.Valid() {
222 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
223 return sdkDep{}
224 }
225 if !aidlPath.Valid() {
226 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
227 return sdkDep{}
228 }
229 return sdkDep{
230 useFiles: true,
231 jar: jarPath.Path(),
232 aidl: aidlPath.Path(),
233 }
234 }
235
236 toModule := func(m string) sdkDep {
237 return sdkDep{
238 useModule: true,
239 module: m,
240 }
241 }
242
Colin Cross8b9d37b2017-09-22 15:30:06 -0700243 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700244 return toFile(v)
245 }
246
247 switch v {
248 case "":
249 return sdkDep{
250 useDefaultLibs: true,
251 }
252 case "current":
253 return toModule("android_stubs_current")
254 case "system_current":
255 return toModule("android_system_stubs_current")
256 case "test_current":
257 return toModule("android_test_stubs_current")
258 default:
259 return toFile(v)
260 }
261}
262
Colin Crossbe1da472017-07-07 15:59:46 -0700263func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700264 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700265 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700266 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
267 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700268 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700269 if !proptools.Bool(j.properties.No_framework_libs) {
270 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
271 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700272 }
273 if sdkDep.useModule {
274 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700275 }
276 } else {
277 if j.deviceProperties.Dex {
Colin Crosscb2c9292017-09-23 19:57:16 -0700278 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700279 }
Colin Cross2fe66872015-03-30 17:20:39 -0700280 }
281 }
Colin Crossf506d872017-07-19 15:53:04 -0700282 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
283 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700284 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700285
286 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700287 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross2fe66872015-03-30 17:20:39 -0700288}
289
Colin Cross46c9b8b2017-06-22 16:51:17 -0700290func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700291 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700292
Colin Cross540eff82017-06-22 17:01:52 -0700293 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700294
295 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700296 if aidlPreprocess.Valid() {
297 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700298 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700299 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700300 }
301
Colin Cross635c3b02016-05-18 15:37:25 -0700302 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
303 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
304 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700305 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
306 flags = append(flags, "-I"+src.String())
307 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700308
Colin Crossf03c82b2015-04-13 13:53:40 -0700309 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700310}
311
Colin Cross32f676a2017-09-06 13:41:06 -0700312type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700313 classpath android.Paths
314 bootClasspath android.Paths
315 staticJars android.Paths
316 staticJarResources android.Paths
317 aidlIncludeDirs android.Paths
318 srcFileLists android.Paths
319 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700320}
Colin Cross2fe66872015-03-30 17:20:39 -0700321
Colin Cross32f676a2017-09-06 13:41:06 -0700322func (j *Module) collectDeps(ctx android.ModuleContext) deps {
323 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700324
325 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
326 if sdkDep.useFiles {
327 deps.classpath = append(deps.classpath, sdkDep.jar)
328 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
329 }
330
Colin Cross2fe66872015-03-30 17:20:39 -0700331 ctx.VisitDirectDeps(func(module blueprint.Module) {
332 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700333 tag := ctx.OtherModuleDependencyTag(module)
334
Colin Crossf506d872017-07-19 15:53:04 -0700335 dep, _ := module.(Dependency)
336 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700337 switch tag {
338 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700339 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700340 default:
341 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700342 }
Colin Crossec7a0422017-07-07 14:47:12 -0700343 return
344 }
345
Colin Crossbe1da472017-07-07 15:59:46 -0700346 switch tag {
347 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700348 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700349 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700350 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700351 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700352 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
353 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700354 deps.staticJarResources = append(deps.staticJarResources, dep.ResourceJarFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700355 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700356 if ctx.ModuleName() == "framework" {
357 // framework.jar has a one-off dependency on the R.java and Manifest.java files
358 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700359 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700360 }
Colin Crossbe1da472017-07-07 15:59:46 -0700361 default:
362 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700363 }
Colin Crossbe1da472017-07-07 15:59:46 -0700364
Colin Cross32f676a2017-09-06 13:41:06 -0700365 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700366 })
367
Colin Cross32f676a2017-09-06 13:41:06 -0700368 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700369}
370
Colin Cross46c9b8b2017-06-22 16:51:17 -0700371func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700372
Colin Cross540eff82017-06-22 17:01:52 -0700373 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700374
Colin Cross32f676a2017-09-06 13:41:06 -0700375 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700376
Colin Crossf03c82b2015-04-13 13:53:40 -0700377 var flags javaBuilderFlags
378
379 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700380 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
381 javacFlags = config.StripJavac9Flags(javacFlags)
382 }
Colin Cross64162712017-08-08 13:17:59 -0700383
384 if j.properties.Java_version != nil {
385 flags.javaVersion = *j.properties.Java_version
386 } else {
387 flags.javaVersion = "${config.DefaultJavaVersion}"
388 }
389
Colin Cross6ade34f2017-09-15 13:00:47 -0700390 flags.bootClasspath.AddPaths(deps.bootClasspath)
391 flags.classpath.AddPaths(deps.classpath)
392
Colin Crossf03c82b2015-04-13 13:53:40 -0700393 if len(javacFlags) > 0 {
394 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
395 flags.javacFlags = "$javacFlags"
396 }
397
Colin Cross32f676a2017-09-06 13:41:06 -0700398 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700399 if len(aidlFlags) > 0 {
400 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
401 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700402 }
403
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700404 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700405
Colin Crossf05fe972015-04-10 17:45:20 -0700406 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700407
Colin Cross0607cf72015-04-28 13:28:51 -0700408 ctx.VisitDirectDeps(func(module blueprint.Module) {
409 if gen, ok := module.(genrule.SourceFileGenerator); ok {
410 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
411 }
412 })
413
Colin Cross32f676a2017-09-06 13:41:06 -0700414 deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700415
Colin Cross0a6e0072017-08-30 14:24:55 -0700416 var jars android.Paths
417
Colin Cross8cf13342015-04-10 15:41:49 -0700418 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700419 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700420 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
421 // If error-prone is enabled, add an additional rule to compile the java files into
422 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700423 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700424 // 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 Crossd6891432017-09-27 17:39:56 -0700426 errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, nil)
Colin Crossc6bbef32017-08-14 14:16:06 -0700427 extraJarDeps = append(extraJarDeps, errorprone)
428 }
429
Colin Crossd6891432017-09-27 17:39:56 -0700430 // Compile java sources into .class files
431 classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraJarDeps)
432 if ctx.Failed() {
433 return
434 }
435
Colin Cross0a6e0072017-08-30 14:24:55 -0700436 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700437 }
438
Colin Cross0f37af02017-09-27 17:42:05 -0700439 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
440 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
441
442 var resArgs []string
443 var resDeps android.Paths
444
445 resArgs = append(resArgs, dirArgs...)
446 resDeps = append(resDeps, dirDeps...)
447
448 resArgs = append(resArgs, fileArgs...)
449 resDeps = append(resDeps, fileDeps...)
450
451 if proptools.Bool(j.properties.Include_srcs) {
452 srcArgs, srcDeps := ResourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
453 resArgs = append(resArgs, srcArgs...)
454 resDeps = append(resDeps, srcDeps...)
455 }
Colin Cross40a36712017-09-27 17:41:35 -0700456
457 if len(resArgs) > 0 {
Colin Cross0a6e0072017-08-30 14:24:55 -0700458 // Combine classes + resources into classes-full-debug.jar
Colin Cross40a36712017-09-27 17:41:35 -0700459 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700460 if ctx.Failed() {
461 return
462 }
Colin Cross20978302015-04-10 17:05:07 -0700463
Colin Cross6ade34f2017-09-15 13:00:47 -0700464 j.resourceJarFiles = append(j.resourceJarFiles, resourceJar)
Colin Cross0a6e0072017-08-30 14:24:55 -0700465 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700466 }
467
Colin Cross6ade34f2017-09-15 13:00:47 -0700468 // Propagate the resources from the transitive closure of static dependencies for copying
469 // into dex jars
470 j.resourceJarFiles = append(j.resourceJarFiles, deps.staticJarResources...)
471
472 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700473 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700474
Colin Cross635acc92017-09-12 22:50:46 -0700475 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
476
Colin Cross0a6e0072017-08-30 14:24:55 -0700477 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700478 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700479 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700480
481 if j.properties.Jarjar_rules != nil {
482 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700483 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700484 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
485 if ctx.Failed() {
486 return
487 }
488 }
489
Colin Cross2fe66872015-03-30 17:20:39 -0700490 j.classpathFile = outputFile
491
Colin Crossa713a6f2017-09-20 18:04:44 -0700492 // TODO(ccross): handle hostdex
Colin Cross59f1bb62017-09-27 17:59:10 -0700493 if ctx.Device() && len(srcFiles) > 0 && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700494 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700495 if false /* emma enabled */ {
496 // If you instrument class files that have local variable debug information in
497 // them emma does not correctly maintain the local variable table.
498 // This will cause an error when you try to convert the class files for Android.
499 // The workaround here is to build different dex file here based on emma switch
500 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
501 // option to remove local variable information
502 dxFlags = append(dxFlags, "--no-locals")
503 }
504
Colin Cross1332b002015-04-07 17:11:30 -0700505 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700506 dxFlags = append(dxFlags, "--no-optimize")
507 }
508
Colin Cross1332b002015-04-07 17:11:30 -0700509 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700510 dxFlags = append(dxFlags,
511 "--debug",
512 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700513 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700514 "--dump-width=1000")
515 }
516
Colin Cross595a4062017-08-31 12:30:37 -0700517 var minSdkVersion string
518 switch j.deviceProperties.Sdk_version {
519 case "", "current", "test_current", "system_current":
520 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
521 default:
522 minSdkVersion = j.deviceProperties.Sdk_version
523 }
524
525 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
526
Colin Cross2fe66872015-03-30 17:20:39 -0700527 flags.dxFlags = strings.Join(dxFlags, " ")
528
Colin Cross6ade34f2017-09-15 13:00:47 -0700529 desugarFlags := []string{
530 "--min_sdk_version " + minSdkVersion,
531 "--desugar_try_with_resources_if_needed=false",
532 "--allow_empty_bootclasspath",
533 }
534
535 if inList("--core-library", dxFlags) {
536 desugarFlags = append(desugarFlags, "--core_library")
537 }
538
539 flags.desugarFlags = strings.Join(desugarFlags, " ")
540
541 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700542 if ctx.Failed() {
543 return
544 }
545
Colin Cross6ade34f2017-09-15 13:00:47 -0700546 // Compile classes.jar into classes.dex
547 dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
548 if ctx.Failed() {
549 return
550 }
551
552 jars := android.Paths{dexJarFile}
553 jars = append(jars, j.resourceJarFiles...)
554
555 outputFile = TransformJarsToJar(ctx, "javalib.jar", jars, android.OptionalPath{}, true)
556
557 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700558 }
Colin Crossb7a63242015-04-16 14:09:14 -0700559 ctx.CheckbuildFile(outputFile)
560 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700561}
562
Colin Cross59f1bb62017-09-27 17:59:10 -0700563func (j *Module) installable() bool {
564 return j.properties.Installable == nil || *j.properties.Installable
565}
566
Colin Crossf506d872017-07-19 15:53:04 -0700567var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700568
Colin Cross74d73e22017-08-02 11:05:49 -0700569func (j *Module) ClasspathFiles() android.Paths {
570 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700571}
572
Colin Cross6ade34f2017-09-15 13:00:47 -0700573func (j *Module) ResourceJarFiles() android.Paths {
574 return j.resourceJarFiles
575}
576
Colin Cross46c9b8b2017-06-22 16:51:17 -0700577func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700578 return j.exportAidlIncludeDirs
579}
580
Colin Cross46c9b8b2017-06-22 16:51:17 -0700581var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700582
Colin Cross46c9b8b2017-06-22 16:51:17 -0700583func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700584 return j.logtagsSrcs
585}
586
Colin Cross2fe66872015-03-30 17:20:39 -0700587//
588// Java libraries (.jar file)
589//
590
Colin Crossf506d872017-07-19 15:53:04 -0700591type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700592 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700593}
594
Colin Crossf506d872017-07-19 15:53:04 -0700595func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700596 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700597
Colin Cross59f1bb62017-09-27 17:59:10 -0700598 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700599 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
600 ctx.ModuleName()+".jar", j.outputFile)
601 }
Colin Crossb7a63242015-04-16 14:09:14 -0700602}
603
Colin Crossf506d872017-07-19 15:53:04 -0700604func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700605 j.deps(ctx)
606}
607
Colin Crossa60ead82017-10-02 18:10:21 -0700608func LibraryFactory(installable bool) func() android.Module {
609 return func() android.Module {
610 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700611
Colin Crossa60ead82017-10-02 18:10:21 -0700612 if !installable {
613 module.properties.Installable = proptools.BoolPtr(false)
614 }
615 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700616
Colin Crossa60ead82017-10-02 18:10:21 -0700617 module.AddProperties(
618 &module.Module.properties,
619 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700620
Colin Crossa60ead82017-10-02 18:10:21 -0700621 InitJavaModule(module, android.HostAndDeviceSupported)
622 return module
623 }
Colin Cross2fe66872015-03-30 17:20:39 -0700624}
625
Colin Crossf506d872017-07-19 15:53:04 -0700626func LibraryHostFactory() android.Module {
627 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700628
Colin Cross36242852017-06-23 15:06:31 -0700629 module.AddProperties(&module.Module.properties)
630
Colin Cross89536d42017-07-07 14:35:50 -0700631 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700632 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700633}
634
635//
636// Java Binaries (.jar file plus wrapper script)
637//
638
Colin Crossf506d872017-07-19 15:53:04 -0700639type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700640 // installable script to execute the resulting jar
641 Wrapper string
642}
643
Colin Crossf506d872017-07-19 15:53:04 -0700644type Binary struct {
645 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700646
Colin Crossf506d872017-07-19 15:53:04 -0700647 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700648
649 wrapperFile android.ModuleSrcPath
650 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700651}
652
Colin Crossf506d872017-07-19 15:53:04 -0700653func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
654 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700655
656 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
657 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700658 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700659 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
660 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700661}
662
Colin Crossf506d872017-07-19 15:53:04 -0700663func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700664 j.deps(ctx)
665}
666
Colin Crossf506d872017-07-19 15:53:04 -0700667func BinaryFactory() android.Module {
668 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700669
Colin Cross540eff82017-06-22 17:01:52 -0700670 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700671
Colin Cross36242852017-06-23 15:06:31 -0700672 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700673 &module.Module.properties,
674 &module.Module.deviceProperties,
675 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700676
Colin Cross89536d42017-07-07 14:35:50 -0700677 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700678 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700679}
680
Colin Crossf506d872017-07-19 15:53:04 -0700681func BinaryHostFactory() android.Module {
682 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700683
Colin Cross36242852017-06-23 15:06:31 -0700684 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700685 &module.Module.properties,
686 &module.Module.deviceProperties,
687 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700688
Colin Cross89536d42017-07-07 14:35:50 -0700689 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700690 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700691}
692
693//
694// Java prebuilts
695//
696
Colin Cross74d73e22017-08-02 11:05:49 -0700697type ImportProperties struct {
698 Jars []string
699}
700
701type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700702 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700703 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700704
Colin Cross74d73e22017-08-02 11:05:49 -0700705 properties ImportProperties
706
Colin Cross0a6e0072017-08-30 14:24:55 -0700707 classpathFiles android.Paths
708 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700709}
710
Colin Cross74d73e22017-08-02 11:05:49 -0700711func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700712 return &j.prebuilt
713}
714
Colin Cross74d73e22017-08-02 11:05:49 -0700715func (j *Import) PrebuiltSrcs() []string {
716 return j.properties.Jars
717}
718
719func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700720 return j.prebuilt.Name(j.ModuleBase.Name())
721}
722
Colin Cross74d73e22017-08-02 11:05:49 -0700723func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700724}
725
Colin Cross74d73e22017-08-02 11:05:49 -0700726func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
727 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700728
Colin Cross635acc92017-09-12 22:50:46 -0700729 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700730}
731
Colin Cross74d73e22017-08-02 11:05:49 -0700732var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700733
Colin Cross74d73e22017-08-02 11:05:49 -0700734func (j *Import) ClasspathFiles() android.Paths {
735 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700736}
737
Colin Cross6ade34f2017-09-15 13:00:47 -0700738func (j *Import) ResourceJarFiles() android.Paths {
739 // resources are in the ClasspathFiles
740 return nil
741}
742
Colin Cross74d73e22017-08-02 11:05:49 -0700743func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700744 return nil
745}
746
Colin Cross74d73e22017-08-02 11:05:49 -0700747var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700748
Colin Cross74d73e22017-08-02 11:05:49 -0700749func ImportFactory() android.Module {
750 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700751
Colin Cross74d73e22017-08-02 11:05:49 -0700752 module.AddProperties(&module.properties)
753
754 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700755 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
756 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700757}
758
Colin Cross74d73e22017-08-02 11:05:49 -0700759func ImportFactoryHost() android.Module {
760 module := &Import{}
761
762 module.AddProperties(&module.properties)
763
764 android.InitPrebuiltModule(module, &module.properties.Jars)
765 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
766 return module
767}
768
Colin Cross2fe66872015-03-30 17:20:39 -0700769func inList(s string, l []string) bool {
770 for _, e := range l {
771 if e == s {
772 return true
773 }
774 }
775 return false
776}
Colin Cross89536d42017-07-07 14:35:50 -0700777
778//
779// Defaults
780//
781type Defaults struct {
782 android.ModuleBase
783 android.DefaultsModuleBase
784}
785
786func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
787}
788
789func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
790}
791
792func defaultsFactory() android.Module {
793 return DefaultsFactory()
794}
795
796func DefaultsFactory(props ...interface{}) android.Module {
797 module := &Defaults{}
798
799 module.AddProperties(props...)
800 module.AddProperties(
801 &CompilerProperties{},
802 &CompilerDeviceProperties{},
803 )
804
805 android.InitDefaultsModule(module)
806
807 return module
808}