blob: bbec5b56e56d2d11711e91382d725838b296aab7 [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
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
21import (
22 "fmt"
23 "path/filepath"
24 "strings"
25
26 "github.com/google/blueprint"
Colin Crossc0b06f12015-04-08 13:03:43 -070027 "github.com/google/blueprint/pathtools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
29 "android/soong/common"
30)
31
Colin Cross2fe66872015-03-30 17:20:39 -070032// TODO:
33// Autogenerated files:
34// AIDL
35// Proto
36// Renderscript
37// Post-jar passes:
38// Proguard
39// Emma
40// Jarjar
41// Dex
42// Rmtypedefs
43// Jack
44// DroidDoc
45// Findbugs
46
47// javaBase contains the properties and members used by all java module types, and implements
48// the blueprint.Module interface.
49type javaBase struct {
50 common.AndroidModuleBase
51 module JavaModuleType
52
53 properties struct {
54 // srcs: list of source files used to compile the Java module. May be .java, .logtags, .proto,
55 // or .aidl files.
56 Srcs []string `android:"arch_variant,arch_subtract"`
57
58 // resource_dirs: list of directories containing resources
59 Resource_dirs []string `android:"arch_variant"`
60
61 // no_standard_libraries: don't build against the default libraries (core-libart, core-junit,
62 // ext, and framework for device targets)
63 No_standard_libraries bool
64
65 // javacflags: list of module-specific flags that will be used for javac compiles
66 Javacflags []string `android:"arch_variant"`
67
68 // dxflags: list of module-specific flags that will be used for dex compiles
69 Dxflags []string `android:"arch_variant"`
70
71 // java_libs: list of of java libraries that will be in the classpath
72 Java_libs []string `android:"arch_variant"`
73
74 // java_static_libs: list of java libraries that will be compiled into the resulting jar
75 Java_static_libs []string `android:"arch_variant"`
76
77 // manifest: manifest file to be included in resulting jar
78 Manifest string
79
80 // sdk_version: if not blank, set to the version of the sdk to compile against
81 Sdk_version string
82
83 // Set for device java libraries, and for host versions of device java libraries
84 // built for testing
85 Dex bool `blueprint:"mutated"`
Colin Cross65bf4f22015-04-03 16:54:17 -070086
87 // jarjar_rules: if not blank, run jarjar using the specified rules file
88 Jarjar_rules string
Colin Crossc0b06f12015-04-08 13:03:43 -070089
90 // aidl_includes: directories to pass to aidl tool
91 Aidl_includes []string
92
93 // aidl_export_include_dirs: directories that should be added as include directories
94 // for any aidl sources of modules that depend on this module
95 Export_aidl_include_dirs []string
Colin Cross2fe66872015-03-30 17:20:39 -070096 }
97
98 // output file suitable for inserting into the classpath of another compile
99 classpathFile string
100
101 // jarSpecs suitable for inserting classes from a static library into another jar
102 classJarSpecs []jarSpec
103
104 // jarSpecs suitable for inserting resources from a static library into another jar
105 resourceJarSpecs []jarSpec
106
Colin Crossc0b06f12015-04-08 13:03:43 -0700107 exportAidlIncludeDirs []string
108
Colin Crossf05fe972015-04-10 17:45:20 -0700109 logtagsSrcs []string
110
Colin Cross2fe66872015-03-30 17:20:39 -0700111 // installed file for binary dependency
112 installFile string
113}
114
115type JavaModuleType interface {
116 GenerateJavaBuildActions(ctx common.AndroidModuleContext)
117}
118
119type JavaDependency interface {
120 ClasspathFile() string
121 ClassJarSpecs() []jarSpec
122 ResourceJarSpecs() []jarSpec
Colin Crossc0b06f12015-04-08 13:03:43 -0700123 AidlIncludeDirs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700124}
125
126func NewJavaBase(base *javaBase, module JavaModuleType, hod common.HostOrDeviceSupported,
127 props ...interface{}) (blueprint.Module, []interface{}) {
128
129 base.module = module
130
131 props = append(props, &base.properties)
132
133 return common.InitAndroidArchModule(base, hod, common.MultilibCommon, props...)
134}
135
136func (j *javaBase) BootClasspath(ctx common.AndroidBaseContext) string {
137 if ctx.Device() {
138 if j.properties.Sdk_version == "" {
139 return "core-libart"
140 } else if j.properties.Sdk_version == "current" {
141 // TODO: !TARGET_BUILD_APPS
Colin Crossc0b06f12015-04-08 13:03:43 -0700142 // TODO: export preprocessed framework.aidl from android_stubs_current
Colin Cross2fe66872015-03-30 17:20:39 -0700143 return "android_stubs_current"
144 } else if j.properties.Sdk_version == "system_current" {
145 return "android_system_stubs_current"
146 } else {
147 return "sdk_v" + j.properties.Sdk_version
148 }
149 } else {
150 if j.properties.Dex {
151 return "core-libart"
152 } else {
153 return ""
154 }
155 }
156}
157
158func (j *javaBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
159 var deps []string
160
161 if !j.properties.No_standard_libraries {
162 bootClasspath := j.BootClasspath(ctx)
163 if bootClasspath != "" {
164 deps = append(deps, bootClasspath)
165 }
166 }
167 deps = append(deps, j.properties.Java_libs...)
168 deps = append(deps, j.properties.Java_static_libs...)
169
170 return deps
171}
172
Colin Crossc0b06f12015-04-08 13:03:43 -0700173func (j *javaBase) aidlFlags(ctx common.AndroidModuleContext, aidlPreprocess string,
174 aidlIncludeDirs []string) string {
175
176 localAidlIncludes := pathtools.PrefixPaths(j.properties.Aidl_includes, common.ModuleSrcDir(ctx))
177
178 var flags []string
179 if aidlPreprocess != "" {
180 flags = append(flags, "-p"+aidlPreprocess)
181 } else {
182 flags = append(flags, common.JoinWithPrefix(aidlIncludeDirs, "-I"))
183 }
184
185 flags = append(flags, common.JoinWithPrefix(j.exportAidlIncludeDirs, "-I"))
186 flags = append(flags, common.JoinWithPrefix(localAidlIncludes, "-I"))
187 flags = append(flags, "-I"+common.ModuleSrcDir(ctx))
188 flags = append(flags, "-I"+filepath.Join(common.ModuleSrcDir(ctx), "src"))
189
190 return strings.Join(flags, " ")
191}
192
Colin Cross2fe66872015-03-30 17:20:39 -0700193func (j *javaBase) collectDeps(ctx common.AndroidModuleContext) (classpath []string,
Colin Crossc0b06f12015-04-08 13:03:43 -0700194 bootClasspath string, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess string,
195 aidlIncludeDirs []string) {
Colin Cross2fe66872015-03-30 17:20:39 -0700196
197 ctx.VisitDirectDeps(func(module blueprint.Module) {
198 otherName := ctx.OtherModuleName(module)
199 if javaDep, ok := module.(JavaDependency); ok {
Colin Cross6cbb1272015-04-08 11:23:01 -0700200 if otherName == j.BootClasspath(ctx) {
201 bootClasspath = javaDep.ClasspathFile()
202 } else if inList(otherName, j.properties.Java_libs) {
Colin Cross2fe66872015-03-30 17:20:39 -0700203 classpath = append(classpath, javaDep.ClasspathFile())
204 } else if inList(otherName, j.properties.Java_static_libs) {
205 classpath = append(classpath, javaDep.ClasspathFile())
206 classJarSpecs = append(classJarSpecs, javaDep.ClassJarSpecs()...)
207 resourceJarSpecs = append(resourceJarSpecs, javaDep.ResourceJarSpecs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700208 } else {
209 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
210 }
Colin Crossaa8630b2015-04-13 13:52:22 -0700211 aidlIncludeDirs = append(aidlIncludeDirs, javaDep.AidlIncludeDirs()...)
212 if sdkDep, ok := module.(sdkDependency); ok {
213 if sdkDep.AidlPreprocessed() != "" {
214 if aidlPreprocess != "" {
215 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
216 aidlPreprocess, sdkDep.AidlPreprocessed())
217 } else {
218 aidlPreprocess = sdkDep.AidlPreprocessed()
219 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700220 }
221 }
Colin Cross2fe66872015-03-30 17:20:39 -0700222 } else {
223 ctx.ModuleErrorf("unknown dependency module type for %q", otherName)
224 }
225 })
226
Colin Crossc0b06f12015-04-08 13:03:43 -0700227 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess, aidlIncludeDirs
Colin Cross2fe66872015-03-30 17:20:39 -0700228}
229
230func (j *javaBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
231 j.module.GenerateJavaBuildActions(ctx)
232}
233
234func (j *javaBase) GenerateJavaBuildActions(ctx common.AndroidModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700235
236 j.exportAidlIncludeDirs = pathtools.PrefixPaths(j.properties.Export_aidl_include_dirs,
237 common.ModuleSrcDir(ctx))
238
239 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
240 aidlIncludeDirs := j.collectDeps(ctx)
241
Colin Cross2fe66872015-03-30 17:20:39 -0700242 flags := javaBuilderFlags{
243 javacFlags: strings.Join(j.properties.Javacflags, " "),
Colin Crossc0b06f12015-04-08 13:03:43 -0700244 aidlFlags: j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs),
Colin Cross2fe66872015-03-30 17:20:39 -0700245 }
246
247 var javacDeps []string
248
Colin Cross2fe66872015-03-30 17:20:39 -0700249 if bootClasspath != "" {
250 flags.bootClasspath = "-bootclasspath " + bootClasspath
251 javacDeps = append(javacDeps, bootClasspath)
252 }
253
254 if len(classpath) > 0 {
255 flags.classpath = "-classpath " + strings.Join(classpath, ":")
256 javacDeps = append(javacDeps, classpath...)
257 }
258
Colin Crossc0b06f12015-04-08 13:03:43 -0700259 srcFiles := common.ExpandSources(ctx, j.properties.Srcs)
260
Colin Crossf05fe972015-04-10 17:45:20 -0700261 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700262
Colin Cross8cf13342015-04-10 15:41:49 -0700263 if len(srcFiles) > 0 {
264 // Compile java sources into .class files
265 classes := TransformJavaToClasses(ctx, srcFiles, flags, javacDeps)
266 if ctx.Failed() {
267 return
268 }
269
270 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700271 }
272
273 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs), resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700274
275 manifest := j.properties.Manifest
276 if manifest != "" {
277 manifest = filepath.Join(common.ModuleSrcDir(ctx), manifest)
278 }
279
280 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
281 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
282
283 // Combine classes + resources into classes-full-debug.jar
284 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
285 if ctx.Failed() {
286 return
287 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700288
Colin Cross65bf4f22015-04-03 16:54:17 -0700289 if j.properties.Jarjar_rules != "" {
290 jarjar_rules := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Jarjar_rules)
291 // Transform classes-full-debug.jar into classes-jarjar.jar
292 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
293 if ctx.Failed() {
294 return
295 }
Colin Cross20978302015-04-10 17:05:07 -0700296
297 classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile)
298 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700299 }
300
Colin Cross20978302015-04-10 17:05:07 -0700301 j.resourceJarSpecs = resourceJarSpecs
302 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700303 j.classpathFile = outputFile
304
Colin Cross8cf13342015-04-10 15:41:49 -0700305 if j.properties.Dex && len(srcFiles) > 0 {
Colin Cross2fe66872015-03-30 17:20:39 -0700306 dxFlags := j.properties.Dxflags
307 if false /* emma enabled */ {
308 // If you instrument class files that have local variable debug information in
309 // them emma does not correctly maintain the local variable table.
310 // This will cause an error when you try to convert the class files for Android.
311 // The workaround here is to build different dex file here based on emma switch
312 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
313 // option to remove local variable information
314 dxFlags = append(dxFlags, "--no-locals")
315 }
316
Colin Cross1332b002015-04-07 17:11:30 -0700317 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700318 dxFlags = append(dxFlags, "--no-optimize")
319 }
320
Colin Cross1332b002015-04-07 17:11:30 -0700321 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700322 dxFlags = append(dxFlags,
323 "--debug",
324 "--verbose",
325 "--dump-to="+filepath.Join(common.ModuleOutDir(ctx), "classes.lst"),
326 "--dump-width=1000")
327 }
328
329 flags.dxFlags = strings.Join(dxFlags, " ")
330
331 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700332 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700333 if ctx.Failed() {
334 return
335 }
336
337 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700338 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700339 }
340
341 j.installFile = ctx.InstallFileName("framework", ctx.ModuleName()+".jar", outputFile)
342}
343
344var _ JavaDependency = (*JavaLibrary)(nil)
345
346func (j *javaBase) ClasspathFile() string {
347 return j.classpathFile
348}
349
350func (j *javaBase) ClassJarSpecs() []jarSpec {
351 return j.classJarSpecs
352}
353
354func (j *javaBase) ResourceJarSpecs() []jarSpec {
355 return j.resourceJarSpecs
356}
357
Colin Crossc0b06f12015-04-08 13:03:43 -0700358func (j *javaBase) AidlIncludeDirs() []string {
359 return j.exportAidlIncludeDirs
360}
361
Colin Crossf05fe972015-04-10 17:45:20 -0700362var _ logtagsProducer = (*javaBase)(nil)
363
364func (j *javaBase) logtags() []string {
365 return j.logtagsSrcs
366}
367
Colin Cross2fe66872015-03-30 17:20:39 -0700368//
369// Java libraries (.jar file)
370//
371
372type JavaLibrary struct {
373 javaBase
374}
375
376func JavaLibraryFactory() (blueprint.Module, []interface{}) {
377 module := &JavaLibrary{}
378
379 module.properties.Dex = true
380
381 return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported)
382}
383
384func JavaLibraryHostFactory() (blueprint.Module, []interface{}) {
385 module := &JavaLibrary{}
386
387 return NewJavaBase(&module.javaBase, module, common.HostSupported)
388}
389
390//
391// Java Binaries (.jar file plus wrapper script)
392//
393
394type JavaBinary struct {
395 JavaLibrary
396
397 binaryProperties struct {
398 // wrapper: installable script to execute the resulting jar
399 Wrapper string
400 }
401}
402
403func (j *JavaBinary) GenerateJavaBuildActions(ctx common.AndroidModuleContext) {
404 j.JavaLibrary.GenerateJavaBuildActions(ctx)
405
406 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
407 // another build rule before the jar has been installed.
408 ctx.InstallFile("bin", filepath.Join(common.ModuleSrcDir(ctx), j.binaryProperties.Wrapper),
409 j.installFile)
410}
411
412func JavaBinaryFactory() (blueprint.Module, []interface{}) {
413 module := &JavaBinary{}
414
415 module.properties.Dex = true
416
417 return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported, &module.binaryProperties)
418}
419
420func JavaBinaryHostFactory() (blueprint.Module, []interface{}) {
421 module := &JavaBinary{}
422
423 return NewJavaBase(&module.javaBase, module, common.HostSupported, &module.binaryProperties)
424}
425
426//
427// Java prebuilts
428//
429
430type JavaPrebuilt struct {
431 common.AndroidModuleBase
432
433 properties struct {
Colin Crossaa8630b2015-04-13 13:52:22 -0700434 Srcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700435 }
436
Colin Crosse1d62a82015-04-03 16:53:05 -0700437 classpathFile string
438 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700439}
440
441func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
442 if len(j.properties.Srcs) != 1 {
443 ctx.ModuleErrorf("expected exactly one jar in srcs")
444 return
445 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700446 prebuilt := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Srcs[0])
447
448 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
449
450 j.classpathFile = prebuilt
451 j.classJarSpecs = []jarSpec{classJarSpec}
452 j.resourceJarSpecs = []jarSpec{resourceJarSpec}
Colin Crosse1d62a82015-04-03 16:53:05 -0700453 ctx.InstallFileName("framework", ctx.ModuleName()+".jar", j.classpathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700454}
455
456var _ JavaDependency = (*JavaPrebuilt)(nil)
457
458func (j *JavaPrebuilt) ClasspathFile() string {
459 return j.classpathFile
460}
461
462func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700463 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700464}
465
466func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700467 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700468}
469
Colin Crossc0b06f12015-04-08 13:03:43 -0700470func (j *JavaPrebuilt) AidlIncludeDirs() []string {
471 return nil
472}
473
Colin Cross2fe66872015-03-30 17:20:39 -0700474func JavaPrebuiltFactory() (blueprint.Module, []interface{}) {
475 module := &JavaPrebuilt{}
476
477 return common.InitAndroidArchModule(module, common.HostAndDeviceSupported,
478 common.MultilibCommon, &module.properties)
479}
480
Colin Crossaa8630b2015-04-13 13:52:22 -0700481//
482// SDK java prebuilts (.jar containing resources plus framework.aidl)
483//
484
485type sdkDependency interface {
486 JavaDependency
487 AidlPreprocessed() string
488}
489
490var _ sdkDependency = (*sdkPrebuilt)(nil)
491
492type sdkPrebuilt struct {
493 JavaPrebuilt
494
495 sdkProperties struct {
496 Aidl_preprocessed string
497 }
498
499 aidlPreprocessed string
500}
501
502func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
503 j.JavaPrebuilt.GenerateAndroidBuildActions(ctx)
504
505 if j.sdkProperties.Aidl_preprocessed != "" {
506 j.aidlPreprocessed = filepath.Join(common.ModuleSrcDir(ctx), j.sdkProperties.Aidl_preprocessed)
507 }
508}
509
510func (j *sdkPrebuilt) AidlPreprocessed() string {
511 return j.aidlPreprocessed
512}
513
514func SdkPrebuiltFactory() (blueprint.Module, []interface{}) {
515 module := &sdkPrebuilt{}
516
517 return common.InitAndroidArchModule(module, common.HostAndDeviceSupported,
518 common.MultilibCommon, &module.properties, &module.sdkProperties)
519}
520
Colin Cross2fe66872015-03-30 17:20:39 -0700521func inList(s string, l []string) bool {
522 for _, e := range l {
523 if e == s {
524 return true
525 }
526 }
527 return false
528}