blob: a23ca9a64885a23c4ceeb8c97f49e7be1747d325 [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
124 AidlPreprocessed() string
Colin Cross2fe66872015-03-30 17:20:39 -0700125}
126
127func NewJavaBase(base *javaBase, module JavaModuleType, hod common.HostOrDeviceSupported,
128 props ...interface{}) (blueprint.Module, []interface{}) {
129
130 base.module = module
131
132 props = append(props, &base.properties)
133
134 return common.InitAndroidArchModule(base, hod, common.MultilibCommon, props...)
135}
136
137func (j *javaBase) BootClasspath(ctx common.AndroidBaseContext) string {
138 if ctx.Device() {
139 if j.properties.Sdk_version == "" {
140 return "core-libart"
141 } else if j.properties.Sdk_version == "current" {
142 // TODO: !TARGET_BUILD_APPS
Colin Crossc0b06f12015-04-08 13:03:43 -0700143 // TODO: export preprocessed framework.aidl from android_stubs_current
Colin Cross2fe66872015-03-30 17:20:39 -0700144 return "android_stubs_current"
145 } else if j.properties.Sdk_version == "system_current" {
146 return "android_system_stubs_current"
147 } else {
148 return "sdk_v" + j.properties.Sdk_version
149 }
150 } else {
151 if j.properties.Dex {
152 return "core-libart"
153 } else {
154 return ""
155 }
156 }
157}
158
159func (j *javaBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
160 var deps []string
161
162 if !j.properties.No_standard_libraries {
163 bootClasspath := j.BootClasspath(ctx)
164 if bootClasspath != "" {
165 deps = append(deps, bootClasspath)
166 }
167 }
168 deps = append(deps, j.properties.Java_libs...)
169 deps = append(deps, j.properties.Java_static_libs...)
170
171 return deps
172}
173
Colin Crossc0b06f12015-04-08 13:03:43 -0700174func (j *javaBase) aidlFlags(ctx common.AndroidModuleContext, aidlPreprocess string,
175 aidlIncludeDirs []string) string {
176
177 localAidlIncludes := pathtools.PrefixPaths(j.properties.Aidl_includes, common.ModuleSrcDir(ctx))
178
179 var flags []string
180 if aidlPreprocess != "" {
181 flags = append(flags, "-p"+aidlPreprocess)
182 } else {
183 flags = append(flags, common.JoinWithPrefix(aidlIncludeDirs, "-I"))
184 }
185
186 flags = append(flags, common.JoinWithPrefix(j.exportAidlIncludeDirs, "-I"))
187 flags = append(flags, common.JoinWithPrefix(localAidlIncludes, "-I"))
188 flags = append(flags, "-I"+common.ModuleSrcDir(ctx))
189 flags = append(flags, "-I"+filepath.Join(common.ModuleSrcDir(ctx), "src"))
190
191 return strings.Join(flags, " ")
192}
193
Colin Cross2fe66872015-03-30 17:20:39 -0700194func (j *javaBase) collectDeps(ctx common.AndroidModuleContext) (classpath []string,
Colin Crossc0b06f12015-04-08 13:03:43 -0700195 bootClasspath string, classJarSpecs, resourceJarSpecs []jarSpec, aidlPreprocess string,
196 aidlIncludeDirs []string) {
Colin Cross2fe66872015-03-30 17:20:39 -0700197
198 ctx.VisitDirectDeps(func(module blueprint.Module) {
199 otherName := ctx.OtherModuleName(module)
200 if javaDep, ok := module.(JavaDependency); ok {
Colin Cross6cbb1272015-04-08 11:23:01 -0700201 if otherName == j.BootClasspath(ctx) {
202 bootClasspath = javaDep.ClasspathFile()
203 } else if inList(otherName, j.properties.Java_libs) {
Colin Cross2fe66872015-03-30 17:20:39 -0700204 classpath = append(classpath, javaDep.ClasspathFile())
205 } else if inList(otherName, j.properties.Java_static_libs) {
206 classpath = append(classpath, javaDep.ClasspathFile())
207 classJarSpecs = append(classJarSpecs, javaDep.ClassJarSpecs()...)
208 resourceJarSpecs = append(resourceJarSpecs, javaDep.ResourceJarSpecs()...)
Colin Cross2fe66872015-03-30 17:20:39 -0700209 } else {
210 panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
211 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700212 aidlIncludeDirs = append(aidlIncludeDirs, j.AidlIncludeDirs()...)
213 if j.AidlPreprocessed() != "" {
214 if aidlPreprocess != "" {
215 ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
216 aidlPreprocess, j.AidlPreprocessed())
217 } else {
218 aidlPreprocess = j.AidlPreprocessed()
219 }
220 }
Colin Cross2fe66872015-03-30 17:20:39 -0700221 } else {
222 ctx.ModuleErrorf("unknown dependency module type for %q", otherName)
223 }
224 })
225
Colin Crossc0b06f12015-04-08 13:03:43 -0700226 return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess, aidlIncludeDirs
Colin Cross2fe66872015-03-30 17:20:39 -0700227}
228
229func (j *javaBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
230 j.module.GenerateJavaBuildActions(ctx)
231}
232
233func (j *javaBase) GenerateJavaBuildActions(ctx common.AndroidModuleContext) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700234
235 j.exportAidlIncludeDirs = pathtools.PrefixPaths(j.properties.Export_aidl_include_dirs,
236 common.ModuleSrcDir(ctx))
237
238 classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
239 aidlIncludeDirs := j.collectDeps(ctx)
240
Colin Cross2fe66872015-03-30 17:20:39 -0700241 flags := javaBuilderFlags{
242 javacFlags: strings.Join(j.properties.Javacflags, " "),
Colin Crossc0b06f12015-04-08 13:03:43 -0700243 aidlFlags: j.aidlFlags(ctx, aidlPreprocess, aidlIncludeDirs),
Colin Cross2fe66872015-03-30 17:20:39 -0700244 }
245
246 var javacDeps []string
247
Colin Cross2fe66872015-03-30 17:20:39 -0700248 if bootClasspath != "" {
249 flags.bootClasspath = "-bootclasspath " + bootClasspath
250 javacDeps = append(javacDeps, bootClasspath)
251 }
252
253 if len(classpath) > 0 {
254 flags.classpath = "-classpath " + strings.Join(classpath, ":")
255 javacDeps = append(javacDeps, classpath...)
256 }
257
Colin Crossc0b06f12015-04-08 13:03:43 -0700258 srcFiles := common.ExpandSources(ctx, j.properties.Srcs)
259
Colin Crossf05fe972015-04-10 17:45:20 -0700260 srcFiles = j.genSources(ctx, srcFiles, flags)
Colin Crossc0b06f12015-04-08 13:03:43 -0700261
Colin Cross8cf13342015-04-10 15:41:49 -0700262 if len(srcFiles) > 0 {
263 // Compile java sources into .class files
264 classes := TransformJavaToClasses(ctx, srcFiles, flags, javacDeps)
265 if ctx.Failed() {
266 return
267 }
268
269 classJarSpecs = append([]jarSpec{classes}, classJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700270 }
271
272 resourceJarSpecs = append(ResourceDirsToJarSpecs(ctx, j.properties.Resource_dirs), resourceJarSpecs...)
Colin Cross2fe66872015-03-30 17:20:39 -0700273
274 manifest := j.properties.Manifest
275 if manifest != "" {
276 manifest = filepath.Join(common.ModuleSrcDir(ctx), manifest)
277 }
278
279 allJarSpecs := append([]jarSpec(nil), classJarSpecs...)
280 allJarSpecs = append(allJarSpecs, resourceJarSpecs...)
281
282 // Combine classes + resources into classes-full-debug.jar
283 outputFile := TransformClassesToJar(ctx, allJarSpecs, manifest)
284 if ctx.Failed() {
285 return
286 }
Colin Cross65bf4f22015-04-03 16:54:17 -0700287
Colin Cross65bf4f22015-04-03 16:54:17 -0700288 if j.properties.Jarjar_rules != "" {
289 jarjar_rules := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Jarjar_rules)
290 // Transform classes-full-debug.jar into classes-jarjar.jar
291 outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
292 if ctx.Failed() {
293 return
294 }
Colin Cross20978302015-04-10 17:05:07 -0700295
296 classes, _ := TransformPrebuiltJarToClasses(ctx, outputFile)
297 classJarSpecs = []jarSpec{classes}
Colin Cross65bf4f22015-04-03 16:54:17 -0700298 }
299
Colin Cross20978302015-04-10 17:05:07 -0700300 j.resourceJarSpecs = resourceJarSpecs
301 j.classJarSpecs = classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700302 j.classpathFile = outputFile
303
Colin Cross8cf13342015-04-10 15:41:49 -0700304 if j.properties.Dex && len(srcFiles) > 0 {
Colin Cross2fe66872015-03-30 17:20:39 -0700305 dxFlags := j.properties.Dxflags
306 if false /* emma enabled */ {
307 // If you instrument class files that have local variable debug information in
308 // them emma does not correctly maintain the local variable table.
309 // This will cause an error when you try to convert the class files for Android.
310 // The workaround here is to build different dex file here based on emma switch
311 // then later copy into classes.dex. When emma is on, dx is run with --no-locals
312 // option to remove local variable information
313 dxFlags = append(dxFlags, "--no-locals")
314 }
315
Colin Cross1332b002015-04-07 17:11:30 -0700316 if ctx.AConfig().Getenv("NO_OPTIMIZE_DX") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700317 dxFlags = append(dxFlags, "--no-optimize")
318 }
319
Colin Cross1332b002015-04-07 17:11:30 -0700320 if ctx.AConfig().Getenv("GENERATE_DEX_DEBUG") != "" {
Colin Cross2fe66872015-03-30 17:20:39 -0700321 dxFlags = append(dxFlags,
322 "--debug",
323 "--verbose",
324 "--dump-to="+filepath.Join(common.ModuleOutDir(ctx), "classes.lst"),
325 "--dump-width=1000")
326 }
327
328 flags.dxFlags = strings.Join(dxFlags, " ")
329
330 // Compile classes.jar into classes.dex
Colin Cross6d1e72d2015-04-10 17:44:24 -0700331 dexJarSpec := TransformClassesJarToDex(ctx, outputFile, flags)
Colin Cross2fe66872015-03-30 17:20:39 -0700332 if ctx.Failed() {
333 return
334 }
335
336 // Combine classes.dex + resources into javalib.jar
Colin Cross6d1e72d2015-04-10 17:44:24 -0700337 outputFile = TransformDexToJavaLib(ctx, resourceJarSpecs, dexJarSpec)
Colin Cross2fe66872015-03-30 17:20:39 -0700338 }
339
340 j.installFile = ctx.InstallFileName("framework", ctx.ModuleName()+".jar", outputFile)
341}
342
343var _ JavaDependency = (*JavaLibrary)(nil)
344
345func (j *javaBase) ClasspathFile() string {
346 return j.classpathFile
347}
348
349func (j *javaBase) ClassJarSpecs() []jarSpec {
350 return j.classJarSpecs
351}
352
353func (j *javaBase) ResourceJarSpecs() []jarSpec {
354 return j.resourceJarSpecs
355}
356
Colin Crossc0b06f12015-04-08 13:03:43 -0700357func (j *javaBase) AidlIncludeDirs() []string {
358 return j.exportAidlIncludeDirs
359}
360
361func (j *javaBase) AidlPreprocessed() string {
362 return ""
363}
364
Colin Crossf05fe972015-04-10 17:45:20 -0700365var _ logtagsProducer = (*javaBase)(nil)
366
367func (j *javaBase) logtags() []string {
368 return j.logtagsSrcs
369}
370
Colin Cross2fe66872015-03-30 17:20:39 -0700371//
372// Java libraries (.jar file)
373//
374
375type JavaLibrary struct {
376 javaBase
377}
378
379func JavaLibraryFactory() (blueprint.Module, []interface{}) {
380 module := &JavaLibrary{}
381
382 module.properties.Dex = true
383
384 return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported)
385}
386
387func JavaLibraryHostFactory() (blueprint.Module, []interface{}) {
388 module := &JavaLibrary{}
389
390 return NewJavaBase(&module.javaBase, module, common.HostSupported)
391}
392
393//
394// Java Binaries (.jar file plus wrapper script)
395//
396
397type JavaBinary struct {
398 JavaLibrary
399
400 binaryProperties struct {
401 // wrapper: installable script to execute the resulting jar
402 Wrapper string
403 }
404}
405
406func (j *JavaBinary) GenerateJavaBuildActions(ctx common.AndroidModuleContext) {
407 j.JavaLibrary.GenerateJavaBuildActions(ctx)
408
409 // Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
410 // another build rule before the jar has been installed.
411 ctx.InstallFile("bin", filepath.Join(common.ModuleSrcDir(ctx), j.binaryProperties.Wrapper),
412 j.installFile)
413}
414
415func JavaBinaryFactory() (blueprint.Module, []interface{}) {
416 module := &JavaBinary{}
417
418 module.properties.Dex = true
419
420 return NewJavaBase(&module.javaBase, module, common.HostAndDeviceSupported, &module.binaryProperties)
421}
422
423func JavaBinaryHostFactory() (blueprint.Module, []interface{}) {
424 module := &JavaBinary{}
425
426 return NewJavaBase(&module.javaBase, module, common.HostSupported, &module.binaryProperties)
427}
428
429//
430// Java prebuilts
431//
432
433type JavaPrebuilt struct {
434 common.AndroidModuleBase
435
436 properties struct {
Colin Crossc0b06f12015-04-08 13:03:43 -0700437 Srcs []string
438 Aidl_preprocessed string
Colin Cross2fe66872015-03-30 17:20:39 -0700439 }
440
Colin Crossc0b06f12015-04-08 13:03:43 -0700441 aidlPreprocessed string
Colin Crosse1d62a82015-04-03 16:53:05 -0700442 classpathFile string
443 classJarSpecs, resourceJarSpecs []jarSpec
Colin Cross2fe66872015-03-30 17:20:39 -0700444}
445
446func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
447 if len(j.properties.Srcs) != 1 {
448 ctx.ModuleErrorf("expected exactly one jar in srcs")
449 return
450 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700451 prebuilt := filepath.Join(common.ModuleSrcDir(ctx), j.properties.Srcs[0])
452
453 classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
454
455 j.classpathFile = prebuilt
456 j.classJarSpecs = []jarSpec{classJarSpec}
457 j.resourceJarSpecs = []jarSpec{resourceJarSpec}
Colin Crossc0b06f12015-04-08 13:03:43 -0700458 if j.properties.Aidl_preprocessed != "" {
459 j.aidlPreprocessed = filepath.Join(common.ModuleSrcDir(ctx), j.properties.Aidl_preprocessed)
460 }
Colin Crosse1d62a82015-04-03 16:53:05 -0700461 ctx.InstallFileName("framework", ctx.ModuleName()+".jar", j.classpathFile)
Colin Cross2fe66872015-03-30 17:20:39 -0700462}
463
464var _ JavaDependency = (*JavaPrebuilt)(nil)
465
466func (j *JavaPrebuilt) ClasspathFile() string {
467 return j.classpathFile
468}
469
470func (j *JavaPrebuilt) ClassJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700471 return j.classJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700472}
473
474func (j *JavaPrebuilt) ResourceJarSpecs() []jarSpec {
Colin Crosse1d62a82015-04-03 16:53:05 -0700475 return j.resourceJarSpecs
Colin Cross2fe66872015-03-30 17:20:39 -0700476}
477
Colin Crossc0b06f12015-04-08 13:03:43 -0700478func (j *JavaPrebuilt) AidlIncludeDirs() []string {
479 return nil
480}
481
482func (j *JavaPrebuilt) AidlPreprocessed() string {
483 return j.aidlPreprocessed
484}
485
Colin Cross2fe66872015-03-30 17:20:39 -0700486func JavaPrebuiltFactory() (blueprint.Module, []interface{}) {
487 module := &JavaPrebuilt{}
488
489 return common.InitAndroidArchModule(module, common.HostAndDeviceSupported,
490 common.MultilibCommon, &module.properties)
491}
492
493func inList(s string, l []string) bool {
494 for _, e := range l {
495 if e == s {
496 return true
497 }
498 }
499 return false
500}