blob: b76c2a4edef2947c92208ca5b177f557a6e12558 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
22 "fmt"
Colin Cross74d73e22017-08-02 11:05:49 -070023 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Cross0607cf72015-04-28 13:28:51 -070030 "android/soong/genrule"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross2fe66872015-03-30 17:20:39 -070032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Cross89536d42017-07-07 14:35:50 -070035 android.RegisterModuleType("java_defaults", defaultsFactory)
36
Colin Crossf506d872017-07-19 15:53:04 -070037 android.RegisterModuleType("java_library", LibraryFactory)
38 android.RegisterModuleType("java_library_static", LibraryFactory)
39 android.RegisterModuleType("java_library_host", LibraryHostFactory)
40 android.RegisterModuleType("java_binary", BinaryFactory)
41 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070042 android.RegisterModuleType("java_import", ImportFactory)
43 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Crosse8dc34a2017-07-19 11:22:16 -070044 android.RegisterModuleType("android_prebuilt_sdk", SdkPrebuiltFactory)
Colin Cross798bfce2016-10-12 14:28:16 -070045 android.RegisterModuleType("android_app", AndroidAppFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070046
Colin Cross798bfce2016-10-12 14:28:16 -070047 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070048}
49
Colin Cross2fe66872015-03-30 17:20:39 -070050// TODO:
51// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070052// Proto
53// Renderscript
54// Post-jar passes:
55// Proguard
Colin Crossba211132017-06-22 15:36:39 -070056// Jacoco
Colin Cross2fe66872015-03-30 17:20:39 -070057// Jarjar
58// Dex
59// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070060// DroidDoc
61// Findbugs
62
Colin Cross89536d42017-07-07 14:35:50 -070063type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070064 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
65 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070066 Srcs []string `android:"arch_variant"`
67
68 // list of source files that should not be used to build the Java module.
69 // This is most useful in the arch/multilib variants to remove non-common files
70 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
72 // list of directories containing Java resources
Colin Crosse8dc34a2017-07-19 11:22:16 -070073 Resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070074
Colin Crosse8dc34a2017-07-19 11:22:16 -070075 // list of directories that should be excluded from resource_dirs
76 Exclude_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070077
Paul Duffin2b67e3b2016-11-30 16:13:09 +000078 // don't build against the default libraries (legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070079 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070080 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070081
82 // list of module-specific flags that will be used for javac compiles
83 Javacflags []string `android:"arch_variant"`
84
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070086 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070089 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070090
91 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -070093
Colin Cross540eff82017-06-22 17:01:52 -070094 // if not blank, run jarjar using the specified rules file
95 Jarjar_rules *string
Colin Cross64162712017-08-08 13:17:59 -070096
97 // If not blank, set the java version passed to javac as -source and -target
98 Java_version *string
Colin 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 Cross540eff82017-06-22 17:01:52 -0700102}
103
Colin Cross89536d42017-07-07 14:35:50 -0700104type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700105 // list of module-specific flags that will be used for dex compiles
106 Dxflags []string `android:"arch_variant"`
107
Colin Cross7d5136f2015-05-11 13:39:40 -0700108 // if not blank, set to the version of the sdk to compile against
109 Sdk_version string
110
111 // Set for device java libraries, and for host versions of device java libraries
112 // built for testing
113 Dex bool `blueprint:"mutated"`
114
Colin Cross7d5136f2015-05-11 13:39:40 -0700115 // directories to pass to aidl tool
116 Aidl_includes []string
117
118 // directories that should be added as include directories
119 // for any aidl sources of modules that depend on this module
120 Export_aidl_include_dirs []string
121}
122
Colin Cross46c9b8b2017-06-22 16:51:17 -0700123// Module contains the properties and members used by all java module types
124type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700125 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700126 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700127
Colin Cross89536d42017-07-07 14:35:50 -0700128 properties CompilerProperties
129 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700130
131 // output file suitable for inserting into the classpath of another compile
Colin Cross635c3b02016-05-18 15:37:25 -0700132 classpathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700133
Colin Crossb7a63242015-04-16 14:09:14 -0700134 // output file suitable for installing or running
Colin Cross635c3b02016-05-18 15:37:25 -0700135 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700136
Colin Cross635c3b02016-05-18 15:37:25 -0700137 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700138
Colin Cross635c3b02016-05-18 15:37:25 -0700139 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700140
Colin Crossb7a63242015-04-16 14:09:14 -0700141 // filelists of extra source files that should be included in the javac command line,
142 // for example R.java generated by aapt for android apps
Colin Cross635c3b02016-05-18 15:37:25 -0700143 ExtraSrcLists android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700144
Colin Cross2fe66872015-03-30 17:20:39 -0700145 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700146 installFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700147}
148
Colin Crossf506d872017-07-19 15:53:04 -0700149type Dependency interface {
Colin Cross74d73e22017-08-02 11:05:49 -0700150 ClasspathFiles() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700151 AidlIncludeDirs() android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700152}
153
Colin Cross89536d42017-07-07 14:35:50 -0700154func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
155 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
156 android.InitDefaultableModule(module)
157}
158
Colin Crossbe1da472017-07-07 15:59:46 -0700159type dependencyTag struct {
160 blueprint.BaseDependencyTag
161 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700162}
163
Colin Crossbe1da472017-07-07 15:59:46 -0700164var (
Colin Crossf506d872017-07-19 15:53:04 -0700165 staticLibTag = dependencyTag{name: "staticlib"}
166 libTag = dependencyTag{name: "javalib"}
Colin Crossbe1da472017-07-07 15:59:46 -0700167 bootClasspathTag = dependencyTag{name: "bootclasspath"}
168 frameworkResTag = dependencyTag{name: "framework-res"}
169 sdkDependencyTag = dependencyTag{name: "sdk"}
170)
Colin Cross2fe66872015-03-30 17:20:39 -0700171
Colin Crossbe1da472017-07-07 15:59:46 -0700172func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross76b5f0c2017-08-29 16:02:06 -0700173 if !proptools.Bool(j.properties.No_standard_libs) {
Colin Crossbe1da472017-07-07 15:59:46 -0700174 if ctx.Device() {
175 switch j.deviceProperties.Sdk_version {
176 case "":
Colin Cross227d4362017-08-30 14:14:52 -0700177 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
178 ctx.AddDependency(ctx.Module(), libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700179 case "current":
180 // TODO: !TARGET_BUILD_APPS
181 // TODO: export preprocessed framework.aidl from android_stubs_current
182 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
Colin Cross227d4362017-08-30 14:14:52 -0700183 case "test_current":
184 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_test_stubs_current")
Colin Crossbe1da472017-07-07 15:59:46 -0700185 case "system_current":
186 ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
187 default:
188 ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
189 }
190 } else {
191 if j.deviceProperties.Dex {
Colin Cross227d4362017-08-30 14:14:52 -0700192 ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-oj", "core-libart")
Colin Crossbe1da472017-07-07 15:59:46 -0700193 }
Colin Cross2fe66872015-03-30 17:20:39 -0700194 }
195 }
Colin Crossf506d872017-07-19 15:53:04 -0700196 ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
197 ctx.AddDependency(ctx.Module(), staticLibTag, j.properties.Static_libs...)
Colin Cross7f9036c2017-08-30 13:27:57 -0700198
199 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Colin Cross2fe66872015-03-30 17:20:39 -0700200}
201
Colin Cross46c9b8b2017-06-22 16:51:17 -0700202func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700203 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700204
Colin Cross540eff82017-06-22 17:01:52 -0700205 localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
Colin Crossc0b06f12015-04-08 13:03:43 -0700206
207 var flags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208 if aidlPreprocess.Valid() {
209 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700210 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700211 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700212 }
213
Colin Cross635c3b02016-05-18 15:37:25 -0700214 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
215 flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
216 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Crossd48633a2017-07-13 14:41:17 -0700217 if src := android.ExistentPathForSource(ctx, "", "src"); src.Valid() {
218 flags = append(flags, "-I"+src.String())
219 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700220
Colin Crossf03c82b2015-04-13 13:53:40 -0700221 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700222}
223
Colin Cross0a6e0072017-08-30 14:24:55 -0700224func (j *Module) collectDeps(ctx android.ModuleContext) (classpath, bootClasspath, staticJars,
225 aidlIncludeDirs, srcFileLists android.Paths, aidlPreprocess android.OptionalPath) {
Colin Cross2fe66872015-03-30 17:20:39 -0700226
227 ctx.VisitDirectDeps(func(module blueprint.Module) {
228 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700229 tag := ctx.OtherModuleDependencyTag(module)
230
Colin Crossf506d872017-07-19 15:53:04 -0700231 dep, _ := module.(Dependency)
232 if dep == nil {
Colin Crossec7a0422017-07-07 14:47:12 -0700233 switch tag {
234 case android.DefaultsDepTag, android.SourceDepTag:
235 default:
236 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700237 }
Colin Crossec7a0422017-07-07 14:47:12 -0700238 return
239 }
240
Colin Crossbe1da472017-07-07 15:59:46 -0700241 switch tag {
242 case bootClasspathTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700243 bootClasspath = append(bootClasspath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700244 case libTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700245 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Crossf506d872017-07-19 15:53:04 -0700246 case staticLibTag:
Colin Cross74d73e22017-08-02 11:05:49 -0700247 classpath = append(classpath, dep.ClasspathFiles()...)
Colin Cross0a6e0072017-08-30 14:24:55 -0700248 staticJars = append(staticJars, dep.ClasspathFiles()...)
Colin Crossbe1da472017-07-07 15:59:46 -0700249 case frameworkResTag:
Colin Crossec7a0422017-07-07 14:47:12 -0700250 if ctx.ModuleName() == "framework" {
251 // framework.jar has a one-off dependency on the R.java and Manifest.java files
252 // generated by framework-res.apk
253 srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
254 }
Colin Crossbe1da472017-07-07 15:59:46 -0700255 case sdkDependencyTag:
256 sdkDep := module.(sdkDependency)
Colin Cross227d4362017-08-30 14:14:52 -0700257 bootClasspath = append(bootClasspath, sdkDep.ClasspathFiles()...)
Colin Crossec7a0422017-07-07 14:47:12 -0700258 if sdkDep.AidlPreprocessed().Valid() {
259 if aidlPreprocess.Valid() {
260 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
261 aidlPreprocess, sdkDep.AidlPreprocessed())
262 } else {
263 aidlPreprocess = sdkDep.AidlPreprocessed()
Colin Crossc0b06f12015-04-08 13:03:43 -0700264 }
265 }
Colin Crossbe1da472017-07-07 15:59:46 -0700266 default:
267 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
Colin Cross2fe66872015-03-30 17:20:39 -0700268 }
Colin Crossbe1da472017-07-07 15:59:46 -0700269
Colin Crossf506d872017-07-19 15:53:04 -0700270 aidlIncludeDirs = append(aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700271 })
272
Colin Cross0a6e0072017-08-30 14:24:55 -0700273 return
Colin Cross2fe66872015-03-30 17:20:39 -0700274}
275
Colin Cross46c9b8b2017-06-22 16:51:17 -0700276func (j *Module) compile(ctx android.ModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700277
Colin Cross540eff82017-06-22 17:01:52 -0700278 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700279
Colin Cross0a6e0072017-08-30 14:24:55 -0700280 classpath, bootClasspath, staticJars, aidlIncludeDirs, srcFileLists,
281 aidlPreprocess := j.collectDeps(ctx)
Colin Crossc0b06f12015-04-08 13:03:43 -0700282
Colin Crossf03c82b2015-04-13 13:53:40 -0700283 var flags javaBuilderFlags
284
285 javacFlags := j.properties.Javacflags
Colin Cross64162712017-08-08 13:17:59 -0700286
287 if j.properties.Java_version != nil {
288 flags.javaVersion = *j.properties.Java_version
289 } else {
290 flags.javaVersion = "${config.DefaultJavaVersion}"
291 }
292
Colin Crossf03c82b2015-04-13 13:53:40 -0700293 if len(javacFlags) > 0 {
294 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
295 flags.javacFlags = "$javacFlags"
296 }
297
298 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
299 if len(aidlFlags) > 0 {
300 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
301 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700302 }
303
Colin Crossf506d872017-07-19 15:53:04 -0700304 var deps android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700305
Colin Cross74d73e22017-08-02 11:05:49 -0700306 if len(bootClasspath) > 0 {
307 flags.bootClasspath = "-bootclasspath " + strings.Join(bootClasspath.Strings(), ":")
308 deps = append(deps, bootClasspath...)
Colin Cross227d4362017-08-30 14:14:52 -0700309 } else if ctx.Device() {
310 // Explicitly clear the bootclasspath for device builds
311 flags.bootClasspath = `-bootclasspath ""`
Colin Cross2fe66872015-03-30 17:20:39 -0700312 }
313
314 if len(classpath) > 0 {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700315 flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":")
Colin Crossf506d872017-07-19 15:53:04 -0700316 deps = append(deps, classpath...)
Colin Cross2fe66872015-03-30 17:20:39 -0700317 }
318
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700319 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Crossc0b06f12015-04-08 13:03:43 -0700320
Colin Crossf05fe972015-04-10 17:45:20 -0700321 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700322
Colin Cross0607cf72015-04-28 13:28:51 -0700323 ctx.VisitDirectDeps(func(module blueprint.Module) {
324 if gen, ok := module.(genrule.SourceFileGenerator); ok {
325 srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...)
326 }
327 })
328
Colin Crossb7a63242015-04-16 14:09:14 -0700329 srcFileLists = append(srcFileLists, j.ExtraSrcLists...)
330
Colin Crossc6bbef32017-08-14 14:16:06 -0700331 var extraJarDeps android.Paths
332
Colin Cross0a6e0072017-08-30 14:24:55 -0700333 var jars android.Paths
334
Colin Cross8cf13342015-04-10 15:41:49 -0700335 if len(srcFiles) > 0 {
336 // Compile java sources into .class files
Colin Crossf506d872017-07-19 15:53:04 -0700337 classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, deps)
Colin Cross8cf13342015-04-10 15:41:49 -0700338 if ctx.Failed() {
339 return
340 }
341
Colin Crossc6bbef32017-08-14 14:16:06 -0700342 if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
343 // If error-prone is enabled, add an additional rule to compile the java files into
344 // a separate set of classes (so that they don't overwrite the normal ones and require
345 // a rebuild when error-prone is turned off). Add the classes as a dependency to
346 // the jar command so the two compiles can run in parallel.
347 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
348 // enable error-prone without affecting the output class files.
349 errorprone := RunErrorProne(ctx, srcFiles, srcFileLists, flags, deps)
350 extraJarDeps = append(extraJarDeps, errorprone)
351 }
352
Colin Cross0a6e0072017-08-30 14:24:55 -0700353 jars = append(jars, classes)
Colin Cross2fe66872015-03-30 17:20:39 -0700354 }
355
Colin Cross0a6e0072017-08-30 14:24:55 -0700356 resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs, j.properties.Exclude_resource_dirs)
Colin Cross635c3b02016-05-18 15:37:25 -0700357 manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
Colin Cross2fe66872015-03-30 17:20:39 -0700358
Colin Cross0a6e0072017-08-30 14:24:55 -0700359 if len(resourceJarSpecs) > 0 || manifest.Valid() {
360 // Combine classes + resources into classes-full-debug.jar
361 resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, manifest, extraJarDeps)
Colin Cross65bf4f22015-04-03 16:54:17 -0700362 if ctx.Failed() {
363 return
364 }
Colin Cross20978302015-04-10 17:05:07 -0700365
Colin Cross0a6e0072017-08-30 14:24:55 -0700366 jars = append(jars, resourceJar)
Colin Cross65bf4f22015-04-03 16:54:17 -0700367 }
368
Colin Cross0a6e0072017-08-30 14:24:55 -0700369 jars = append(jars, staticJars...)
370
371 // Combine the classes built from sources, any manifests, and any static libraries into
372 // classes-combined.jar. If there is only one input jar this step will be skipped.
373 outputFile := TransformJarsToJar(ctx, "classes-combined.jar", jars)
374
375 if j.properties.Jarjar_rules != nil {
376 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
377 // Transform classes-combined.jar into classes-jarjar.jar
378 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
379 if ctx.Failed() {
380 return
381 }
382 }
383
Colin Cross2fe66872015-03-30 17:20:39 -0700384 j.classpathFile = outputFile
385
Colin Cross540eff82017-06-22 17:01:52 -0700386 if j.deviceProperties.Dex && len(srcFiles) > 0 {
387 dxFlags := j.deviceProperties.Dxflags
Colin Cross2fe66872015-03-30 17:20:39 -0700388 if false /* emma enabled */ {
389 // If you instrument class files that have local variable debug information in
390 // them emma does not correctly maintain the local variable table.
391 // This will cause an error when you try to convert the class files for Android.
392 // The workaround here is to build different dex file here based on emma switch
393 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
394 // option to remove local variable information
395 dxFlags = append(dxFlags, "--no-locals")
396 }
397
Colin Cross1332b002015-04-07 17:11:30 -0700398 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700399 dxFlags = append(dxFlags, "--no-optimize")
400 }
401
Colin Cross1332b002015-04-07 17:11:30 -0700402 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700403 dxFlags = append(dxFlags,
404 "--debug",
405 "--verbose",
Colin Cross635c3b02016-05-18 15:37:25 -0700406 "--dump-to="+android.PathForModuleOut(ctx, "classes.lst").String(),
Colin Cross2fe66872015-03-30 17:20:39 -0700407 "--dump-width=1000")
408 }
409
Colin Cross595a4062017-08-31 12:30:37 -0700410 var minSdkVersion string
411 switch j.deviceProperties.Sdk_version {
412 case "", "current", "test_current", "system_current":
413 minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
414 default:
415 minSdkVersion = j.deviceProperties.Sdk_version
416 }
417
418 dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
419
Colin Cross2fe66872015-03-30 17:20:39 -0700420 flags.dxFlags = strings.Join(dxFlags, " ")
421
422 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700423 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700424 if ctx.Failed() {
425 return
426 }
427
428 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700429 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700430 }
Colin Crossb7a63242015-04-16 14:09:14 -0700431 ctx.CheckbuildFile(outputFile)
432 j.outputFile = outputFile
Colin Cross2fe66872015-03-30 17:20:39 -0700433}
434
Colin Crossf506d872017-07-19 15:53:04 -0700435var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700436
Colin Cross74d73e22017-08-02 11:05:49 -0700437func (j *Module) ClasspathFiles() android.Paths {
438 return android.Paths{j.classpathFile}
Colin Cross2fe66872015-03-30 17:20:39 -0700439}
440
Colin Cross46c9b8b2017-06-22 16:51:17 -0700441func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700442 return j.exportAidlIncludeDirs
443}
444
Colin Cross46c9b8b2017-06-22 16:51:17 -0700445var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -0700446
Colin Cross46c9b8b2017-06-22 16:51:17 -0700447func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -0700448 return j.logtagsSrcs
449}
450
Colin Cross2fe66872015-03-30 17:20:39 -0700451//
452// Java libraries (.jar file)
453//
454
Colin Crossf506d872017-07-19 15:53:04 -0700455type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700456 Module
Colin Cross2fe66872015-03-30 17:20:39 -0700457}
458
Colin Crossf506d872017-07-19 15:53:04 -0700459func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700460 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -0700461
Colin Cross2c429dc2017-08-31 16:45:16 -0700462 if j.properties.Installable == nil || *j.properties.Installable == true {
463 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
464 ctx.ModuleName()+".jar", j.outputFile)
465 }
Colin Crossb7a63242015-04-16 14:09:14 -0700466}
467
Colin Crossf506d872017-07-19 15:53:04 -0700468func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700469 j.deps(ctx)
470}
471
Colin Crossf506d872017-07-19 15:53:04 -0700472func LibraryFactory() android.Module {
473 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700474
Colin Cross540eff82017-06-22 17:01:52 -0700475 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700476
Colin Cross36242852017-06-23 15:06:31 -0700477 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700478 &module.Module.properties,
479 &module.Module.deviceProperties)
Colin Cross36242852017-06-23 15:06:31 -0700480
Colin Cross89536d42017-07-07 14:35:50 -0700481 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700482 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700483}
484
Colin Crossf506d872017-07-19 15:53:04 -0700485func LibraryHostFactory() android.Module {
486 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700487
Colin Cross36242852017-06-23 15:06:31 -0700488 module.AddProperties(&module.Module.properties)
489
Colin Cross89536d42017-07-07 14:35:50 -0700490 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700491 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700492}
493
494//
495// Java Binaries (.jar file plus wrapper script)
496//
497
Colin Crossf506d872017-07-19 15:53:04 -0700498type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700499 // installable script to execute the resulting jar
500 Wrapper string
501}
502
Colin Crossf506d872017-07-19 15:53:04 -0700503type Binary struct {
504 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700505
Colin Crossf506d872017-07-19 15:53:04 -0700506 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700507
508 wrapperFile android.ModuleSrcPath
509 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -0700510}
511
Colin Crossf506d872017-07-19 15:53:04 -0700512func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
513 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross2fe66872015-03-30 17:20:39 -0700514
515 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
516 // another build rule before the jar has been installed.
Colin Cross10a03492017-08-10 17:09:43 -0700517 j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper)
Colin Cross5c517922017-08-31 12:29:17 -0700518 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
519 ctx.ModuleName(), j.wrapperFile, j.installFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700520}
521
Colin Crossf506d872017-07-19 15:53:04 -0700522func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700523 j.deps(ctx)
524}
525
Colin Crossf506d872017-07-19 15:53:04 -0700526func BinaryFactory() android.Module {
527 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700528
Colin Cross540eff82017-06-22 17:01:52 -0700529 module.deviceProperties.Dex = true
Colin Cross2fe66872015-03-30 17:20:39 -0700530
Colin Cross36242852017-06-23 15:06:31 -0700531 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700532 &module.Module.properties,
533 &module.Module.deviceProperties,
534 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700535
Colin Cross89536d42017-07-07 14:35:50 -0700536 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -0700537 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700538}
539
Colin Crossf506d872017-07-19 15:53:04 -0700540func BinaryHostFactory() android.Module {
541 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -0700542
Colin Cross36242852017-06-23 15:06:31 -0700543 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700544 &module.Module.properties,
545 &module.Module.deviceProperties,
546 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -0700547
Colin Cross89536d42017-07-07 14:35:50 -0700548 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700549 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700550}
551
552//
553// Java prebuilts
554//
555
Colin Cross74d73e22017-08-02 11:05:49 -0700556type ImportProperties struct {
557 Jars []string
558}
559
560type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700561 android.ModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -0700562 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -0700563
Colin Cross74d73e22017-08-02 11:05:49 -0700564 properties ImportProperties
565
Colin Cross0a6e0072017-08-30 14:24:55 -0700566 classpathFiles android.Paths
567 combinedClasspathFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700568}
569
Colin Cross74d73e22017-08-02 11:05:49 -0700570func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -0700571 return &j.prebuilt
572}
573
Colin Cross74d73e22017-08-02 11:05:49 -0700574func (j *Import) PrebuiltSrcs() []string {
575 return j.properties.Jars
576}
577
578func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -0700579 return j.prebuilt.Name(j.ModuleBase.Name())
580}
581
Colin Cross74d73e22017-08-02 11:05:49 -0700582func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross1e676be2016-10-12 14:38:15 -0700583}
584
Colin Cross74d73e22017-08-02 11:05:49 -0700585func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
586 j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -0700587
Colin Cross0a6e0072017-08-30 14:24:55 -0700588 j.combinedClasspathFile = TransformJarsToJar(ctx, "classes.jar", j.classpathFiles)
Colin Cross2fe66872015-03-30 17:20:39 -0700589}
590
Colin Cross74d73e22017-08-02 11:05:49 -0700591var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700592
Colin Cross74d73e22017-08-02 11:05:49 -0700593func (j *Import) ClasspathFiles() android.Paths {
594 return j.classpathFiles
Colin Cross2fe66872015-03-30 17:20:39 -0700595}
596
Colin Cross74d73e22017-08-02 11:05:49 -0700597func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700598 return nil
599}
600
Colin Cross74d73e22017-08-02 11:05:49 -0700601var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -0700602
Colin Cross74d73e22017-08-02 11:05:49 -0700603func ImportFactory() android.Module {
604 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -0700605
Colin Cross74d73e22017-08-02 11:05:49 -0700606 module.AddProperties(&module.properties)
607
608 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700609 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
610 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700611}
612
Colin Cross74d73e22017-08-02 11:05:49 -0700613func ImportFactoryHost() android.Module {
614 module := &Import{}
615
616 module.AddProperties(&module.properties)
617
618 android.InitPrebuiltModule(module, &module.properties.Jars)
619 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommon)
620 return module
621}
622
Colin Crossaa8630b2015-04-13 13:52:22 -0700623//
624// SDK java prebuilts (.jar containing resources plus framework.aidl)
625//
626
627type sdkDependency interface {
Colin Crossf506d872017-07-19 15:53:04 -0700628 Dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700629 AidlPreprocessed() android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700630}
631
632var _ sdkDependency = (*sdkPrebuilt)(nil)
633
Colin Cross7d5136f2015-05-11 13:39:40 -0700634type sdkPrebuiltProperties struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700635 Aidl_preprocessed *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700636}
637
Colin Crossaa8630b2015-04-13 13:52:22 -0700638type sdkPrebuilt struct {
Colin Cross74d73e22017-08-02 11:05:49 -0700639 Import
Colin Crossaa8630b2015-04-13 13:52:22 -0700640
Colin Cross7d5136f2015-05-11 13:39:40 -0700641 sdkProperties sdkPrebuiltProperties
Colin Crossaa8630b2015-04-13 13:52:22 -0700642
Colin Cross635c3b02016-05-18 15:37:25 -0700643 aidlPreprocessed android.OptionalPath
Colin Crossaa8630b2015-04-13 13:52:22 -0700644}
645
Colin Cross635c3b02016-05-18 15:37:25 -0700646func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross74d73e22017-08-02 11:05:49 -0700647 j.Import.GenerateAndroidBuildActions(ctx)
Colin Crossaa8630b2015-04-13 13:52:22 -0700648
Colin Cross635c3b02016-05-18 15:37:25 -0700649 j.aidlPreprocessed = android.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed)
Colin Crossaa8630b2015-04-13 13:52:22 -0700650}
651
Colin Cross635c3b02016-05-18 15:37:25 -0700652func (j *sdkPrebuilt) AidlPreprocessed() android.OptionalPath {
Colin Crossaa8630b2015-04-13 13:52:22 -0700653 return j.aidlPreprocessed
654}
655
Colin Cross36242852017-06-23 15:06:31 -0700656func SdkPrebuiltFactory() android.Module {
Colin Crossaa8630b2015-04-13 13:52:22 -0700657 module := &sdkPrebuilt{}
658
Colin Cross74d73e22017-08-02 11:05:49 -0700659 module.AddProperties(&module.sdkProperties)
Colin Cross36242852017-06-23 15:06:31 -0700660
Colin Cross74d73e22017-08-02 11:05:49 -0700661 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross36242852017-06-23 15:06:31 -0700662 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
663 return module
Colin Crossaa8630b2015-04-13 13:52:22 -0700664}
665
Colin Cross2fe66872015-03-30 17:20:39 -0700666func inList(s string, l []string) bool {
667 for _, e := range l {
668 if e == s {
669 return true
670 }
671 }
672 return false
673}
Colin Cross89536d42017-07-07 14:35:50 -0700674
675//
676// Defaults
677//
678type Defaults struct {
679 android.ModuleBase
680 android.DefaultsModuleBase
681}
682
683func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
684}
685
686func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
687}
688
689func defaultsFactory() android.Module {
690 return DefaultsFactory()
691}
692
693func DefaultsFactory(props ...interface{}) android.Module {
694 module := &Defaults{}
695
696 module.AddProperties(props...)
697 module.AddProperties(
698 &CompilerProperties{},
699 &CompilerDeviceProperties{},
700 )
701
702 android.InitDefaultsModule(module)
703
704 return module
705}