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 | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{ |
| 30 | Command: `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` + |
| 31 | `${config.JavaCmd} -jar ${config.JacocoCLIJar} instrument -quiet -dest $instrumentedJar $strippedJar && ` + |
| 32 | `${config.Ziptime} $instrumentedJar && ` + |
| 33 | `${config.MergeZipsCmd} --ignore-duplicates -j $out $instrumentedJar $in`, |
| 34 | CommandDeps: []string{ |
| 35 | "${config.Zip2ZipCmd}", |
| 36 | "${config.JavaCmd}", |
| 37 | "${config.JacocoCLIJar}", |
| 38 | "${config.Ziptime}", |
| 39 | "${config.MergeZipsCmd}", |
| 40 | }, |
| 41 | }, |
| 42 | "strippedJar", "stripSpec", "instrumentedJar") |
| 43 | ) |
| 44 | |
| 45 | func jacocoInstrumentJar(ctx android.ModuleContext, outputJar, strippedJar android.WritablePath, |
| 46 | inputJar android.Path, stripSpec string) { |
| 47 | instrumentedJar := android.PathForModuleOut(ctx, "jacoco/instrumented.jar") |
| 48 | |
| 49 | ctx.Build(pctx, android.BuildParams{ |
| 50 | Rule: jacoco, |
| 51 | Description: "jacoco", |
| 52 | Output: outputJar, |
| 53 | ImplicitOutput: strippedJar, |
| 54 | Input: inputJar, |
| 55 | Args: map[string]string{ |
| 56 | "strippedJar": strippedJar.String(), |
| 57 | "stripSpec": stripSpec, |
| 58 | "instrumentedJar": instrumentedJar.String(), |
| 59 | }, |
| 60 | }) |
| 61 | } |
| 62 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 63 | func (j *Module) jacocoModuleToZipCommand(ctx android.ModuleContext) string { |
| 64 | includes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Include_filter) |
| 65 | if err != nil { |
| 66 | ctx.PropertyErrorf("jacoco.include_filter", "%s", err.Error()) |
| 67 | } |
| 68 | excludes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Exclude_filter) |
| 69 | if err != nil { |
| 70 | ctx.PropertyErrorf("jacoco.exclude_filter", "%s", err.Error()) |
| 71 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 72 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 73 | return jacocoFiltersToZipCommand(includes, excludes) |
| 74 | } |
| 75 | |
| 76 | func jacocoFiltersToZipCommand(includes, excludes []string) string { |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 77 | specs := "" |
| 78 | if len(excludes) > 0 { |
Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame^] | 79 | specs += android.JoinWithPrefix(excludes, "-x ") + " " |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 80 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 81 | if len(includes) > 0 { |
| 82 | specs += strings.Join(includes, " ") |
| 83 | } else { |
| 84 | specs += "**/*.class" |
| 85 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 86 | return specs |
| 87 | } |
| 88 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 89 | func jacocoFiltersToSpecs(filters []string) ([]string, error) { |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 90 | specs := make([]string, len(filters)) |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 91 | var err error |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 92 | for i, f := range filters { |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 93 | specs[i], err = jacocoFilterToSpec(f) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 97 | } |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 98 | return specs, nil |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 101 | func jacocoFilterToSpec(filter string) (string, error) { |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 102 | wildcard := strings.HasSuffix(filter, "*") |
| 103 | filter = strings.TrimSuffix(filter, "*") |
| 104 | recursiveWildcard := wildcard && (strings.HasSuffix(filter, ".") || filter == "") |
| 105 | |
| 106 | if strings.ContainsRune(filter, '*') { |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 107 | 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] | 108 | } |
| 109 | |
| 110 | spec := strings.Replace(filter, ".", "/", -1) |
| 111 | |
| 112 | if recursiveWildcard { |
| 113 | spec += "**/*.class" |
| 114 | } else if wildcard { |
| 115 | spec += "*.class" |
Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame^] | 116 | } else { |
| 117 | spec += ".class" |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 120 | return spec, nil |
Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 121 | } |