blob: 8f1ceb2619d6800c0064c5094cd747b388329513 [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"
25
26 "android/soong/android"
27)
28
29var (
30 jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{
Colin Cross84c38822018-01-03 15:59:46 -080031 Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` +
32 `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` +
Colin Cross6abc9732017-12-21 14:11:13 -080033 `${config.JavaCmd} -jar ${config.JacocoCLIJar} ` +
Colin Cross84c38822018-01-03 15:59:46 -080034 ` instrument --quiet --dest $tmpDir $strippedJar && ` +
35 `${config.Ziptime} $tmpJar && ` +
36 `${config.MergeZipsCmd} --ignore-duplicates -j $out $tmpJar $in`,
Colin Crosscb933592017-11-22 13:49:43 -080037 CommandDeps: []string{
38 "${config.Zip2ZipCmd}",
39 "${config.JavaCmd}",
40 "${config.JacocoCLIJar}",
41 "${config.Ziptime}",
42 "${config.MergeZipsCmd}",
43 },
44 },
Colin Cross84c38822018-01-03 15:59:46 -080045 "strippedJar", "stripSpec", "tmpDir", "tmpJar")
Colin Crosscb933592017-11-22 13:49:43 -080046)
47
Colin Cross84c38822018-01-03 15:59:46 -080048// Instruments a jar using the Jacoco command line interface. Uses stripSpec to extract a subset
49// of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then
50// combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar)
51// to produce instrumentedJar.
52func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath,
Colin Crosscb933592017-11-22 13:49:43 -080053 inputJar android.Path, stripSpec string) {
Colin Cross84c38822018-01-03 15:59:46 -080054
55 // The basename of tmpJar has to be the same as the basename of strippedJar
56 tmpJar := android.PathForModuleOut(ctx, "jacoco", "tmp", strippedJar.Base())
Colin Crosscb933592017-11-22 13:49:43 -080057
58 ctx.Build(pctx, android.BuildParams{
59 Rule: jacoco,
60 Description: "jacoco",
Colin Cross84c38822018-01-03 15:59:46 -080061 Output: instrumentedJar,
Colin Crosscb933592017-11-22 13:49:43 -080062 ImplicitOutput: strippedJar,
63 Input: inputJar,
64 Args: map[string]string{
Colin Cross84c38822018-01-03 15:59:46 -080065 "strippedJar": strippedJar.String(),
66 "stripSpec": stripSpec,
67 "tmpDir": filepath.Dir(tmpJar.String()),
68 "tmpJar": tmpJar.String(),
Colin Crosscb933592017-11-22 13:49:43 -080069 },
70 })
71}
72
Colin Cross7a3139e2017-12-19 13:57:50 -080073func (j *Module) jacocoModuleToZipCommand(ctx android.ModuleContext) string {
74 includes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Include_filter)
75 if err != nil {
76 ctx.PropertyErrorf("jacoco.include_filter", "%s", err.Error())
77 }
78 excludes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Exclude_filter)
79 if err != nil {
80 ctx.PropertyErrorf("jacoco.exclude_filter", "%s", err.Error())
81 }
Colin Crosscb933592017-11-22 13:49:43 -080082
Colin Cross7a3139e2017-12-19 13:57:50 -080083 return jacocoFiltersToZipCommand(includes, excludes)
84}
85
86func jacocoFiltersToZipCommand(includes, excludes []string) string {
Colin Crosscb933592017-11-22 13:49:43 -080087 specs := ""
88 if len(excludes) > 0 {
Colin Crossd7deceb2017-12-19 13:59:44 -080089 specs += android.JoinWithPrefix(excludes, "-x ") + " "
Colin Crosscb933592017-11-22 13:49:43 -080090 }
Colin Crosscb933592017-11-22 13:49:43 -080091 if len(includes) > 0 {
92 specs += strings.Join(includes, " ")
93 } else {
94 specs += "**/*.class"
95 }
Colin Crosscb933592017-11-22 13:49:43 -080096 return specs
97}
98
Colin Cross7a3139e2017-12-19 13:57:50 -080099func jacocoFiltersToSpecs(filters []string) ([]string, error) {
Colin Crosscb933592017-11-22 13:49:43 -0800100 specs := make([]string, len(filters))
Colin Cross7a3139e2017-12-19 13:57:50 -0800101 var err error
Colin Crosscb933592017-11-22 13:49:43 -0800102 for i, f := range filters {
Colin Cross7a3139e2017-12-19 13:57:50 -0800103 specs[i], err = jacocoFilterToSpec(f)
104 if err != nil {
105 return nil, err
106 }
Colin Crosscb933592017-11-22 13:49:43 -0800107 }
Colin Cross7a3139e2017-12-19 13:57:50 -0800108 return specs, nil
Colin Crosscb933592017-11-22 13:49:43 -0800109}
110
Colin Cross7a3139e2017-12-19 13:57:50 -0800111func jacocoFilterToSpec(filter string) (string, error) {
Colin Crosscb933592017-11-22 13:49:43 -0800112 wildcard := strings.HasSuffix(filter, "*")
113 filter = strings.TrimSuffix(filter, "*")
114 recursiveWildcard := wildcard && (strings.HasSuffix(filter, ".") || filter == "")
115
116 if strings.ContainsRune(filter, '*') {
Colin Cross7a3139e2017-12-19 13:57:50 -0800117 return "", fmt.Errorf("'*' is only supported as the last character in a filter")
Colin Crosscb933592017-11-22 13:49:43 -0800118 }
119
120 spec := strings.Replace(filter, ".", "/", -1)
121
122 if recursiveWildcard {
123 spec += "**/*.class"
124 } else if wildcard {
125 spec += "*.class"
Colin Crossd7deceb2017-12-19 13:59:44 -0800126 } else {
127 spec += ".class"
Colin Crosscb933592017-11-22 13:49:43 -0800128 }
129
Colin Cross7a3139e2017-12-19 13:57:50 -0800130 return spec, nil
Colin Crosscb933592017-11-22 13:49:43 -0800131}