summaryrefslogtreecommitdiff
path: root/java/java.go
diff options
context:
space:
mode:
Diffstat (limited to 'java/java.go')
-rw-r--r--java/java.go181
1 files changed, 121 insertions, 60 deletions
diff --git a/java/java.go b/java/java.go
index bb6e55667..0a3b9b40b 100644
--- a/java/java.go
+++ b/java/java.go
@@ -51,9 +51,6 @@ func init() {
// Renderscript
// Post-jar passes:
// Proguard
-// Jacoco
-// Jarjar
-// Dex
// Rmtypedefs
// DroidDoc
// Findbugs
@@ -127,6 +124,26 @@ type CompilerProperties struct {
// List of javac flags that should only be used when passing -source 1.9
Javacflags []string
}
+
+ Jacoco struct {
+ // List of classes to include for instrumentation with jacoco to collect coverage
+ // information at runtime when building with coverage enabled. If unset defaults to all
+ // classes.
+ // Supports '*' as the last character of an entry in the list as a wildcard match.
+ // If preceded by '.' it matches all classes in the package and subpackages, otherwise
+ // it matches classes in the package that have the class name as a prefix.
+ Include_filter []string
+
+ // List of classes to exclude from instrumentation with jacoco to collect coverage
+ // information at runtime when building with coverage enabled. Overrides classes selected
+ // by the include_filter property.
+ // Supports '*' as the last character of an entry in the list as a wildcard match.
+ // If preceded by '.' it matches all classes in the package and subpackages, otherwise
+ // it matches classes in the package that have the class name as a prefix.
+ Exclude_filter []string
+ }
+
+ Instrument bool `blueprint:"mutated"`
}
type CompilerDeviceProperties struct {
@@ -134,14 +151,19 @@ type CompilerDeviceProperties struct {
Dxflags []string `android:"arch_variant"`
// if not blank, set to the version of the sdk to compile against
- Sdk_version string
+ Sdk_version *string
- // directories to pass to aidl tool
- Aidl_includes []string
+ Aidl struct {
+ // Top level directories to pass to aidl tool
+ Include_dirs []string
- // directories that should be added as include directories
- // for any aidl sources of modules that depend on this module
- Export_aidl_include_dirs []string
+ // Directories rooted at the Android.bp file to pass to aidl tool
+ Local_include_dirs []string
+
+ // directories that should be added as include directories for any aidl sources of modules
+ // that depend on this module, as well as to aidl for this module.
+ Export_include_dirs []string
+ }
// If true, export a copy of the module as a -hostdex module for host testing.
Hostdex *bool
@@ -172,6 +194,9 @@ type Module struct {
// output file containing classes.dex
dexJarFile android.Path
+ // output file containing uninstrumented classes that will be instrumented by jacoco
+ jacocoReportClassesFile android.Path
+
// output file suitable for installing or running
outputFile android.Path
@@ -179,10 +204,6 @@ type Module struct {
logtagsSrcs android.Paths
- // jars containing source files that should be included in the javac command line,
- // for example R.java generated by aapt for android apps
- ExtraSrcJars android.Paths
-
// installed file for binary dependency
installFile android.Path
}
@@ -307,7 +328,7 @@ func decodeSdkDep(ctx android.BaseContext, v string) sdkDep {
func (j *Module) deps(ctx android.BottomUpMutatorContext) {
if ctx.Device() {
if !proptools.Bool(j.properties.No_standard_libs) {
- sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
+ sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
if sdkDep.useDefaultLibs {
ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
if ctx.AConfig().TargetOpenJDK9() {
@@ -377,7 +398,11 @@ func (j *Module) hasSrcExt(ext string) bool {
func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
aidlIncludeDirs android.Paths) []string {
- localAidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl_includes)
+ aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
+ aidlIncludes = append(aidlIncludes,
+ android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
+ aidlIncludes = append(aidlIncludes,
+ android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
var flags []string
if aidlPreprocess.Valid() {
@@ -387,7 +412,7 @@ func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.Opt
}
flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
- flags = append(flags, android.JoinWithPrefix(localAidlIncludes.Strings(), "-I"))
+ flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
if src := android.ExistentPathForSource(ctx, "", ctx.ModuleDir(), "src"); src.Valid() {
flags = append(flags, "-I"+src.String())
@@ -412,7 +437,7 @@ type deps struct {
func (j *Module) collectDeps(ctx android.ModuleContext) deps {
var deps deps
- sdkDep := decodeSdkDep(ctx, j.deviceProperties.Sdk_version)
+ sdkDep := decodeSdkDep(ctx, String(j.deviceProperties.Sdk_version))
if sdkDep.invalidVersion {
ctx.AddMissingDependencies([]string{sdkDep.module})
} else if sdkDep.useFiles {
@@ -458,7 +483,7 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps {
if ctx.ModuleName() == "framework" {
// framework.jar has a one-off dependency on the R.java and Manifest.java files
// generated by framework-res.apk
- // TODO(ccross): aapt java files should go in a src jar
+ deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
}
case kotlinStdlibTag:
deps.kotlinStdlib = dep.HeaderJars()
@@ -493,14 +518,14 @@ func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaB
}
// javaVersion flag.
- sdk := sdkStringToNumber(ctx, j.deviceProperties.Sdk_version)
+ sdk := sdkStringToNumber(ctx, String(j.deviceProperties.Sdk_version))
if j.properties.Java_version != nil {
flags.javaVersion = *j.properties.Java_version
} else if ctx.Device() && sdk <= 23 {
flags.javaVersion = "1.7"
} else if ctx.Device() && sdk <= 26 || !ctx.AConfig().TargetOpenJDK9() {
flags.javaVersion = "1.8"
- } else if ctx.Device() && j.deviceProperties.Sdk_version != "" && sdk == 10000 {
+ } else if ctx.Device() && String(j.deviceProperties.Sdk_version) != "" && sdk == 10000 {
// TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
flags.javaVersion = "1.8"
} else {
@@ -526,9 +551,9 @@ func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaB
return flags
}
-func (j *Module) compile(ctx android.ModuleContext) {
+func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
- j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Export_aidl_include_dirs)
+ j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
deps := j.collectDeps(ctx)
flags := j.collectBuilderFlags(ctx, deps)
@@ -541,10 +566,11 @@ func (j *Module) compile(ctx android.ModuleContext) {
flags = protoFlags(ctx, &j.protoProperties, flags)
}
- var srcJars android.Paths
- srcFiles, srcJars = j.genSources(ctx, srcFiles, flags)
+ srcFiles = j.genSources(ctx, srcFiles, flags)
+
+ srcJars := srcFiles.FilterByExt(".srcjar")
srcJars = append(srcJars, deps.srcJars...)
- srcJars = append(srcJars, j.ExtraSrcJars...)
+ srcJars = append(srcJars, extraSrcJars...)
var jars android.Paths
@@ -708,6 +734,20 @@ func (j *Module) compile(ctx android.ModuleContext) {
}
if ctx.Device() && j.installable() {
+ outputFile = j.desugar(ctx, flags, outputFile, jarName)
+ }
+
+ if ctx.AConfig().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
+ if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
+ j.properties.Instrument = true
+ }
+ }
+
+ if ctx.AConfig().IsEnvTrue("EMMA_INSTRUMENT") && j.properties.Instrument {
+ outputFile = j.instrument(ctx, flags, outputFile, jarName)
+ }
+
+ if ctx.Device() && j.installable() {
outputFile = j.compileDex(ctx, flags, outputFile, jarName)
if ctx.Failed() {
return
@@ -756,6 +796,42 @@ func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars
return headerJar
}
+func (j *Module) desugar(ctx android.ModuleContext, flags javaBuilderFlags,
+ classesJar android.Path, jarName string) android.Path {
+
+ desugarFlags := []string{
+ "--min_sdk_version " + j.minSdkVersionNumber(ctx),
+ "--desugar_try_with_resources_if_needed=false",
+ "--allow_empty_bootclasspath",
+ }
+
+ if inList("--core-library", j.deviceProperties.Dxflags) {
+ desugarFlags = append(desugarFlags, "--core_library")
+ }
+
+ flags.desugarFlags = strings.Join(desugarFlags, " ")
+
+ desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
+ TransformDesugar(ctx, desugarJar, classesJar, flags)
+
+ return desugarJar
+}
+
+func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
+ classesJar android.Path, jarName string) android.Path {
+
+ specs := j.jacocoStripSpecs(ctx)
+
+ jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco", "jacoco-report-classes.jar")
+ instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
+
+ jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
+
+ j.jacocoReportClassesFile = jacocoReportClassesFile
+
+ return instrumentedJar
+}
+
func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
classesJar android.Path, jarName string) android.Path {
@@ -782,47 +858,29 @@ func (j *Module) compileDex(ctx android.ModuleContext, flags javaBuilderFlags,
"--dump-width=1000")
}
- var minSdkVersion string
- switch j.deviceProperties.Sdk_version {
- case "", "current", "test_current", "system_current":
- minSdkVersion = strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
- default:
- minSdkVersion = j.deviceProperties.Sdk_version
- }
-
- dxFlags = append(dxFlags, "--min-sdk-version="+minSdkVersion)
+ dxFlags = append(dxFlags, "--min-sdk-version="+j.minSdkVersionNumber(ctx))
flags.dxFlags = strings.Join(dxFlags, " ")
- desugarFlags := []string{
- "--min_sdk_version " + minSdkVersion,
- "--desugar_try_with_resources_if_needed=false",
- "--allow_empty_bootclasspath",
- }
-
- if inList("--core-library", dxFlags) {
- desugarFlags = append(desugarFlags, "--core_library")
- }
-
- flags.desugarFlags = strings.Join(desugarFlags, " ")
-
- desugarJar := android.PathForModuleOut(ctx, "desugar", jarName)
- TransformDesugar(ctx, desugarJar, classesJar, flags)
- if ctx.Failed() {
- return nil
- }
-
// Compile classes.jar into classes.dex and then javalib.jar
javalibJar := android.PathForModuleOut(ctx, "dex", jarName)
- TransformClassesJarToDexJar(ctx, javalibJar, desugarJar, flags)
- if ctx.Failed() {
- return nil
- }
+ TransformClassesJarToDexJar(ctx, javalibJar, classesJar, flags)
j.dexJarFile = javalibJar
return javalibJar
}
+// Returns a sdk version as a string that is guaranteed to be a parseable as a number. For
+// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns "10000".
+func (j *Module) minSdkVersionNumber(ctx android.ModuleContext) string {
+ switch String(j.deviceProperties.Sdk_version) {
+ case "", "current", "test_current", "system_current":
+ return strconv.Itoa(ctx.AConfig().DefaultAppTargetSdkInt())
+ default:
+ return String(j.deviceProperties.Sdk_version)
+ }
+}
+
func (j *Module) installable() bool {
return j.properties.Installable == nil || *j.properties.Installable
}
@@ -903,7 +961,7 @@ func LibraryHostFactory() android.Module {
type binaryProperties struct {
// installable script to execute the resulting jar
- Wrapper string
+ Wrapper *string
}
type Binary struct {
@@ -924,8 +982,8 @@ func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// Depend on the installed jar (j.installFile) so that the wrapper doesn't get executed by
// another build rule before the jar has been installed.
- if j.binaryProperties.Wrapper != "" {
- j.wrapperFile = android.PathForModuleSrc(ctx, j.binaryProperties.Wrapper).SourcePath
+ if String(j.binaryProperties.Wrapper) != "" {
+ j.wrapperFile = android.PathForModuleSrc(ctx, String(j.binaryProperties.Wrapper)).SourcePath
} else {
j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
}
@@ -970,7 +1028,7 @@ func BinaryHostFactory() android.Module {
type ImportProperties struct {
Jars []string
- Sdk_version string
+ Sdk_version *string
Installable *bool
}
@@ -1084,3 +1142,6 @@ func DefaultsFactory(props ...interface{}) android.Module {
return module
}
+
+var Bool = proptools.Bool
+var String = proptools.String