blob: 533cabbda3785104589dd80f6a4acd0b8a809ffb [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,
Colin Crossf03c82b2015-04-13 13:53:40 -0700174 aidlIncludeDirs []string) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700175
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
Colin Crossf03c82b2015-04-13 13:53:40 -0700190 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700191}
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 Crossf03c82b2015-04-13 13:53:40 -0700242 var flags javaBuilderFlags
243
244 javacFlags := j.properties.Javacflags
245 if len(javacFlags) > 0 {
246 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
247 flags.javacFlags = "$javacFlags"
248 }
249
250 aidlFlags := j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs)
251 if len(aidlFlags) > 0 {
252 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
253 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700254 }
255
256 var javacDeps []string
257
Colin Cross2fe66872015-03-30 17:20:39 -0700258 if bootClasspath != "" {
259 flags.bootClasspath = "-bootclasspath " + bootClasspath
260 javacDeps = append(javacDeps, bootClasspath)
261 }
262
263 if len(classpath) > 0 {
264 flags.classpath = "-classpath " + strings.Join(classpath, ":")
265 javacDeps = append(javacDeps, classpath...)
266 }
267
Colin Crossc0b06f12015-04-08 13:03:43 -0700268 srcFiles := common.ExpandSources(ctx, j.properties.Srcs)
269
Colin Crossf05fe972015-04-10 17:45:20 -0700270 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700271
Colin Cross8cf13342015-04-10 15:41:49 -0700272 if len(srcFiles) > 0 {
273 // Compile java sources into .class files
274 classes := TransformJavaToClasses(ctx, srcFiles, flags, javacDeps)
275 if ctx.Failed() {
276 return
277 }
278
279 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700280 }
281
282 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs), resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700283
284 manifest := j.properties.Manifest
285 if manifest != "" {
286 manifest = filepath.Join(common.ModuleSrcDir(ctx), manifest)
287 }
288
289 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
290 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
291
292 // Combine classes + resources into classes-full-debug.jar
293 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
294 if ctx.Failed() {
295 return
296 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700297
Colin Cross65bf4f22015-04-03 16:54:17 -0700298 if j.properties.Jarjar_rules != "" {
299 jarjar_rules := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Jarjar_rules)
300 // Transform classes-full-debug.jar into classes-jarjar.jar
301 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
302 if ctx.Failed() {
303 return
304 }
Colin Cross20978302015-04-10 17:05:07 -0700305
306 classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile)
307 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700308 }
309
Colin Cross20978302015-04-10 17:05:07 -0700310 j.resourceJarSpecs = resourceJarSpecs
311 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700312 j.classpathFile = outputFile
313
Colin Cross8cf13342015-04-10 15:41:49 -0700314 if j.properties.Dex && len(srcFiles) > 0 {
Colin Cross2fe66872015-03-30 17:20:39 -0700315 dxFlags := j.properties.Dxflags
316 if false /* emma enabled */ {
317 // If you instrument class files that have local variable debug information in
318 // them emma does not correctly maintain the local variable table.
319 // This will cause an error when you try to convert the class files for Android.
320 // The workaround here is to build different dex file here based on emma switch
321 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
322 // option to remove local variable information
323 dxFlags = append(dxFlags, "--no-locals")
324 }
325
Colin Cross1332b002015-04-07 17:11:30 -0700326 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700327 dxFlags = append(dxFlags, "--no-optimize")
328 }
329
Colin Cross1332b002015-04-07 17:11:30 -0700330 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700331 dxFlags = append(dxFlags,
332 "--debug",
333 "--verbose",
334 "--dump-to="+filepath.Join(common.ModuleOutDir(ctx), "classes.lst"),
335 "--dump-width=1000")
336 }
337
338 flags.dxFlags = strings.Join(dxFlags, " ")
339
340 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700341 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700342 if ctx.Failed() {
343 return
344 }
345
346 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700347 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700348 }
349
350 j.installFile = ctx.InstallFileName("framework", ctx.ModuleName()+".jar", outputFile)
351}
352
353var _ JavaDependency = (*JavaLibrary)(nil)
354
355func (j *javaBase) ClasspathFile() string {
356 return j.classpathFile
357}
358
359func (j *javaBase) ClassJarSpecs() []jarSpec {
360 return j.classJarSpecs
361}
362
363func (j *javaBase) ResourceJarSpecs() []jarSpec {
364 return j.resourceJarSpecs
365}
366
Colin Crossc0b06f12015-04-08 13:03:43 -0700367func (j *javaBase) AidlIncludeDirs() []string {
368 return j.exportAidlIncludeDirs
369}
370
Colin Crossf05fe972015-04-10 17:45:20 -0700371var _ logtagsProducer = (*javaBase)(nil)
372
373func (j *javaBase) logtags() []string {
374 return j.logtagsSrcs
375}
376
Colin Cross2fe66872015-03-30 17:20:39 -0700377//
378// Java libraries (.jar file)
379//
380
381type JavaLibrary struct {
382 javaBase
383}
384
385func JavaLibraryFactory() (blueprint.Module, []interface{}) {
386 module := &JavaLibrary{}
387
388 module.properties.Dex = true
389
390 return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported)
391}
392
393func JavaLibraryHostFactory() (blueprint.Module, []interface{}) {
394 module := &JavaLibrary{}
395
396 return NewJavaBase(&module.javaBase, module, common.HostSupported)
397}
398
399//
400// Java Binaries (.jar file plus wrapper script)
401//
402
403type JavaBinary struct {
404 JavaLibrary
405
406 binaryProperties struct {
407 // wrapper: installable script to execute the resulting jar
408 Wrapper string
409 }
410}
411
412func (j *JavaBinary) GenerateJavaBuildActions(ctx common.AndroidModuleContext) {
413 j.JavaLibrary.GenerateJavaBuildActions(ctx)
414
415 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
416 // another build rule before the jar has been installed.
417 ctx.InstallFile("bin", filepath.Join(common.ModuleSrcDir(ctx), j.binaryProperties.Wrapper),
418 j.installFile)
419}
420
421func JavaBinaryFactory() (blueprint.Module, []interface{}) {
422 module := &JavaBinary{}
423
424 module.properties.Dex = true
425
426 return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported, &module.binaryProperties)
427}
428
429func JavaBinaryHostFactory() (blueprint.Module, []interface{}) {
430 module := &JavaBinary{}
431
432 return NewJavaBase(&module.javaBase, module, common.HostSupported, &module.binaryProperties)
433}
434
435//
436// Java prebuilts
437//
438
439type JavaPrebuilt struct {
440 common.AndroidModuleBase
441
442 properties struct {
Colin Crossaa8630b2015-04-13 13:52:22 -0700443 Srcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700444 }
445
Colin Crosse1d62a82015-04-03 16:53:05 -0700446 classpathFile string
447 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700448}
449
450func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
451 if len(j.properties.Srcs) != 1 {
452 ctx.ModuleErrorf("expected exactly one jar in srcs")
453 return
454 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700455 prebuilt := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Srcs[0])
456
457 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
458
459 j.classpathFile = prebuilt
460 j.classJarSpecs = []jarSpec{classJarSpec}
461 j.resourceJarSpecs = []jarSpec{resourceJarSpec}
Colin Crosse1d62a82015-04-03 16:53:05 -0700462 ctx.InstallFileName("framework", ctx.ModuleName()+".jar", j.classpathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700463}
464
465var _ JavaDependency = (*JavaPrebuilt)(nil)
466
467func (j *JavaPrebuilt) ClasspathFile() string {
468 return j.classpathFile
469}
470
471func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700472 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700473}
474
475func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700476 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700477}
478
Colin Crossc0b06f12015-04-08 13:03:43 -0700479func (j *JavaPrebuilt) AidlIncludeDirs() []string {
480 return nil
481}
482
Colin Cross2fe66872015-03-30 17:20:39 -0700483func JavaPrebuiltFactory() (blueprint.Module, []interface{}) {
484 module := &JavaPrebuilt{}
485
486 return common.InitAndroidArchModule(module, common.HostAndDeviceSupported,
487 common.MultilibCommon, &module.properties)
488}
489
Colin Crossaa8630b2015-04-13 13:52:22 -0700490//
491// SDK java prebuilts (.jar containing resources plus framework.aidl)
492//
493
494type sdkDependency interface {
495 JavaDependency
496 AidlPreprocessed() string
497}
498
499var _ sdkDependency = (*sdkPrebuilt)(nil)
500
501type sdkPrebuilt struct {
502 JavaPrebuilt
503
504 sdkProperties struct {
505 Aidl_preprocessed string
506 }
507
508 aidlPreprocessed string
509}
510
511func (j *sdkPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
512 j.JavaPrebuilt.GenerateAndroidBuildActions(ctx)
513
514 if j.sdkProperties.Aidl_preprocessed != "" {
515 j.aidlPreprocessed = filepath.Join(common.ModuleSrcDir(ctx), j.sdkProperties.Aidl_preprocessed)
516 }
517}
518
519func (j *sdkPrebuilt) AidlPreprocessed() string {
520 return j.aidlPreprocessed
521}
522
523func SdkPrebuiltFactory() (blueprint.Module, []interface{}) {
524 module := &sdkPrebuilt{}
525
526 return common.InitAndroidArchModule(module, common.HostAndDeviceSupported,
527 common.MultilibCommon, &module.properties, &module.sdkProperties)
528}
529
Colin Cross2fe66872015-03-30 17:20:39 -0700530func inList(s string, l []string) bool {
531 for _, e := range l {
532 if e == s {
533 return true
534 }
535 }
536 return false
537}