Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | // This file contains the module types for compiling Java for Android, and converts the properties |
| 18 | // into the flags and filenames necessary to pass to the compiler. The final creation of the rules |
| 19 | // is handled in builder.go |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | |
| 25 | "github.com/google/blueprint" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 26 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 27 | "android/soong" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 28 | "android/soong/common" |
Colin Cross | 0607cf7 | 2015-04-28 13:28:51 -0700 | [diff] [blame] | 29 | "android/soong/genrule" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 32 | func init() { |
| 33 | soong.RegisterModuleType("java_library", JavaLibraryFactory) |
| 34 | soong.RegisterModuleType("java_library_static", JavaLibraryFactory) |
| 35 | soong.RegisterModuleType("java_library_host", JavaLibraryHostFactory) |
| 36 | soong.RegisterModuleType("java_binary", JavaBinaryFactory) |
| 37 | soong.RegisterModuleType("java_binary_host", JavaBinaryHostFactory) |
| 38 | soong.RegisterModuleType("prebuilt_java_library", JavaPrebuiltFactory) |
| 39 | soong.RegisterModuleType("prebuilt_sdk", SdkPrebuiltFactory) |
| 40 | soong.RegisterModuleType("android_app", AndroidAppFactory) |
| 41 | |
| 42 | soong.RegisterSingletonType("logtags", LogtagsSingleton) |
| 43 | } |
| 44 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 45 | // TODO: |
| 46 | // Autogenerated files: |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 47 | // Proto |
| 48 | // Renderscript |
| 49 | // Post-jar passes: |
| 50 | // Proguard |
| 51 | // Emma |
| 52 | // Jarjar |
| 53 | // Dex |
| 54 | // Rmtypedefs |
| 55 | // Jack |
| 56 | // DroidDoc |
| 57 | // Findbugs |
| 58 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 59 | type javaBaseProperties struct { |
| 60 | // list of source files used to compile the Java module. May be .java, .logtags, .proto, |
| 61 | // or .aidl files. |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 62 | Srcs []string `android:"arch_variant"` |
| 63 | |
| 64 | // list of source files that should not be used to build the Java module. |
| 65 | // This is most useful in the arch/multilib variants to remove non-common files |
| 66 | Exclude_srcs []string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 67 | |
| 68 | // list of directories containing Java resources |
| 69 | Java_resource_dirs []string `android:"arch_variant"` |
| 70 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 71 | // list of directories that should be excluded from java_resource_dirs |
| 72 | Exclude_java_resource_dirs []string `android:"arch_variant"` |
| 73 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 74 | // don't build against the default libraries (core-libart, core-junit, |
| 75 | // ext, and framework for device targets) |
| 76 | No_standard_libraries bool |
| 77 | |
| 78 | // list of module-specific flags that will be used for javac compiles |
| 79 | Javacflags []string `android:"arch_variant"` |
| 80 | |
| 81 | // list of module-specific flags that will be used for jack compiles |
| 82 | Jack_flags []string `android:"arch_variant"` |
| 83 | |
| 84 | // list of module-specific flags that will be used for dex compiles |
| 85 | Dxflags []string `android:"arch_variant"` |
| 86 | |
| 87 | // list of of java libraries that will be in the classpath |
| 88 | Java_libs []string `android:"arch_variant"` |
| 89 | |
| 90 | // list of java libraries that will be compiled into the resulting jar |
| 91 | Java_static_libs []string `android:"arch_variant"` |
| 92 | |
| 93 | // manifest file to be included in resulting jar |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 94 | Manifest *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 95 | |
| 96 | // if not blank, set to the version of the sdk to compile against |
| 97 | Sdk_version string |
| 98 | |
| 99 | // Set for device java libraries, and for host versions of device java libraries |
| 100 | // built for testing |
| 101 | Dex bool `blueprint:"mutated"` |
| 102 | |
| 103 | // if not blank, run jarjar using the specified rules file |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 104 | Jarjar_rules *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 105 | |
| 106 | // directories to pass to aidl tool |
| 107 | Aidl_includes []string |
| 108 | |
| 109 | // directories that should be added as include directories |
| 110 | // for any aidl sources of modules that depend on this module |
| 111 | Export_aidl_include_dirs []string |
| 112 | } |
| 113 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 114 | // javaBase contains the properties and members used by all java module types, and implements |
| 115 | // the blueprint.Module interface. |
| 116 | type javaBase struct { |
| 117 | common.AndroidModuleBase |
| 118 | module JavaModuleType |
| 119 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 120 | properties javaBaseProperties |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 121 | |
| 122 | // output file suitable for inserting into the classpath of another compile |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 123 | classpathFile common.Path |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 124 | |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 125 | // output file suitable for installing or running |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 126 | outputFile common.Path |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 127 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 128 | // jarSpecs suitable for inserting classes from a static library into another jar |
| 129 | classJarSpecs []jarSpec |
| 130 | |
| 131 | // jarSpecs suitable for inserting resources from a static library into another jar |
| 132 | resourceJarSpecs []jarSpec |
| 133 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 134 | exportAidlIncludeDirs common.Paths |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 135 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 136 | logtagsSrcs common.Paths |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 137 | |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 138 | // filelists of extra source files that should be included in the javac command line, |
| 139 | // for example R.java generated by aapt for android apps |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 140 | ExtraSrcLists common.Paths |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 141 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 142 | // installed file for binary dependency |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 143 | installFile common.Path |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 146 | type AndroidJavaModuleContext common.AndroidBaseContext |
| 147 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 148 | type JavaModuleType interface { |
| 149 | GenerateJavaBuildActions(ctx common.AndroidModuleContext) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 150 | JavaDependencies(ctx AndroidJavaModuleContext) []string |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | type JavaDependency interface { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 154 | ClasspathFile() common.Path |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 155 | ClassJarSpecs() []jarSpec |
| 156 | ResourceJarSpecs() []jarSpec |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 157 | AidlIncludeDirs() common.Paths |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | func NewJavaBase(base *javaBase, module JavaModuleType, hod common.HostOrDeviceSupported, |
| 161 | props ...interface{}) (blueprint.Module, []interface{}) { |
| 162 | |
| 163 | base.module = module |
| 164 | |
| 165 | props = append(props, &base.properties) |
| 166 | |
| 167 | return common.InitAndroidArchModule(base, hod, common.MultilibCommon, props...) |
| 168 | } |
| 169 | |
| 170 | func (j *javaBase) BootClasspath(ctx common.AndroidBaseContext) string { |
| 171 | if ctx.Device() { |
| 172 | if j.properties.Sdk_version == "" { |
| 173 | return "core-libart" |
| 174 | } else if j.properties.Sdk_version == "current" { |
| 175 | // TODO: !TARGET_BUILD_APPS |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 176 | // TODO: export preprocessed framework.aidl from android_stubs_current |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 177 | return "android_stubs_current" |
| 178 | } else if j.properties.Sdk_version == "system_current" { |
| 179 | return "android_system_stubs_current" |
| 180 | } else { |
| 181 | return "sdk_v" + j.properties.Sdk_version |
| 182 | } |
| 183 | } else { |
| 184 | if j.properties.Dex { |
| 185 | return "core-libart" |
| 186 | } else { |
| 187 | return "" |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Colin Cross | efb9ebe | 2015-04-16 14:08:06 -0700 | [diff] [blame] | 192 | var defaultJavaLibraries = []string{"core-libart", "core-junit", "ext", "framework"} |
| 193 | |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 194 | func javaDepsMutator(ctx common.AndroidBottomUpMutatorContext) { |
| 195 | if j, ok := ctx.Module().(JavaModuleType); ok { |
| 196 | ctx.AddDependency(ctx.Module(), j.JavaDependencies(ctx)...) |
| 197 | } |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 200 | func (j *javaBase) JavaDependencies(ctx AndroidJavaModuleContext) []string { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 201 | var deps []string |
| 202 | |
| 203 | if !j.properties.No_standard_libraries { |
| 204 | bootClasspath := j.BootClasspath(ctx) |
| 205 | if bootClasspath != "" { |
| 206 | deps = append(deps, bootClasspath) |
| 207 | } |
Colin Cross | efb9ebe | 2015-04-16 14:08:06 -0700 | [diff] [blame] | 208 | if ctx.Device() && j.properties.Sdk_version == "" { |
| 209 | deps = append(deps, defaultJavaLibraries...) |
| 210 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 211 | } |
| 212 | deps = append(deps, j.properties.Java_libs...) |
| 213 | deps = append(deps, j.properties.Java_static_libs...) |
| 214 | |
| 215 | return deps |
| 216 | } |
| 217 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 218 | func (j *javaBase) aidlFlags(ctx common.AndroidModuleContext, aidlPreprocess common.OptionalPath, |
| 219 | aidlIncludeDirs common.Paths) []string { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 220 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 221 | localAidlIncludes := common.PathsForModuleSrc(ctx, j.properties.Aidl_includes) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 222 | |
| 223 | var flags []string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 224 | if aidlPreprocess.Valid() { |
| 225 | flags = append(flags, "-p"+aidlPreprocess.String()) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 226 | } else { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 227 | flags = append(flags, common.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I")) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 230 | flags = append(flags, common.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I")) |
| 231 | flags = append(flags, common.JoinWithPrefix(localAidlIncludes.Strings(), "-I")) |
| 232 | flags = append(flags, "-I"+common.PathForModuleSrc(ctx).String()) |
| 233 | flags = append(flags, "-I"+common.PathForModuleSrc(ctx, "src").String()) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 234 | |
Colin Cross | f03c82b | 2015-04-13 13:53:40 -0700 | [diff] [blame] | 235 | return flags |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 238 | func (j *javaBase) collectDeps(ctx common.AndroidModuleContext) (classpath common.Paths, |
| 239 | bootClasspath common.OptionalPath, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess common.OptionalPath, |
| 240 | aidlIncludeDirs common.Paths, srcFileLists common.Paths) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 241 | |
| 242 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 243 | otherName := ctx.OtherModuleName(module) |
| 244 | if javaDep, ok := module.(JavaDependency); ok { |
Colin Cross | 6cbb127 | 2015-04-08 11:23:01 -0700 | [diff] [blame] | 245 | if otherName == j.BootClasspath(ctx) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 246 | bootClasspath = common.OptionalPathForPath(javaDep.ClasspathFile()) |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 247 | } else if inList(otherName, defaultJavaLibraries) { |
| 248 | classpath = append(classpath, javaDep.ClasspathFile()) |
Colin Cross | 6cbb127 | 2015-04-08 11:23:01 -0700 | [diff] [blame] | 249 | } else if inList(otherName, j.properties.Java_libs) { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 250 | classpath = append(classpath, javaDep.ClasspathFile()) |
| 251 | } else if inList(otherName, j.properties.Java_static_libs) { |
| 252 | classpath = append(classpath, javaDep.ClasspathFile()) |
| 253 | classJarSpecs = append(classJarSpecs, javaDep.ClassJarSpecs()...) |
| 254 | resourceJarSpecs = append(resourceJarSpecs, javaDep.ResourceJarSpecs()...) |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 255 | } else if otherName == "framework-res" { |
| 256 | if ctx.ModuleName() == "framework" { |
| 257 | // framework.jar has a one-off dependency on the R.java and Manifest.java files |
| 258 | // generated by framework-res.apk |
| 259 | srcFileLists = append(srcFileLists, module.(*javaBase).module.(*AndroidApp).aaptJavaFileList) |
| 260 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 261 | } else { |
| 262 | panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName())) |
| 263 | } |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 264 | aidlIncludeDirs = append(aidlIncludeDirs, javaDep.AidlIncludeDirs()...) |
| 265 | if sdkDep, ok := module.(sdkDependency); ok { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 266 | if sdkDep.AidlPreprocessed().Valid() { |
| 267 | if aidlPreprocess.Valid() { |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 268 | ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q", |
| 269 | aidlPreprocess, sdkDep.AidlPreprocessed()) |
| 270 | } else { |
| 271 | aidlPreprocess = sdkDep.AidlPreprocessed() |
| 272 | } |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 275 | } |
| 276 | }) |
| 277 | |
Colin Cross | e7a9f3f | 2015-04-13 14:02:52 -0700 | [diff] [blame] | 278 | return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess, |
| 279 | aidlIncludeDirs, srcFileLists |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | func (j *javaBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 283 | j.module.GenerateJavaBuildActions(ctx) |
| 284 | } |
| 285 | |
| 286 | func (j *javaBase) GenerateJavaBuildActions(ctx common.AndroidModuleContext) { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 287 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 288 | j.exportAidlIncludeDirs = common.PathsForModuleSrc(ctx, j.properties.Export_aidl_include_dirs) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 289 | |
| 290 | classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess, |
Colin Cross | e7a9f3f | 2015-04-13 14:02:52 -0700 | [diff] [blame] | 291 | aidlIncludeDirs, srcFileLists := j.collectDeps(ctx) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 292 | |
Colin Cross | f03c82b | 2015-04-13 13:53:40 -0700 | [diff] [blame] | 293 | var flags javaBuilderFlags |
| 294 | |
| 295 | javacFlags := j.properties.Javacflags |
| 296 | if len(javacFlags) > 0 { |
| 297 | ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " ")) |
| 298 | flags.javacFlags = "$javacFlags" |
| 299 | } |
| 300 | |
| 301 | aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs) |
| 302 | if len(aidlFlags) > 0 { |
| 303 | ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " ")) |
| 304 | flags.aidlFlags = "$aidlFlags" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 307 | var javacDeps common.Paths |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 308 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 309 | if bootClasspath.Valid() { |
| 310 | flags.bootClasspath = "-bootclasspath " + bootClasspath.String() |
| 311 | javacDeps = append(javacDeps, bootClasspath.Path()) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if len(classpath) > 0 { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 315 | flags.classpath = "-classpath " + strings.Join(classpath.Strings(), ":") |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 316 | javacDeps = append(javacDeps, classpath...) |
| 317 | } |
| 318 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 319 | srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 320 | |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 321 | srcFiles = j.genSources(ctx, srcFiles, flags) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 322 | |
Colin Cross | 0607cf7 | 2015-04-28 13:28:51 -0700 | [diff] [blame] | 323 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 324 | if gen, ok := module.(genrule.SourceFileGenerator); ok { |
| 325 | srcFiles = append(srcFiles, gen.GeneratedSourceFiles()...) |
| 326 | } |
| 327 | }) |
| 328 | |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 329 | srcFileLists = append(srcFileLists, j.ExtraSrcLists...) |
| 330 | |
Colin Cross | 8cf1334 | 2015-04-10 15:41:49 -0700 | [diff] [blame] | 331 | if len(srcFiles) > 0 { |
| 332 | // Compile java sources into .class files |
Colin Cross | e7a9f3f | 2015-04-13 14:02:52 -0700 | [diff] [blame] | 333 | classes := TransformJavaToClasses(ctx, srcFiles, srcFileLists, flags, javacDeps) |
Colin Cross | 8cf1334 | 2015-04-10 15:41:49 -0700 | [diff] [blame] | 334 | if ctx.Failed() { |
| 335 | return |
| 336 | } |
| 337 | |
| 338 | classJarSpecs = append([]jarSpec{classes}, classJarSpecs...) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 341 | resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs), |
Colin Cross | 276284f | 2015-04-20 13:51:48 -0700 | [diff] [blame] | 342 | resourceJarSpecs...) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 343 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 344 | manifest := common.OptionalPathForModuleSrc(ctx, j.properties.Manifest) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 345 | |
| 346 | allJarSpecs := append([]jarSpec(nil), classJarSpecs...) |
| 347 | allJarSpecs = append(allJarSpecs, resourceJarSpecs...) |
| 348 | |
| 349 | // Combine classes + resources into classes-full-debug.jar |
| 350 | outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest) |
| 351 | if ctx.Failed() { |
| 352 | return |
| 353 | } |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 354 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 355 | if j.properties.Jarjar_rules != nil { |
| 356 | jarjar_rules := common.PathForModuleSrc(ctx, *j.properties.Jarjar_rules) |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 357 | // Transform classes-full-debug.jar into classes-jarjar.jar |
| 358 | outputFile = TransformJarJar(ctx, outputFile, jarjar_rules) |
| 359 | if ctx.Failed() { |
| 360 | return |
| 361 | } |
Colin Cross | 2097830 | 2015-04-10 17:05:07 -0700 | [diff] [blame] | 362 | |
| 363 | classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile) |
| 364 | classJarSpecs = []jarSpec{classes} |
Colin Cross | 65bf4f2 | 2015-04-03 16:54:17 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Colin Cross | 2097830 | 2015-04-10 17:05:07 -0700 | [diff] [blame] | 367 | j.resourceJarSpecs = resourceJarSpecs |
| 368 | j.classJarSpecs = classJarSpecs |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 369 | j.classpathFile = outputFile |
| 370 | |
Colin Cross | 8cf1334 | 2015-04-10 15:41:49 -0700 | [diff] [blame] | 371 | if j.properties.Dex && len(srcFiles) > 0 { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 372 | dxFlags := j.properties.Dxflags |
| 373 | if false /* emma enabled */ { |
| 374 | // If you instrument class files that have local variable debug information in |
| 375 | // them emma does not correctly maintain the local variable table. |
| 376 | // This will cause an error when you try to convert the class files for Android. |
| 377 | // The workaround here is to build different dex file here based on emma switch |
| 378 | // then later copy into classes.dex. When emma is on, dx is run with --no-locals |
| 379 | // option to remove local variable information |
| 380 | dxFlags = append(dxFlags, "--no-locals") |
| 381 | } |
| 382 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 383 | if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 384 | dxFlags = append(dxFlags, "--no-optimize") |
| 385 | } |
| 386 | |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 387 | if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 388 | dxFlags = append(dxFlags, |
| 389 | "--debug", |
| 390 | "--verbose", |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 391 | "--dump-to="+common.PathForModuleOut(ctx, "classes.lst").String(), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 392 | "--dump-width=1000") |
| 393 | } |
| 394 | |
| 395 | flags.dxFlags = strings.Join(dxFlags, " ") |
| 396 | |
| 397 | // Compile classes.jar into classes.dex |
Colin Cross | 6d1e72d | 2015-04-10 17:44:24 -0700 | [diff] [blame] | 398 | dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 399 | if ctx.Failed() { |
| 400 | return |
| 401 | } |
| 402 | |
| 403 | // Combine classes.dex + resources into javalib.jar |
Colin Cross | 6d1e72d | 2015-04-10 17:44:24 -0700 | [diff] [blame] | 404 | outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 405 | } |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 406 | ctx.CheckbuildFile(outputFile) |
| 407 | j.outputFile = outputFile |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | var _ JavaDependency = (*JavaLibrary)(nil) |
| 411 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 412 | func (j *javaBase) ClasspathFile() common.Path { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 413 | return j.classpathFile |
| 414 | } |
| 415 | |
| 416 | func (j *javaBase) ClassJarSpecs() []jarSpec { |
| 417 | return j.classJarSpecs |
| 418 | } |
| 419 | |
| 420 | func (j *javaBase) ResourceJarSpecs() []jarSpec { |
| 421 | return j.resourceJarSpecs |
| 422 | } |
| 423 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 424 | func (j *javaBase) AidlIncludeDirs() common.Paths { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 425 | return j.exportAidlIncludeDirs |
| 426 | } |
| 427 | |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 428 | var _ logtagsProducer = (*javaBase)(nil) |
| 429 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 430 | func (j *javaBase) logtags() common.Paths { |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 431 | return j.logtagsSrcs |
| 432 | } |
| 433 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 434 | // |
| 435 | // Java libraries (.jar file) |
| 436 | // |
| 437 | |
| 438 | type JavaLibrary struct { |
| 439 | javaBase |
| 440 | } |
| 441 | |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 442 | func (j *JavaLibrary) GenerateJavaBuildActions(ctx common.AndroidModuleContext) { |
| 443 | j.javaBase.GenerateJavaBuildActions(ctx) |
| 444 | |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame^] | 445 | j.installFile = ctx.InstallFileName(common.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.outputFile) |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 448 | func JavaLibraryFactory() (blueprint.Module, []interface{}) { |
| 449 | module := &JavaLibrary{} |
| 450 | |
| 451 | module.properties.Dex = true |
| 452 | |
| 453 | return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported) |
| 454 | } |
| 455 | |
| 456 | func JavaLibraryHostFactory() (blueprint.Module, []interface{}) { |
| 457 | module := &JavaLibrary{} |
| 458 | |
| 459 | return NewJavaBase(&module.javaBase, module, common.HostSupported) |
| 460 | } |
| 461 | |
| 462 | // |
| 463 | // Java Binaries (.jar file plus wrapper script) |
| 464 | // |
| 465 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 466 | type javaBinaryProperties struct { |
| 467 | // installable script to execute the resulting jar |
| 468 | Wrapper string |
| 469 | } |
| 470 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 471 | type JavaBinary struct { |
| 472 | JavaLibrary |
| 473 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 474 | binaryProperties javaBinaryProperties |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | func (j *JavaBinary) GenerateJavaBuildActions(ctx common.AndroidModuleContext) { |
| 478 | j.JavaLibrary.GenerateJavaBuildActions(ctx) |
| 479 | |
| 480 | // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by |
| 481 | // another build rule before the jar has been installed. |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame^] | 482 | ctx.InstallFile(common.PathForModuleInstall(ctx, "bin"), common.PathForModuleSrc(ctx, j.binaryProperties.Wrapper), |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 483 | j.installFile) |
| 484 | } |
| 485 | |
| 486 | func JavaBinaryFactory() (blueprint.Module, []interface{}) { |
| 487 | module := &JavaBinary{} |
| 488 | |
| 489 | module.properties.Dex = true |
| 490 | |
| 491 | return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported, &module.binaryProperties) |
| 492 | } |
| 493 | |
| 494 | func JavaBinaryHostFactory() (blueprint.Module, []interface{}) { |
| 495 | module := &JavaBinary{} |
| 496 | |
| 497 | return NewJavaBase(&module.javaBase, module, common.HostSupported, &module.binaryProperties) |
| 498 | } |
| 499 | |
| 500 | // |
| 501 | // Java prebuilts |
| 502 | // |
| 503 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 504 | type javaPrebuiltProperties struct { |
| 505 | Srcs []string |
| 506 | } |
| 507 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 508 | type JavaPrebuilt struct { |
| 509 | common.AndroidModuleBase |
| 510 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 511 | properties javaPrebuiltProperties |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 512 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 513 | classpathFile common.Path |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 514 | classJarSpecs, resourceJarSpecs []jarSpec |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 518 | if len(j.properties.Srcs) != 1 { |
| 519 | ctx.ModuleErrorf("expected exactly one jar in srcs") |
| 520 | return |
| 521 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 522 | prebuilt := common.PathForModuleSrc(ctx, j.properties.Srcs[0]) |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 523 | |
| 524 | classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt) |
| 525 | |
| 526 | j.classpathFile = prebuilt |
| 527 | j.classJarSpecs = []jarSpec{classJarSpec} |
| 528 | j.resourceJarSpecs = []jarSpec{resourceJarSpec} |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame^] | 529 | ctx.InstallFileName(common.PathForModuleInstall(ctx, "framework"), ctx.ModuleName()+".jar", j.classpathFile) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | var _ JavaDependency = (*JavaPrebuilt)(nil) |
| 533 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 534 | func (j *JavaPrebuilt) ClasspathFile() common.Path { |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 535 | return j.classpathFile |
| 536 | } |
| 537 | |
| 538 | func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec { |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 539 | return j.classJarSpecs |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec { |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 543 | return j.resourceJarSpecs |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 546 | func (j *JavaPrebuilt) AidlIncludeDirs() common.Paths { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 547 | return nil |
| 548 | } |
| 549 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 550 | func JavaPrebuiltFactory() (blueprint.Module, []interface{}) { |
| 551 | module := &JavaPrebuilt{} |
| 552 | |
| 553 | return common.InitAndroidArchModule(module, common.HostAndDeviceSupported, |
| 554 | common.MultilibCommon, &module.properties) |
| 555 | } |
| 556 | |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 557 | // |
| 558 | // SDK java prebuilts (.jar containing resources plus framework.aidl) |
| 559 | // |
| 560 | |
| 561 | type sdkDependency interface { |
| 562 | JavaDependency |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 563 | AidlPreprocessed() common.OptionalPath |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | var _ sdkDependency = (*sdkPrebuilt)(nil) |
| 567 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 568 | type sdkPrebuiltProperties struct { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 569 | Aidl_preprocessed *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 572 | type sdkPrebuilt struct { |
| 573 | JavaPrebuilt |
| 574 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 575 | sdkProperties sdkPrebuiltProperties |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 576 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 577 | aidlPreprocessed common.OptionalPath |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 581 | j.JavaPrebuilt.GenerateAndroidBuildActions(ctx) |
| 582 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 583 | j.aidlPreprocessed = common.OptionalPathForModuleSrc(ctx, j.sdkProperties.Aidl_preprocessed) |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 586 | func (j *sdkPrebuilt) AidlPreprocessed() common.OptionalPath { |
Colin Cross | aa8630b | 2015-04-13 13:52:22 -0700 | [diff] [blame] | 587 | return j.aidlPreprocessed |
| 588 | } |
| 589 | |
| 590 | func SdkPrebuiltFactory() (blueprint.Module, []interface{}) { |
| 591 | module := &sdkPrebuilt{} |
| 592 | |
| 593 | return common.InitAndroidArchModule(module, common.HostAndDeviceSupported, |
| 594 | common.MultilibCommon, &module.properties, &module.sdkProperties) |
| 595 | } |
| 596 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 597 | func inList(s string, l []string) bool { |
| 598 | for _, e := range l { |
| 599 | if e == s { |
| 600 | return true |
| 601 | } |
| 602 | } |
| 603 | return false |
| 604 | } |