2017-11-22 22:49:43 +01:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
// Rules for instrumenting classes using jacoco
|
|
|
|
|
|
|
|
import (
|
2017-12-19 22:57:50 +01:00
|
|
|
"fmt"
|
2018-01-04 00:59:46 +01:00
|
|
|
"path/filepath"
|
2017-11-22 22:49:43 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2018-01-25 22:53:22 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
2017-11-22 22:49:43 +01:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{
|
2018-01-04 00:59:46 +01:00
|
|
|
Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` +
|
|
|
|
`${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` +
|
2017-12-21 23:11:13 +01:00
|
|
|
`${config.JavaCmd} -jar ${config.JacocoCLIJar} ` +
|
2018-01-04 00:59:46 +01:00
|
|
|
` instrument --quiet --dest $tmpDir $strippedJar && ` +
|
|
|
|
`${config.Ziptime} $tmpJar && ` +
|
|
|
|
`${config.MergeZipsCmd} --ignore-duplicates -j $out $tmpJar $in`,
|
2017-11-22 22:49:43 +01:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.Zip2ZipCmd}",
|
|
|
|
"${config.JavaCmd}",
|
|
|
|
"${config.JacocoCLIJar}",
|
|
|
|
"${config.Ziptime}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
|
|
|
},
|
2018-01-04 00:59:46 +01:00
|
|
|
"strippedJar", "stripSpec", "tmpDir", "tmpJar")
|
2017-11-22 22:49:43 +01:00
|
|
|
)
|
|
|
|
|
2018-01-04 00:59:46 +01:00
|
|
|
// Instruments a jar using the Jacoco command line interface. Uses stripSpec to extract a subset
|
|
|
|
// of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then
|
|
|
|
// combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar)
|
|
|
|
// to produce instrumentedJar.
|
|
|
|
func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath,
|
2017-11-22 22:49:43 +01:00
|
|
|
inputJar android.Path, stripSpec string) {
|
2018-01-04 00:59:46 +01:00
|
|
|
|
|
|
|
// The basename of tmpJar has to be the same as the basename of strippedJar
|
|
|
|
tmpJar := android.PathForModuleOut(ctx, "jacoco", "tmp", strippedJar.Base())
|
2017-11-22 22:49:43 +01:00
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: jacoco,
|
|
|
|
Description: "jacoco",
|
2018-01-04 00:59:46 +01:00
|
|
|
Output: instrumentedJar,
|
2017-11-22 22:49:43 +01:00
|
|
|
ImplicitOutput: strippedJar,
|
|
|
|
Input: inputJar,
|
|
|
|
Args: map[string]string{
|
2018-01-04 00:59:46 +01:00
|
|
|
"strippedJar": strippedJar.String(),
|
|
|
|
"stripSpec": stripSpec,
|
|
|
|
"tmpDir": filepath.Dir(tmpJar.String()),
|
|
|
|
"tmpJar": tmpJar.String(),
|
2017-11-22 22:49:43 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:57:50 +01:00
|
|
|
func (j *Module) jacocoModuleToZipCommand(ctx android.ModuleContext) string {
|
|
|
|
includes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Include_filter)
|
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("jacoco.include_filter", "%s", err.Error())
|
|
|
|
}
|
|
|
|
excludes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Exclude_filter)
|
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("jacoco.exclude_filter", "%s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return jacocoFiltersToZipCommand(includes, excludes)
|
|
|
|
}
|
2017-11-22 22:49:43 +01:00
|
|
|
|
2017-12-19 22:57:50 +01:00
|
|
|
func jacocoFiltersToZipCommand(includes, excludes []string) string {
|
2017-11-22 22:49:43 +01:00
|
|
|
specs := ""
|
|
|
|
if len(excludes) > 0 {
|
2017-12-19 22:59:44 +01:00
|
|
|
specs += android.JoinWithPrefix(excludes, "-x ") + " "
|
2017-11-22 22:49:43 +01:00
|
|
|
}
|
|
|
|
if len(includes) > 0 {
|
|
|
|
specs += strings.Join(includes, " ")
|
|
|
|
} else {
|
|
|
|
specs += "**/*.class"
|
|
|
|
}
|
|
|
|
return specs
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:57:50 +01:00
|
|
|
func jacocoFiltersToSpecs(filters []string) ([]string, error) {
|
2017-11-22 22:49:43 +01:00
|
|
|
specs := make([]string, len(filters))
|
2017-12-19 22:57:50 +01:00
|
|
|
var err error
|
2017-11-22 22:49:43 +01:00
|
|
|
for i, f := range filters {
|
2017-12-19 22:57:50 +01:00
|
|
|
specs[i], err = jacocoFilterToSpec(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-22 22:49:43 +01:00
|
|
|
}
|
2018-01-25 22:53:22 +01:00
|
|
|
return proptools.NinjaAndShellEscape(specs), nil
|
2017-11-22 22:49:43 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:57:50 +01:00
|
|
|
func jacocoFilterToSpec(filter string) (string, error) {
|
2018-01-25 22:53:22 +01:00
|
|
|
recursiveWildcard := strings.HasSuffix(filter, "**")
|
|
|
|
nonRecursiveWildcard := false
|
|
|
|
if !recursiveWildcard {
|
|
|
|
nonRecursiveWildcard = strings.HasSuffix(filter, "*")
|
|
|
|
filter = strings.TrimSuffix(filter, "*")
|
|
|
|
} else {
|
|
|
|
filter = strings.TrimSuffix(filter, "**")
|
|
|
|
}
|
|
|
|
|
|
|
|
if recursiveWildcard && !(strings.HasSuffix(filter, ".") || filter == "") {
|
|
|
|
return "", fmt.Errorf("only '**' or '.**' is supported as recursive wildcard in a filter")
|
|
|
|
}
|
2017-11-22 22:49:43 +01:00
|
|
|
|
|
|
|
if strings.ContainsRune(filter, '*') {
|
2017-12-19 22:57:50 +01:00
|
|
|
return "", fmt.Errorf("'*' is only supported as the last character in a filter")
|
2017-11-22 22:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
spec := strings.Replace(filter, ".", "/", -1)
|
|
|
|
|
|
|
|
if recursiveWildcard {
|
|
|
|
spec += "**/*.class"
|
2018-01-25 22:53:22 +01:00
|
|
|
} else if nonRecursiveWildcard {
|
2017-11-22 22:49:43 +01:00
|
|
|
spec += "*.class"
|
2017-12-19 22:59:44 +01:00
|
|
|
} else {
|
|
|
|
spec += ".class"
|
2017-11-22 22:49:43 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 22:57:50 +01:00
|
|
|
return spec, nil
|
2017-11-22 22:49:43 +01:00
|
|
|
}
|