blob: 9162161d317dedc34847ced57691b0bba6409fdc [file] [log] [blame]
Colin Crosscb933592017-11-22 13:49:43 -08001// Copyright 2017 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// Rules for instrumenting classes using jacoco
18
19import (
Colin Cross7a3139e2017-12-19 13:57:50 -080020 "fmt"
Colin Cross84c38822018-01-03 15:59:46 -080021 "path/filepath"
Colin Crosscb933592017-11-22 13:49:43 -080022 "strings"
23
24 "github.com/google/blueprint"
Nan Zhangffe2c1c2018-01-25 13:53:22 -080025 "github.com/google/blueprint/proptools"
Colin Crosscb933592017-11-22 13:49:43 -080026
27 "android/soong/android"
Chris Gross2f748692020-06-24 20:36:59 +000028 "android/soong/java/config"
Colin Crosscb933592017-11-22 13:49:43 -080029)
30
31var (
32 jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{
Colin Cross84c38822018-01-03 15:59:46 -080033 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` +
34 `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` +
Sasha Smundak26c6d9e2019-06-11 13:30:13 -070035 `${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JacocoCLIJar} ` +
Colin Cross84c38822018-01-03 15:59:46 -080036 ` instrument --quiet --dest $tmpDir $strippedJar && ` +
37 `${config.Ziptime} $tmpJar && ` +
38 `${config.MergeZipsCmd} --ignore-duplicates -j $out $tmpJar $in`,
Colin Crosscb933592017-11-22 13:49:43 -080039 CommandDeps: []string{
40 "${config.Zip2ZipCmd}",
41 "${config.JavaCmd}",
42 "${config.JacocoCLIJar}",
43 "${config.Ziptime}",
44 "${config.MergeZipsCmd}",
45 },
46 },
Colin Cross84c38822018-01-03 15:59:46 -080047 "strippedJar", "stripSpec", "tmpDir", "tmpJar")
Colin Crosscb933592017-11-22 13:49:43 -080048)
49
Colin Cross84c38822018-01-03 15:59:46 -080050// Instruments a jar using the Jacoco command line interface. Uses stripSpec to extract a subset
51// of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then
52// combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar)
53// to produce instrumentedJar.
54func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath,
Colin Crosscb933592017-11-22 13:49:43 -080055 inputJar android.Path, stripSpec string) {
Colin Cross84c38822018-01-03 15:59:46 -080056
57 // The basename of tmpJar has to be the same as the basename of strippedJar
58 tmpJar := android.PathForModuleOut(ctx, "jacoco", "tmp", strippedJar.Base())
Colin Crosscb933592017-11-22 13:49:43 -080059
60 ctx.Build(pctx, android.BuildParams{
61 Rule: jacoco,
62 Description: "jacoco",
Colin Cross84c38822018-01-03 15:59:46 -080063 Output: instrumentedJar,
Colin Crosscb933592017-11-22 13:49:43 -080064 ImplicitOutput: strippedJar,
65 Input: inputJar,
66 Args: map[string]string{
Colin Cross84c38822018-01-03 15:59:46 -080067 "strippedJar": strippedJar.String(),
68 "stripSpec": stripSpec,
69 "tmpDir": filepath.Dir(tmpJar.String()),
70 "tmpJar": tmpJar.String(),
Colin Crosscb933592017-11-22 13:49:43 -080071 },
72 })
73}
74
Colin Cross7a3139e2017-12-19 13:57:50 -080075func (j *Module) jacocoModuleToZipCommand(ctx android.ModuleContext) string {
76 includes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Include_filter)
77 if err != nil {
78 ctx.PropertyErrorf("jacoco.include_filter", "%s", err.Error())
79 }
Chris Gross2f748692020-06-24 20:36:59 +000080 // Also include the default list of classes to exclude from instrumentation.
81 excludes, err := jacocoFiltersToSpecs(append(j.properties.Jacoco.Exclude_filter, config.DefaultJacocoExcludeFilter...))
Colin Cross7a3139e2017-12-19 13:57:50 -080082 if err != nil {
83 ctx.PropertyErrorf("jacoco.exclude_filter", "%s", err.Error())
84 }
Colin Crosscb933592017-11-22 13:49:43 -080085
Colin Cross7a3139e2017-12-19 13:57:50 -080086 return jacocoFiltersToZipCommand(includes, excludes)
87}
88
89func jacocoFiltersToZipCommand(includes, excludes []string) string {
Colin Crosscb933592017-11-22 13:49:43 -080090 specs := ""
91 if len(excludes) > 0 {
Colin Crossd7deceb2017-12-19 13:59:44 -080092 specs += android.JoinWithPrefix(excludes, "-x ") + " "
Colin Crosscb933592017-11-22 13:49:43 -080093 }
Colin Crosscb933592017-11-22 13:49:43 -080094 if len(includes) > 0 {
95 specs += strings.Join(includes, " ")
96 } else {
97 specs += "**/*.class"
98 }
Colin Crosscb933592017-11-22 13:49:43 -080099 return specs
100}
101
Colin Cross7a3139e2017-12-19 13:57:50 -0800102func jacocoFiltersToSpecs(filters []string) ([]string, error) {
Colin Crosscb933592017-11-22 13:49:43 -0800103 specs := make([]string, len(filters))
Colin Cross7a3139e2017-12-19 13:57:50 -0800104 var err error
Colin Crosscb933592017-11-22 13:49:43 -0800105 for i, f := range filters {
Colin Cross7a3139e2017-12-19 13:57:50 -0800106 specs[i], err = jacocoFilterToSpec(f)
107 if err != nil {
108 return nil, err
109 }
Colin Crosscb933592017-11-22 13:49:43 -0800110 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800111 return proptools.NinjaAndShellEscapeList(specs), nil
Colin Crosscb933592017-11-22 13:49:43 -0800112}
113
Colin Cross7a3139e2017-12-19 13:57:50 -0800114func jacocoFilterToSpec(filter string) (string, error) {
Nan Zhangffe2c1c2018-01-25 13:53:22 -0800115 recursiveWildcard := strings.HasSuffix(filter, "**")
116 nonRecursiveWildcard := false
117 if !recursiveWildcard {
118 nonRecursiveWildcard = strings.HasSuffix(filter, "*")
119 filter = strings.TrimSuffix(filter, "*")
120 } else {
121 filter = strings.TrimSuffix(filter, "**")
122 }
123
124 if recursiveWildcard && !(strings.HasSuffix(filter, ".") || filter == "") {
125 return "", fmt.Errorf("only '**' or '.**' is supported as recursive wildcard in a filter")
126 }
Colin Crosscb933592017-11-22 13:49:43 -0800127
128 if strings.ContainsRune(filter, '*') {
Colin Cross7a3139e2017-12-19 13:57:50 -0800129 return "", fmt.Errorf("'*' is only supported as the last character in a filter")
Colin Crosscb933592017-11-22 13:49:43 -0800130 }
131
132 spec := strings.Replace(filter, ".", "/", -1)
133
134 if recursiveWildcard {
135 spec += "**/*.class"
Nan Zhangffe2c1c2018-01-25 13:53:22 -0800136 } else if nonRecursiveWildcard {
Colin Crosscb933592017-11-22 13:49:43 -0800137 spec += "*.class"
Colin Crossd7deceb2017-12-19 13:59:44 -0800138 } else {
139 spec += ".class"
Colin Crosscb933592017-11-22 13:49:43 -0800140 }
141
Colin Cross7a3139e2017-12-19 13:57:50 -0800142 return spec, nil
Colin Crosscb933592017-11-22 13:49:43 -0800143}