| 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" | 
| Nan Zhang | ffe2c1c | 2018-01-25 13:53:22 -0800 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 26 |  | 
|  | 27 | "android/soong/android" | 
|  | 28 | ) | 
|  | 29 |  | 
|  | 30 | var ( | 
|  | 31 | jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{ | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 32 | Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` + | 
|  | 33 | `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` + | 
| Colin Cross | 6abc973 | 2017-12-21 14:11:13 -0800 | [diff] [blame] | 34 | `${config.JavaCmd} -jar ${config.JacocoCLIJar} ` + | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 35 | `  instrument --quiet --dest $tmpDir $strippedJar && ` + | 
|  | 36 | `${config.Ziptime} $tmpJar && ` + | 
|  | 37 | `${config.MergeZipsCmd} --ignore-duplicates -j $out $tmpJar $in`, | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 38 | CommandDeps: []string{ | 
|  | 39 | "${config.Zip2ZipCmd}", | 
|  | 40 | "${config.JavaCmd}", | 
|  | 41 | "${config.JacocoCLIJar}", | 
|  | 42 | "${config.Ziptime}", | 
|  | 43 | "${config.MergeZipsCmd}", | 
|  | 44 | }, | 
|  | 45 | }, | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 46 | "strippedJar", "stripSpec", "tmpDir", "tmpJar") | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 47 | ) | 
|  | 48 |  | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 49 | // 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. | 
|  | 53 | func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath, | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 54 | inputJar android.Path, stripSpec string) { | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 55 |  | 
|  | 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 Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 58 |  | 
|  | 59 | ctx.Build(pctx, android.BuildParams{ | 
|  | 60 | Rule:           jacoco, | 
|  | 61 | Description:    "jacoco", | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 62 | Output:         instrumentedJar, | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 63 | ImplicitOutput: strippedJar, | 
|  | 64 | Input:          inputJar, | 
|  | 65 | Args: map[string]string{ | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 66 | "strippedJar": strippedJar.String(), | 
|  | 67 | "stripSpec":   stripSpec, | 
|  | 68 | "tmpDir":      filepath.Dir(tmpJar.String()), | 
|  | 69 | "tmpJar":      tmpJar.String(), | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 70 | }, | 
|  | 71 | }) | 
|  | 72 | } | 
|  | 73 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 74 | func (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 Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 83 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 84 | return jacocoFiltersToZipCommand(includes, excludes) | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | func jacocoFiltersToZipCommand(includes, excludes []string) string { | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 88 | specs := "" | 
|  | 89 | if len(excludes) > 0 { | 
| Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame] | 90 | specs += android.JoinWithPrefix(excludes, "-x ") + " " | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 91 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 92 | if len(includes) > 0 { | 
|  | 93 | specs += strings.Join(includes, " ") | 
|  | 94 | } else { | 
|  | 95 | specs += "**/*.class" | 
|  | 96 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 97 | return specs | 
|  | 98 | } | 
|  | 99 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 100 | func jacocoFiltersToSpecs(filters []string) ([]string, error) { | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 101 | specs := make([]string, len(filters)) | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 102 | var err error | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 103 | for i, f := range filters { | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 104 | specs[i], err = jacocoFilterToSpec(f) | 
|  | 105 | if err != nil { | 
|  | 106 | return nil, err | 
|  | 107 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 108 | } | 
| Nan Zhang | ffe2c1c | 2018-01-25 13:53:22 -0800 | [diff] [blame] | 109 | return proptools.NinjaAndShellEscape(specs), nil | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 112 | func jacocoFilterToSpec(filter string) (string, error) { | 
| Nan Zhang | ffe2c1c | 2018-01-25 13:53:22 -0800 | [diff] [blame] | 113 | 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 Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 125 |  | 
|  | 126 | if strings.ContainsRune(filter, '*') { | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 127 | 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] | 128 | } | 
|  | 129 |  | 
|  | 130 | spec := strings.Replace(filter, ".", "/", -1) | 
|  | 131 |  | 
|  | 132 | if recursiveWildcard { | 
|  | 133 | spec += "**/*.class" | 
| Nan Zhang | ffe2c1c | 2018-01-25 13:53:22 -0800 | [diff] [blame] | 134 | } else if nonRecursiveWildcard { | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 135 | spec += "*.class" | 
| Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame] | 136 | } else { | 
|  | 137 | spec += ".class" | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 140 | return spec, nil | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 141 | } |