blob: bab77c5a6d42dfda902cbaf691a7741f47a33905 [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// Renderscript
53// Post-jar passes:
54// Proguard
Colin Crossba211132017-06-22 15:36:39 -070055// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070056// Jarjar
57// Dex
58// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070059// DroidDoc
60// Findbugs
61
Colin Cross89536d42017-07-07 14:35:50 -070062type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070063 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
64 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070065 Srcs []string `android:"arch_variant"`
66
67 // list of source files that should not be used to build the Java module.
68 // This is most useful in the arch/multilib variants to remove non-common files
69 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070070
71 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070072 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070073
Colin Cross86a63ff2017-09-27 17:33:10 -070074 // list of directories that should be excluded from java_resource_dirs
75 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070076
Colin Cross0f37af02017-09-27 17:42:05 -070077 // list of files to use as Java resources
78 Java_resources []string `android:"arch_variant"`
79
80 // list of files that should be excluded from java_resources
81 Exclude_java_resources []string `android:"arch_variant"`
82
Colin Crossfa5eb232017-10-01 20:33:03 -070083 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070084 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070085 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070086
Colin Crossfa5eb232017-10-01 20:33:03 -070087 // don't build against the framework libraries (legacy-test, core-junit,
88 // ext, and framework for device targets)
89 No_framework_libs *bool
90
Colin Cross7d5136f2015-05-11 13:39:40 -070091 // list of module-specific flags that will be used for javac compiles
92 Javacflags []string `android:"arch_variant"`
93
Colin Cross7d5136f2015-05-11 13:39:40 -070094 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070095 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070096
97 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070098 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070099
100 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700101 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700102
Colin Cross540eff82017-06-22 17:01:52 -0700103 // if not blank, run jarjar using the specified rules file
104 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -0700105
106 // If not blank, set the java version passed to javac as -source and -target
107 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700108
109 // If set to false, don't allow this module to be installed. Defaults to true.
110 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700111
Colin Cross0f37af02017-09-27 17:42:05 -0700112 // If set to true, include sources used to compile the module in to the final jar
113 Include_srcs *bool
114
Colin Cross32f676a2017-09-06 13:41:06 -0700115 // List of modules to use as annotation processors
116 Annotation_processors []string
117
118 // List of classes to pass to javac to use as annotation processors
119 Annotation_processor_classes []string
Colin Cross540eff82017-06-22 17:01:52 -0700120}
121
Colin Cross89536d42017-07-07 14:35:50 -0700122type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700123 // list of module-specific flags that will be used for dex compiles
124 Dxflags []string `android:"arch_variant"`
125
Colin Cross7d5136f2015-05-11 13:39:40 -0700126 // if not blank, set to the version of the sdk to compile against
127 Sdk_version string
128
129 // Set for device java libraries, and for host versions of device java libraries
130 // built for testing
131 Dex bool `blueprint:"mutated"`
132
Colin Cross7d5136f2015-05-11 13:39:40 -0700133 // directories to pass to aidl tool
134 Aidl_includes []string
135
136 // directories that should be added as include directories
137 // for any aidl sources of modules that depend on this module
138 Export_aidl_include_dirs []string
139}
140
Colin Cross46c9b8b2017-06-22 16:51:17 -0700141// Module contains the properties and members used by all java module types
142type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700143 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700144 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700145
Colin Cross89536d42017-07-07 14:35:50 -0700146 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700147 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700148 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
Colin Crossb7a63242015-04-16 14:09:14 -0700156 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700157 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700158
Colin Cross635c3b02016-05-18 15:37:25 -0700159 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700160
Colin Cross635c3b02016-05-18 15:37:25 -0700161 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700162
Colin Crossb7a63242015-04-16 14:09:14 -0700163 // filelists of extra source files that should be included in the javac command line,
164 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700165 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700166
Colin Cross2fe66872015-03-30 17:20:39 -0700167 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700168 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700169}
170
Colin Crossf506d872017-07-19 15:53:04 -0700171type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700172 ClasspathFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700173 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700174}
175
Colin Cross89536d42017-07-07 14:35:50 -0700176func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
177 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
178 android.InitDefaultableModule(module)
179}
180
Colin Crossbe1da472017-07-07 15:59:46 -0700181type dependencyTag struct {
182 blueprint.BaseDependencyTag
183 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700184}
185
Colin Crossbe1da472017-07-07 15:59:46 -0700186var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700187 staticLibTag = dependencyTag{name: "staticlib"}
188 libTag = dependencyTag{name: "javalib"}
189 bootClasspathTag = dependencyTag{name: "bootclasspath"}
190 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossbe1da472017-07-07 15:59:46 -0700191)
Colin Cross2fe66872015-03-30 17:20:39 -0700192
Colin Crossfc3674a2017-09-18 17:41:52 -0700193type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700194 useModule, useFiles, useDefaultLibs, invalidVersion bool
195
196 module string
197 jar android.Path
198 aidl android.Path
Colin Crossfc3674a2017-09-18 17:41:52 -0700199}
200
201func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
202 switch v {
203 case "", "current", "system_current", "test_current":
204 // OK
205 default:
206 if _, err := strconv.Atoi(v); err != nil {
207 ctx.PropertyErrorf("sdk_version", "invalid sdk version")
208 return sdkDep{}
209 }
210 }
211
212 toFile := func(v string) sdkDep {
213 dir := filepath.Join("prebuilts/sdk", v)
214 jar := filepath.Join(dir, "android.jar")
215 aidl := filepath.Join(dir, "framework.aidl")
216 jarPath := android.ExistentPathForSource(ctx, "sdkdir", jar)
217 aidlPath := android.ExistentPathForSource(ctx, "sdkdir", aidl)
Colin Cross47ff2522017-10-02 14:22:08 -0700218
219 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.AConfig().AllowMissingDependencies() {
220 return sdkDep{
221 invalidVersion: true,
222 module: "sdk_v" + v,
223 }
224 }
225
Colin Crossfc3674a2017-09-18 17:41:52 -0700226 if !jarPath.Valid() {
227 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
228 return sdkDep{}
229 }
Colin Cross47ff2522017-10-02 14:22:08 -0700230
Colin Crossfc3674a2017-09-18 17:41:52 -0700231 if !aidlPath.Valid() {
232 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
233 return sdkDep{}
234 }
Colin Cross47ff2522017-10-02 14:22:08 -0700235
Colin Crossfc3674a2017-09-18 17:41:52 -0700236 return sdkDep{
237 useFiles: true,
238 jar: jarPath.Path(),
239 aidl: aidlPath.Path(),
240 }
241 }
242
243 toModule := func(m string) sdkDep {
244 return sdkDep{
245 useModule: true,
246 module: m,
247 }
248 }
249
Colin Cross8b9d37b2017-09-22 15:30:06 -0700250 if ctx.AConfig().UnbundledBuild() && v != "" {
Colin Crossfc3674a2017-09-18 17:41:52 -0700251 return toFile(v)
252 }
253
254 switch v {
255 case "":
256 return sdkDep{
257 useDefaultLibs: true,
258 }
259 case "current":
260 return toModule("android_stubs_current")
261 case "system_current":
262 return toModule("android_system_stubs_current")
263 case "test_current":
264 return toModule("android_test_stubs_current")
265 default:
266 return toFile(v)
267 }
268}
269
Colin Crossbe1da472017-07-07 15:59:46 -0700270func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700271 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700272 if ctx.Device() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700273 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
274 if sdkDep.useDefaultLibs {
Colin Crosscb2c9292017-09-23 19:57:16 -0700275 ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700276 if !proptools.Bool(j.properties.No_framework_libs) {
277 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
278 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700279 }
280 if sdkDep.useModule {
281 ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.module)
Colin Crossbe1da472017-07-07 15:59:46 -0700282 }
283 } else {
Colin Cross965714f2017-10-03 20:57:01 -0700284 // TODO(ccross): add hostdex support
Colin Cross2fe66872015-03-30 17:20:39 -0700285 }
286 }
Colin Crossf506d872017-07-19 15:53:04 -0700287 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
288 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross6ade34f2017-09-15 13:00:47 -0700289 ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700290
291 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700292 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross6af17aa2017-09-20 12:59:05 -0700293
294 if j.hasSrcExt(".proto") {
295 protoDeps(ctx, &j.protoProperties)
296 }
297}
298
299func hasSrcExt(srcs []string, ext string) bool {
300 for _, src := range srcs {
301 if filepath.Ext(src) == ext {
302 return true
303 }
304 }
305
306 return false
307}
308
309func (j *Module) hasSrcExt(ext string) bool {
310 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700311}
312
Colin Cross46c9b8b2017-06-22 16:51:17 -0700313func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700314 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700315
Colin Cross540eff82017-06-22 17:01:52 -0700316 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700317
318 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700319 if aidlPreprocess.Valid() {
320 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700321 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700322 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700323 }
324
Colin Cross635c3b02016-05-18 15:37:25 -0700325 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
326 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
327 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700328 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
329 flags = append(flags, "-I"+src.String())
330 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700331
Colin Crossf03c82b2015-04-13 13:53:40 -0700332 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700333}
334
Colin Cross32f676a2017-09-06 13:41:06 -0700335type deps struct {
Colin Cross6ade34f2017-09-15 13:00:47 -0700336 classpath android.Paths
337 bootClasspath android.Paths
338 staticJars android.Paths
339 staticJarResources android.Paths
340 aidlIncludeDirs android.Paths
341 srcFileLists android.Paths
342 aidlPreprocess android.OptionalPath
Colin Cross32f676a2017-09-06 13:41:06 -0700343}
Colin Cross2fe66872015-03-30 17:20:39 -0700344
Colin Cross32f676a2017-09-06 13:41:06 -0700345func (j *Module) collectDeps(ctx android.ModuleContext) deps {
346 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700347
348 sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
Colin Cross47ff2522017-10-02 14:22:08 -0700349 if sdkDep.invalidVersion {
350 ctx.AddMissingDependencies([]string{sdkDep.module})
351 } else if sdkDep.useFiles {
Colin Crossfc3674a2017-09-18 17:41:52 -0700352 deps.classpath = append(deps.classpath, sdkDep.jar)
353 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
354 }
355
Colin Cross2fe66872015-03-30 17:20:39 -0700356 ctx.VisitDirectDeps(func(module blueprint.Module) {
357 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700358 tag := ctx.OtherModuleDependencyTag(module)
359
Colin Crossf506d872017-07-19 15:53:04 -0700360 dep, _ := module.(Dependency)
361 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700362 switch tag {
363 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700364 // Nothing to do
Colin Crossec7a0422017-07-07 14:47:12 -0700365 default:
366 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700367 }
Colin Crossec7a0422017-07-07 14:47:12 -0700368 return
369 }
370
Colin Crossbe1da472017-07-07 15:59:46 -0700371 switch tag {
372 case bootClasspathTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700373 deps.bootClasspath = append(deps.bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700374 case libTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700375 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700376 case staticLibTag:
Colin Cross32f676a2017-09-06 13:41:06 -0700377 deps.classpath = append(deps.classpath, dep.ClasspathFiles()...)
378 deps.staticJars = append(deps.staticJars, dep.ClasspathFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700379 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700380 if ctx.ModuleName() == "framework" {
381 // framework.jar has a one-off dependency on the R.java and Manifest.java files
382 // generated by framework-res.apk
Colin Cross32f676a2017-09-06 13:41:06 -0700383 deps.srcFileLists = append(deps.srcFileLists, module.(*AndroidApp).aaptJavaFileList)
Colin Crossec7a0422017-07-07 14:47:12 -0700384 }
Colin Crossbe1da472017-07-07 15:59:46 -0700385 default:
386 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700387 }
Colin Crossbe1da472017-07-07 15:59:46 -0700388
Colin Cross32f676a2017-09-06 13:41:06 -0700389 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700390 })
391
Colin Cross32f676a2017-09-06 13:41:06 -0700392 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700393}
394
Colin Cross46c9b8b2017-06-22 16:51:17 -0700395func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700396
Colin Cross540eff82017-06-22 17:01:52 -0700397 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700398
Colin Cross32f676a2017-09-06 13:41:06 -0700399 deps := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700400
Colin Crossf03c82b2015-04-13 13:53:40 -0700401 var flags javaBuilderFlags
402
403 javacFlags := j.properties.Javacflags
Colin Cross4f26bc02017-09-06 12:52:16 -0700404 if ctx.AConfig().Getenv("EXPERIMENTAL_USE_OPENJDK9") == "" {
405 javacFlags = config.StripJavac9Flags(javacFlags)
406 }
Colin Cross64162712017-08-08 13:17:59 -0700407
408 if j.properties.Java_version != nil {
409 flags.javaVersion = *j.properties.Java_version
410 } else {
411 flags.javaVersion = "${config.DefaultJavaVersion}"
412 }
413
Colin Cross6ade34f2017-09-15 13:00:47 -0700414 flags.bootClasspath.AddPaths(deps.bootClasspath)
415 flags.classpath.AddPaths(deps.classpath)
416
Colin Crossf03c82b2015-04-13 13:53:40 -0700417 if len(javacFlags) > 0 {
418 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
419 flags.javacFlags = "$javacFlags"
420 }
421
Colin Cross32f676a2017-09-06 13:41:06 -0700422 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700423 if len(aidlFlags) > 0 {
424 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
425 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700426 }
427
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700428 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700429
Colin Cross6af17aa2017-09-20 12:59:05 -0700430 if hasSrcExt(srcFiles.Strings(), ".proto") {
431 flags = protoFlags(ctx, &j.protoProperties, flags)
432 }
433
434 var srcFileLists android.Paths
435
436 srcFiles, srcFileLists = j.genSources(ctx, srcFiles, flags)
437
438 srcFileLists = append(srcFileLists, deps.srcFileLists...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700439
Colin Cross0607cf72015-04-28 13:28:51 -0700440 ctx.VisitDirectDeps(func(module blueprint.Module) {
441 if gen, ok := module.(genrule.SourceFileGenerator); ok {
442 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
443 }
444 })
445
Colin Cross6af17aa2017-09-20 12:59:05 -0700446 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
Colin Crossb7a63242015-04-16 14:09:14 -0700447
Colin Cross0a6e0072017-08-30 14:24:55 -0700448 var jars android.Paths
449
Colin Cross8cf13342015-04-10 15:41:49 -0700450 if len(srcFiles) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -0700451 var extraJarDeps android.Paths
Colin Crossc6bbef32017-08-14 14:16:06 -0700452 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
453 // If error-prone is enabled, add an additional rule to compile the java files into
454 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -0700455 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -0700456 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
457 // enable error-prone without affecting the output class files.
Colin Cross6af17aa2017-09-20 12:59:05 -0700458 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -0700459 extraJarDeps = append(extraJarDeps, errorprone)
460 }
461
Colin Crossd6891432017-09-27 17:39:56 -0700462 // Compile java sources into .class files
Colin Cross6af17aa2017-09-20 12:59:05 -0700463 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, extraJarDeps)
Colin Crossd6891432017-09-27 17:39:56 -0700464 if ctx.Failed() {
465 return
466 }
467
Colin Cross0a6e0072017-08-30 14:24:55 -0700468 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700469 }
470
Colin Cross0f37af02017-09-27 17:42:05 -0700471 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
472 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
473
474 var resArgs []string
475 var resDeps android.Paths
476
477 resArgs = append(resArgs, dirArgs...)
478 resDeps = append(resDeps, dirDeps...)
479
480 resArgs = append(resArgs, fileArgs...)
481 resDeps = append(resDeps, fileDeps...)
482
483 if proptools.Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -0700484 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700485 resArgs = append(resArgs, srcArgs...)
486 resDeps = append(resDeps, srcDeps...)
487 }
Colin Cross40a36712017-09-27 17:41:35 -0700488
489 if len(resArgs) > 0 {
Colin Cross40a36712017-09-27 17:41:35 -0700490 resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700491 if ctx.Failed() {
492 return
493 }
Colin Cross20978302015-04-10 17:05:07 -0700494
Colin Cross0a6e0072017-08-30 14:24:55 -0700495 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700496 }
497
Colin Cross6ade34f2017-09-15 13:00:47 -0700498 // static classpath jars have the resources in them, so the resource jars aren't necessary here
Colin Cross32f676a2017-09-06 13:41:06 -0700499 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700500
Colin Cross635acc92017-09-12 22:50:46 -0700501 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
502
Colin Cross0a6e0072017-08-30 14:24:55 -0700503 // Combine the classes built from sources, any manifests, and any static libraries into
Colin Cross8649b262017-09-27 18:03:17 -0700504 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross635acc92017-09-12 22:50:46 -0700505 outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
Colin Cross0a6e0072017-08-30 14:24:55 -0700506
507 if j.properties.Jarjar_rules != nil {
508 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -0700509 // Transform classes.jar into classes-jarjar.jar
Colin Cross0a6e0072017-08-30 14:24:55 -0700510 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
511 if ctx.Failed() {
512 return
513 }
514 }
515
Colin Cross2fe66872015-03-30 17:20:39 -0700516 j.classpathFile = outputFile
517
Colin Crossa713a6f2017-09-20 18:04:44 -0700518 // TODO(ccross): handle hostdex
Colin Crossc157a8d2017-10-03 17:17:07 -0700519 if ctx.Device() && j.installable() {
Colin Cross540eff82017-06-22 17:01:52 -0700520 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700521 if false /* emma enabled */ {
522 // If you instrument class files that have local variable debug information in
523 // them emma does not correctly maintain the local variable table.
524 // This will cause an error when you try to convert the class files for Android.
525 // The workaround here is to build different dex file here based on emma switch
526 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
527 // option to remove local variable information
528 dxFlags = append(dxFlags, "--no-locals")
529 }
530
Colin Cross1332b002015-04-07 17:11:30 -0700531 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700532 dxFlags = append(dxFlags, "--no-optimize")
533 }
534
Colin Cross1332b002015-04-07 17:11:30 -0700535 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700536 dxFlags = append(dxFlags,
537 "--debug",
538 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700539 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700540 "--dump-width=1000")
541 }
542
Colin Cross595a4062017-08-31 12:30:37 -0700543 var minSdkVersion string
544 switch j.deviceProperties.Sdk_version {
545 case "", "current", "test_current", "system_current":
546 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
547 default:
548 minSdkVersion = j.deviceProperties.Sdk_version
549 }
550
551 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
552
Colin Cross2fe66872015-03-30 17:20:39 -0700553 flags.dxFlags = strings.Join(dxFlags, " ")
554
Colin Cross6ade34f2017-09-15 13:00:47 -0700555 desugarFlags := []string{
556 "--min_sdk_version " + minSdkVersion,
557 "--desugar_try_with_resources_if_needed=false",
558 "--allow_empty_bootclasspath",
559 }
560
561 if inList("--core-library", dxFlags) {
562 desugarFlags = append(desugarFlags, "--core_library")
563 }
564
565 flags.desugarFlags = strings.Join(desugarFlags, " ")
566
567 desugarJar := TransformDesugar(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700568 if ctx.Failed() {
569 return
570 }
571
Colin Cross7db5d632017-10-04 17:07:09 -0700572 // Compile classes.jar into classes.dex and then javalib.jar
573 outputFile = TransformClassesJarToDexJar(ctx, "javalib.jar", desugarJar, flags)
Colin Cross6ade34f2017-09-15 13:00:47 -0700574 if ctx.Failed() {
575 return
576 }
577
Colin Cross6ade34f2017-09-15 13:00:47 -0700578 j.dexJarFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700579 }
Colin Crossb7a63242015-04-16 14:09:14 -0700580 ctx.CheckbuildFile(outputFile)
581 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700582}
583
Colin Cross59f1bb62017-09-27 17:59:10 -0700584func (j *Module) installable() bool {
585 return j.properties.Installable == nil || *j.properties.Installable
586}
587
Colin Crossf506d872017-07-19 15:53:04 -0700588var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700589
Colin Cross74d73e22017-08-02 11:05:49 -0700590func (j *Module) ClasspathFiles() android.Paths {
591 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700592}
593
Colin Cross46c9b8b2017-06-22 16:51:17 -0700594func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700595 return j.exportAidlIncludeDirs
596}
597
Colin Cross46c9b8b2017-06-22 16:51:17 -0700598var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700599
Colin Cross46c9b8b2017-06-22 16:51:17 -0700600func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700601 return j.logtagsSrcs
602}
603
Colin Cross2fe66872015-03-30 17:20:39 -0700604//
605// Java libraries (.jar file)
606//
607
Colin Crossf506d872017-07-19 15:53:04 -0700608type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700609 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700610}
611
Colin Crossf506d872017-07-19 15:53:04 -0700612func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700613 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700614
Colin Cross59f1bb62017-09-27 17:59:10 -0700615 if j.installable() {
Colin Cross2c429dc2017-08-31 16:45:16 -0700616 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
617 ctx.ModuleName()+".jar", j.outputFile)
618 }
Colin Crossb7a63242015-04-16 14:09:14 -0700619}
620
Colin Crossf506d872017-07-19 15:53:04 -0700621func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700622 j.deps(ctx)
623}
624
Colin Crossa60ead82017-10-02 18:10:21 -0700625func LibraryFactory(installable bool) func() android.Module {
626 return func() android.Module {
627 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700628
Colin Crossa60ead82017-10-02 18:10:21 -0700629 if !installable {
630 module.properties.Installable = proptools.BoolPtr(false)
631 }
632 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700633
Colin Crossa60ead82017-10-02 18:10:21 -0700634 module.AddProperties(
635 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700636 &module.Module.deviceProperties,
637 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700638
Colin Crossa60ead82017-10-02 18:10:21 -0700639 InitJavaModule(module, android.HostAndDeviceSupported)
640 return module
641 }
Colin Cross2fe66872015-03-30 17:20:39 -0700642}
643
Colin Crossf506d872017-07-19 15:53:04 -0700644func LibraryHostFactory() android.Module {
645 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700646
Colin Cross6af17aa2017-09-20 12:59:05 -0700647 module.AddProperties(
648 &module.Module.properties,
649 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -0700650
Colin Cross89536d42017-07-07 14:35:50 -0700651 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700652 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700653}
654
655//
656// Java Binaries (.jar file plus wrapper script)
657//
658
Colin Crossf506d872017-07-19 15:53:04 -0700659type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700660 // installable script to execute the resulting jar
661 Wrapper string
662}
663
Colin Crossf506d872017-07-19 15:53:04 -0700664type Binary struct {
665 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700666
Colin Crossf506d872017-07-19 15:53:04 -0700667 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700668
669 wrapperFile android.ModuleSrcPath
670 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700671}
672
Colin Crossf506d872017-07-19 15:53:04 -0700673func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
674 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700675
676 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
677 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700678 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700679 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
680 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700681}
682
Colin Crossf506d872017-07-19 15:53:04 -0700683func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700684 j.deps(ctx)
685}
686
Colin Crossf506d872017-07-19 15:53:04 -0700687func BinaryFactory() android.Module {
688 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700689
Colin Cross540eff82017-06-22 17:01:52 -0700690 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700691
Colin Cross36242852017-06-23 15:06:31 -0700692 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700693 &module.Module.properties,
694 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700695 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700696 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700697
Colin Cross89536d42017-07-07 14:35:50 -0700698 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700699 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700700}
701
Colin Crossf506d872017-07-19 15:53:04 -0700702func BinaryHostFactory() android.Module {
703 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700704
Colin Cross36242852017-06-23 15:06:31 -0700705 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700706 &module.Module.properties,
707 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -0700708 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -0700709 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700710
Colin Cross89536d42017-07-07 14:35:50 -0700711 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700712 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700713}
714
715//
716// Java prebuilts
717//
718
Colin Cross74d73e22017-08-02 11:05:49 -0700719type ImportProperties struct {
720 Jars []string
721}
722
723type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700724 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700725 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700726
Colin Cross74d73e22017-08-02 11:05:49 -0700727 properties ImportProperties
728
Colin Cross0a6e0072017-08-30 14:24:55 -0700729 classpathFiles android.Paths
730 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700731}
732
Colin Cross74d73e22017-08-02 11:05:49 -0700733func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700734 return &j.prebuilt
735}
736
Colin Cross74d73e22017-08-02 11:05:49 -0700737func (j *Import) PrebuiltSrcs() []string {
738 return j.properties.Jars
739}
740
741func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700742 return j.prebuilt.Name(j.ModuleBase.Name())
743}
744
Colin Cross74d73e22017-08-02 11:05:49 -0700745func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700746}
747
Colin Cross74d73e22017-08-02 11:05:49 -0700748func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
749 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700750
Colin Cross635acc92017-09-12 22:50:46 -0700751 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles, android.OptionalPath{}, false)
Colin Cross2fe66872015-03-30 17:20:39 -0700752}
753
Colin Cross74d73e22017-08-02 11:05:49 -0700754var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700755
Colin Cross74d73e22017-08-02 11:05:49 -0700756func (j *Import) ClasspathFiles() android.Paths {
757 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700758}
759
Colin Cross74d73e22017-08-02 11:05:49 -0700760func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700761 return nil
762}
763
Colin Cross74d73e22017-08-02 11:05:49 -0700764var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700765
Colin Cross74d73e22017-08-02 11:05:49 -0700766func ImportFactory() android.Module {
767 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700768
Colin Cross74d73e22017-08-02 11:05:49 -0700769 module.AddProperties(&module.properties)
770
771 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700772 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
773 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700774}
775
Colin Cross74d73e22017-08-02 11:05:49 -0700776func ImportFactoryHost() android.Module {
777 module := &Import{}
778
779 module.AddProperties(&module.properties)
780
781 android.InitPrebuiltModule(module, &module.properties.Jars)
782 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
783 return module
784}
785
Colin Cross2fe66872015-03-30 17:20:39 -0700786func inList(s string, l []string) bool {
787 for _, e := range l {
788 if e == s {
789 return true
790 }
791 }
792 return false
793}
Colin Cross89536d42017-07-07 14:35:50 -0700794
795//
796// Defaults
797//
798type Defaults struct {
799 android.ModuleBase
800 android.DefaultsModuleBase
801}
802
803func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
804}
805
806func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
807}
808
809func defaultsFactory() android.Module {
810 return DefaultsFactory()
811}
812
813func DefaultsFactory(props ...interface{}) android.Module {
814 module := &Defaults{}
815
816 module.AddProperties(props...)
817 module.AddProperties(
818 &CompilerProperties{},
819 &CompilerDeviceProperties{},
820 )
821
822 android.InitDefaultsModule(module)
823
824 return module
825}