Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | // Rules for instrumenting classes using jacoco |
| 18 | |
| 19 | import ( |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 20 | "fmt" |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 21 | "path/filepath" |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint" |
| 25 | |
| 26 | "android/soong/android" |
| 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{ |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 31 | Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` + |
| 32 | `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` + |
Colin Cross | 6abc973 | 2017-12-21 14:11:13 -0800 | [diff] [blame] | 33 | `${config.JavaCmd} -jar ${config.JacocoCLIJar} ` + |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 34 | ` instrument --quiet --dest $tmpDir $strippedJar && ` + |
| 35 | `${config.Ziptime} $tmpJar && ` + |
| 36 | `${config.MergeZipsCmd} --ignore-duplicates -j $out $tmpJar $in`, |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 37 | CommandDeps: []string{ |
| 38 | "${config.Zip2ZipCmd}", |
| 39 | "${config.JavaCmd}", |
| 40 | "${config.JacocoCLIJar}", |
| 41 | "${config.Ziptime}", |
| 42 | "${config.MergeZipsCmd}", |
| 43 | }, |
| 44 | }, |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 45 | "strippedJar", "stripSpec", "tmpDir", "tmpJar") |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 46 | ) |
| 47 | |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 48 | // 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. |
| 52 | func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath, |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 53 | inputJar android.Path, stripSpec string) { |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 54 | |
| 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 Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 57 | |
| 58 | ctx.Build(pctx, android.BuildParams{ |
| 59 | Rule: jacoco, |
| 60 | Description: "jacoco", |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 61 | Output: instrumentedJar, |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 62 | ImplicitOutput: strippedJar, |
| 63 | Input: inputJar, |
| 64 | Args: map[string]string{ |
Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame^] | 65 | "strippedJar": strippedJar.String(), |
| 66 | "stripSpec": stripSpec, |
| 67 | "tmpDir": filepath.Dir(tmpJar.String()), |
| 68 | "tmpJar": tmpJar.String(), |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 69 | }, |
| 70 | }) |
| 71 | } |
| 72 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 73 | func (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 Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 82 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 83 | return jacocoFiltersToZipCommand(includes, excludes) |
| 84 | } |
| 85 | |
| 86 | func jacocoFiltersToZipCommand(includes, excludes []string) string { |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 87 | specs := "" |
| 88 | if len(excludes) > 0 { |
Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame] | 89 | specs += android.JoinWithPrefix(excludes, "-x ") + " " |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 90 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 91 | if len(includes) > 0 { |
| 92 | specs += strings.Join(includes, " ") |
| 93 | } else { |
| 94 | specs += "**/*.class" |
| 95 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 96 | return specs |
| 97 | } |
| 98 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 99 | func jacocoFiltersToSpecs(filters []string) ([]string, error) { |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 100 | specs := make([]string, len(filters)) |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 101 | var err error |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 102 | for i, f := range filters { |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 103 | specs[i], err = jacocoFilterToSpec(f) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 107 | } |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 108 | return specs, nil |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 111 | func jacocoFilterToSpec(filter string) (string, error) { |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 112 | wildcard := strings.HasSuffix(filter, "*") |
| 113 | filter = strings.TrimSuffix(filter, "*") |
| 114 | recursiveWildcard := wildcard && (strings.HasSuffix(filter, ".") || filter == "") |
| 115 | |
| 116 | if strings.ContainsRune(filter, '*') { |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 117 | return "", fmt.Errorf("'*' is only supported as the last character in a filter") |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | spec := strings.Replace(filter, ".", "/", -1) |
| 121 | |
| 122 | if recursiveWildcard { |
| 123 | spec += "**/*.class" |
| 124 | } else if wildcard { |
| 125 | spec += "*.class" |
Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame] | 126 | } else { |
| 127 | spec += ".class" |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 130 | return spec, nil |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 131 | } |