| 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" | 
| Chris Gross | 2f74869 | 2020-06-24 20:36:59 +0000 | [diff] [blame] | 28 | "android/soong/java/config" | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 29 | ) | 
|  | 30 |  | 
|  | 31 | var ( | 
|  | 32 | jacoco = pctx.AndroidStaticRule("jacoco", blueprint.RuleParams{ | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 33 | Command: `rm -rf $tmpDir && mkdir -p $tmpDir && ` + | 
|  | 34 | `${config.Zip2ZipCmd} -i $in -o $strippedJar $stripSpec && ` + | 
| Sasha Smundak | 26c6d9e | 2019-06-11 13:30:13 -0700 | [diff] [blame] | 35 | `${config.JavaCmd} ${config.JavaVmFlags} -jar ${config.JacocoCLIJar} ` + | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 36 | `  instrument --quiet --dest $tmpDir $strippedJar && ` + | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 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}", | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 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 |  | 
| Sam Delmerico | 1e3f78f | 2022-09-07 12:07:07 -0400 | [diff] [blame] | 48 | func jacocoDepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 49 | type instrumentable interface { | 
|  | 50 | shouldInstrument(ctx android.BaseModuleContext) bool | 
|  | 51 | shouldInstrumentInApex(ctx android.BaseModuleContext) bool | 
|  | 52 | setInstrument(value bool) | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | j, ok := ctx.Module().(instrumentable) | 
| Cole Faust | 021bf3d | 2024-05-01 16:59:00 -0700 | [diff] [blame] | 56 | if !ctx.Module().Enabled(ctx) || !ok { | 
| Sam Delmerico | 1e3f78f | 2022-09-07 12:07:07 -0400 | [diff] [blame] | 57 | return | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | if j.shouldInstrumentInApex(ctx) { | 
|  | 61 | j.setInstrument(true) | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | if j.shouldInstrument(ctx) && ctx.ModuleName() != "jacocoagent" { | 
|  | 65 | // We can use AddFarVariationDependencies here because, since this dep | 
|  | 66 | // is added as libs only (i.e. a compiletime CLASSPATH entry only), | 
|  | 67 | // the first variant of jacocoagent is sufficient to prevent | 
|  | 68 | // compile time errors. | 
|  | 69 | // At this stage in the build, AddVariationDependencies is not always | 
|  | 70 | // able to procure a variant of jacocoagent that matches the calling | 
|  | 71 | // module. | 
|  | 72 | ctx.AddFarVariationDependencies(ctx.Module().Target().Variations(), libTag, "jacocoagent") | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 76 | // Instruments a jar using the Jacoco command line interface.  Uses stripSpec to extract a subset | 
|  | 77 | // of the classes in inputJar into strippedJar, instruments strippedJar into tmpJar, and then | 
|  | 78 | // combines the classes in tmpJar with inputJar (preferring the instrumented classes in tmpJar) | 
|  | 79 | // to produce instrumentedJar. | 
|  | 80 | func jacocoInstrumentJar(ctx android.ModuleContext, instrumentedJar, strippedJar android.WritablePath, | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 81 | inputJar android.Path, stripSpec string) { | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 82 |  | 
|  | 83 | // The basename of tmpJar has to be the same as the basename of strippedJar | 
|  | 84 | tmpJar := android.PathForModuleOut(ctx, "jacoco", "tmp", strippedJar.Base()) | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 85 |  | 
|  | 86 | ctx.Build(pctx, android.BuildParams{ | 
|  | 87 | Rule:           jacoco, | 
|  | 88 | Description:    "jacoco", | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 89 | Output:         instrumentedJar, | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 90 | ImplicitOutput: strippedJar, | 
|  | 91 | Input:          inputJar, | 
|  | 92 | Args: map[string]string{ | 
| Colin Cross | 84c3882 | 2018-01-03 15:59:46 -0800 | [diff] [blame] | 93 | "strippedJar": strippedJar.String(), | 
|  | 94 | "stripSpec":   stripSpec, | 
|  | 95 | "tmpDir":      filepath.Dir(tmpJar.String()), | 
|  | 96 | "tmpJar":      tmpJar.String(), | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 97 | }, | 
|  | 98 | }) | 
|  | 99 | } | 
|  | 100 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 101 | func (j *Module) jacocoModuleToZipCommand(ctx android.ModuleContext) string { | 
|  | 102 | includes, err := jacocoFiltersToSpecs(j.properties.Jacoco.Include_filter) | 
|  | 103 | if err != nil { | 
|  | 104 | ctx.PropertyErrorf("jacoco.include_filter", "%s", err.Error()) | 
|  | 105 | } | 
| Chris Gross | 2f74869 | 2020-06-24 20:36:59 +0000 | [diff] [blame] | 106 | // Also include the default list of classes to exclude from instrumentation. | 
|  | 107 | excludes, err := jacocoFiltersToSpecs(append(j.properties.Jacoco.Exclude_filter, config.DefaultJacocoExcludeFilter...)) | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 108 | if err != nil { | 
|  | 109 | ctx.PropertyErrorf("jacoco.exclude_filter", "%s", err.Error()) | 
|  | 110 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 111 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 112 | return jacocoFiltersToZipCommand(includes, excludes) | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | func jacocoFiltersToZipCommand(includes, excludes []string) string { | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 116 | specs := "" | 
|  | 117 | if len(excludes) > 0 { | 
| Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame] | 118 | specs += android.JoinWithPrefix(excludes, "-x ") + " " | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 119 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 120 | if len(includes) > 0 { | 
|  | 121 | specs += strings.Join(includes, " ") | 
|  | 122 | } else { | 
| Maxim Pleshivenkov | 9d77384 | 2021-06-30 10:31:46 -0400 | [diff] [blame] | 123 | specs += "'**/*.class'" | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 124 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 125 | return specs | 
|  | 126 | } | 
|  | 127 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 128 | func jacocoFiltersToSpecs(filters []string) ([]string, error) { | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 129 | specs := make([]string, len(filters)) | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 130 | var err error | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 131 | for i, f := range filters { | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 132 | specs[i], err = jacocoFilterToSpec(f) | 
|  | 133 | if err != nil { | 
|  | 134 | return nil, err | 
|  | 135 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 136 | } | 
| Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 137 | return proptools.NinjaAndShellEscapeList(specs), nil | 
| 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 | func jacocoFilterToSpec(filter string) (string, error) { | 
| Nan Zhang | ffe2c1c | 2018-01-25 13:53:22 -0800 | [diff] [blame] | 141 | recursiveWildcard := strings.HasSuffix(filter, "**") | 
|  | 142 | nonRecursiveWildcard := false | 
|  | 143 | if !recursiveWildcard { | 
|  | 144 | nonRecursiveWildcard = strings.HasSuffix(filter, "*") | 
|  | 145 | filter = strings.TrimSuffix(filter, "*") | 
|  | 146 | } else { | 
|  | 147 | filter = strings.TrimSuffix(filter, "**") | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | if recursiveWildcard && !(strings.HasSuffix(filter, ".") || filter == "") { | 
|  | 151 | return "", fmt.Errorf("only '**' or '.**' is supported as recursive wildcard in a filter") | 
|  | 152 | } | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 153 |  | 
|  | 154 | if strings.ContainsRune(filter, '*') { | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 155 | 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] | 156 | } | 
|  | 157 |  | 
|  | 158 | spec := strings.Replace(filter, ".", "/", -1) | 
|  | 159 |  | 
|  | 160 | if recursiveWildcard { | 
|  | 161 | spec += "**/*.class" | 
| Nan Zhang | ffe2c1c | 2018-01-25 13:53:22 -0800 | [diff] [blame] | 162 | } else if nonRecursiveWildcard { | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 163 | spec += "*.class" | 
| Colin Cross | d7deceb | 2017-12-19 13:59:44 -0800 | [diff] [blame] | 164 | } else { | 
|  | 165 | spec += ".class" | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| Colin Cross | 7a3139e | 2017-12-19 13:57:50 -0800 | [diff] [blame] | 168 | return spec, nil | 
| Colin Cross | cb93359 | 2017-11-22 13:49:43 -0800 | [diff] [blame] | 169 | } |