ThiƩbaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame^] | 1 | // Copyright 2021 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 bloaty implements a singleton that measures binary (e.g. ELF |
| 16 | // executable, shared library or Rust rlib) section sizes at build time. |
| 17 | package bloaty |
| 18 | |
| 19 | import ( |
| 20 | "android/soong/android" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
| 25 | const bloatyDescriptorExt = "bloaty.csv" |
| 26 | |
| 27 | var ( |
| 28 | fileSizeMeasurerKey blueprint.ProviderKey |
| 29 | pctx = android.NewPackageContext("android/soong/bloaty") |
| 30 | |
| 31 | // bloaty is used to measure a binary section sizes. |
| 32 | bloaty = pctx.AndroidStaticRule("bloaty", |
| 33 | blueprint.RuleParams{ |
| 34 | Command: "${bloaty} -n 0 --csv ${in} > ${out}", |
| 35 | CommandDeps: []string{"${bloaty}"}, |
| 36 | }) |
| 37 | |
| 38 | // The bloaty merger script is used to combine the outputs from bloaty |
| 39 | // into a single protobuf. |
| 40 | bloatyMerger = pctx.AndroidStaticRule("bloatyMerger", |
| 41 | blueprint.RuleParams{ |
| 42 | Command: "${bloatyMerger} ${out}.lst ${out}", |
| 43 | CommandDeps: []string{"${bloatyMerger}"}, |
| 44 | Rspfile: "${out}.lst", |
| 45 | RspfileContent: "${in}", |
| 46 | }) |
| 47 | ) |
| 48 | |
| 49 | func init() { |
| 50 | pctx.VariableConfigMethod("hostPrebuiltTag", android.Config.PrebuiltOS) |
| 51 | pctx.SourcePathVariable("bloaty", "prebuilts/build-tools/${hostPrebuiltTag}/bin/bloaty") |
| 52 | pctx.HostBinToolVariable("bloatyMerger", "bloaty_merger") |
| 53 | android.RegisterSingletonType("file_metrics", fileSizesSingleton) |
| 54 | fileSizeMeasurerKey = blueprint.NewProvider(android.ModuleOutPath{}) |
| 55 | } |
| 56 | |
| 57 | // MeasureSizeForPath should be called by binary producers (e.g. in builder.go). |
| 58 | func MeasureSizeForPath(ctx android.ModuleContext, filePath android.WritablePath) { |
| 59 | ctx.SetProvider(fileSizeMeasurerKey, filePath) |
| 60 | } |
| 61 | |
| 62 | type sizesSingleton struct{} |
| 63 | |
| 64 | func fileSizesSingleton() android.Singleton { |
| 65 | return &sizesSingleton{} |
| 66 | } |
| 67 | |
| 68 | func (singleton *sizesSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 69 | var deps android.Paths |
| 70 | // Visit all modules. If the size provider give us a binary path to measure, |
| 71 | // create the rule to measure it. |
| 72 | ctx.VisitAllModules(func(m android.Module) { |
| 73 | if !ctx.ModuleHasProvider(m, fileSizeMeasurerKey) { |
| 74 | return |
| 75 | } |
| 76 | filePath := ctx.ModuleProvider(m, fileSizeMeasurerKey).(android.ModuleOutPath) |
| 77 | sizeFile := filePath.ReplaceExtension(ctx, bloatyDescriptorExt) |
| 78 | ctx.Build(pctx, android.BuildParams{ |
| 79 | Rule: bloaty, |
| 80 | Description: "bloaty " + filePath.Rel(), |
| 81 | Input: filePath, |
| 82 | Output: sizeFile, |
| 83 | }) |
| 84 | deps = append(deps, sizeFile) |
| 85 | }) |
| 86 | |
| 87 | ctx.Build(pctx, android.BuildParams{ |
| 88 | Rule: bloatyMerger, |
| 89 | Inputs: android.SortedUniquePaths(deps), |
| 90 | Output: android.PathForOutput(ctx, "binary_sizes.pb"), |
| 91 | }) |
| 92 | } |