blob: 8b6d4ac87bf94f157d0890a36ed30a22e72687c8 [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"
28)
29
30var (
31 jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{
Colin Cross84c38822018-01-03 15:59:46 -080032 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` +
33 `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` +
Colin Cross6abc9732017-12-21 14:11:13 -080034 `${config.JavaCmd} -jar ${config.JacocoCLIJar} ` +
Colin Cross84c38822018-01-03 15:59:46 -080035 ` instrument --quiet --dest $tmpDir $strippedJar && ` +
36 `${config.Ziptime} $tmpJar && ` +
37 `${config.MergeZipsCmd} --ignore-duplicates -j $out $tmpJar $in`,
Colin Crosscb933592017-11-22 13:49:43 -080038 CommandDeps: []string{
39 "${config.Zip2ZipCmd}",
40 "${config.JavaCmd}",
41 "${config.JacocoCLIJar}",
42 "${config.Ziptime}",
43 "${config.MergeZipsCmd}",
44 },
45 },
Colin Cross84c38822018-01-03 15:59:46 -080046 "strippedJar", "stripSpec", "tmpDir", "tmpJar")
Colin Crosscb933592017-11-22 13:49:43 -080047)
48
Colin Cross84c38822018-01-03 15:59:46 -080049// Instruments a jar using the Jacoco command line interface. Uses stripSpec to extract a subset
50// of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then
51// combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar)
52// to produce instrumentedJar.
53func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath,
Colin Crosscb933592017-11-22 13:49:43 -080054 inputJar android.Path, stripSpec string) {
Colin Cross84c38822018-01-03 15:59:46 -080055
56 // The basename of tmpJar has to be the same as the basename of strippedJar
57 tmpJar := android.PathForModuleOut(ctx, "jacoco", "tmp", strippedJar.Base())
Colin Crosscb933592017-11-22 13:49:43 -080058
59 ctx.Build(pctx, android.BuildParams{
60 Rule: jacoco,
61 Description: "jacoco",
Colin Cross84c38822018-01-03 15:59:46 -080062 Output: instrumentedJar,
Colin Crosscb933592017-11-22 13:49:43 -080063 ImplicitOutput: strippedJar,
64 Input: inputJar,
65 Args: map[string]string{
Colin Cross84c38822018-01-03 15:59:46 -080066 "strippedJar": strippedJar.String(),
67 "stripSpec": stripSpec,
68 "tmpDir": filepath.Dir(tmpJar.String()),
69 "tmpJar": tmpJar.String(),
Colin Crosscb933592017-11-22 13:49:43 -080070 },
71 })
72}
73
Colin Cross7a3139e2017-12-19 13:57:50 -080074func (j *Module) jacocoModuleToZipCommand(ctx android.ModuleContext) string {
75 includes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Include_filter)
76 if err != nil {
77 ctx.PropertyErrorf("jacoco.include_filter", "%s", err.Error())
78 }
79 excludes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Exclude_filter)
80 if err != nil {
81 ctx.PropertyErrorf("jacoco.exclude_filter", "%s", err.Error())
82 }
Colin Crosscb933592017-11-22 13:49:43 -080083
Colin Cross7a3139e2017-12-19 13:57:50 -080084 return jacocoFiltersToZipCommand(includes, excludes)
85}
86
87func jacocoFiltersToZipCommand(includes, excludes []string) string {
Colin Crosscb933592017-11-22 13:49:43 -080088 specs := ""
89 if len(excludes) > 0 {
Colin Crossd7deceb2017-12-19 13:59:44 -080090 specs += android.JoinWithPrefix(excludes, "-x ") + " "
Colin Crosscb933592017-11-22 13:49:43 -080091 }
Colin Crosscb933592017-11-22 13:49:43 -080092 if len(includes) > 0 {
93 specs += strings.Join(includes, " ")
94 } else {
95 specs += "**/*.class"
96 }
Colin Crosscb933592017-11-22 13:49:43 -080097 return specs
98}
99
Colin Cross7a3139e2017-12-19 13:57:50 -0800100func jacocoFiltersToSpecs(filters []string) ([]string, error) {
Colin Crosscb933592017-11-22 13:49:43 -0800101 specs := make([]string, len(filters))
Colin Cross7a3139e2017-12-19 13:57:50 -0800102 var err error
Colin Crosscb933592017-11-22 13:49:43 -0800103 for i, f := range filters {
Colin Cross7a3139e2017-12-19 13:57:50 -0800104 specs[i], err = jacocoFilterToSpec(f)
105 if err != nil {
106 return nil, err
107 }
Colin Crosscb933592017-11-22 13:49:43 -0800108 }
Colin Cross0b9f31f2019-02-28 11:00:01 -0800109 return proptools.NinjaAndShellEscapeList(specs), nil
Colin Crosscb933592017-11-22 13:49:43 -0800110}
111
Colin Cross7a3139e2017-12-19 13:57:50 -0800112func jacocoFilterToSpec(filter string) (string, error) {
Nan Zhangffe2c1c2018-01-25 13:53:22 -0800113 recursiveWildcard := strings.HasSuffix(filter, "**")
114 nonRecursiveWildcard := false
115 if !recursiveWildcard {
116 nonRecursiveWildcard = strings.HasSuffix(filter, "*")
117 filter = strings.TrimSuffix(filter, "*")
118 } else {
119 filter = strings.TrimSuffix(filter, "**")
120 }
121
122 if recursiveWildcard && !(strings.HasSuffix(filter, ".") || filter == "") {
123 return "", fmt.Errorf("only '**' or '.**' is supported as recursive wildcard in a filter")
124 }
Colin Crosscb933592017-11-22 13:49:43 -0800125
126 if strings.ContainsRune(filter, '*') {
Colin Cross7a3139e2017-12-19 13:57:50 -0800127 return "", fmt.Errorf("'*' is only supported as the last character in a filter")
Colin Crosscb933592017-11-22 13:49:43 -0800128 }
129
130 spec := strings.Replace(filter, ".", "/", -1)
131
132 if recursiveWildcard {
133 spec += "**/*.class"
Nan Zhangffe2c1c2018-01-25 13:53:22 -0800134 } else if nonRecursiveWildcard {
Colin Crosscb933592017-11-22 13:49:43 -0800135 spec += "*.class"
Colin Crossd7deceb2017-12-19 13:59:44 -0800136 } else {
137 spec += ".class"
Colin Crosscb933592017-11-22 13:49:43 -0800138 }
139
Colin Cross7a3139e2017-12-19 13:57:50 -0800140 return spec, nil
Colin Crosscb933592017-11-22 13:49:43 -0800141}