| 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 |  | 
| Thiébaud Weksteen | 55d6b3e | 2021-03-29 11:26:18 +0200 | [diff] [blame] | 25 | const bloatyDescriptorExt = ".bloaty.csv" | 
| Thiébaud Weksteen | 85e8bd6 | 2021-04-06 14:45:41 +0200 | [diff] [blame] | 26 | const protoFilename = "binary_sizes.pb.gz" | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 27 |  | 
|  | 28 | var ( | 
| Colin Cross | 3c0a83d | 2023-12-12 14:13:26 -0800 | [diff] [blame] | 29 | fileSizeMeasurerKey blueprint.ProviderKey[measuredFiles] | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 30 | pctx                = android.NewPackageContext("android/soong/bloaty") | 
|  | 31 |  | 
|  | 32 | // bloaty is used to measure a binary section sizes. | 
|  | 33 | bloaty = pctx.AndroidStaticRule("bloaty", | 
|  | 34 | blueprint.RuleParams{ | 
|  | 35 | Command:     "${bloaty} -n 0 --csv ${in} > ${out}", | 
|  | 36 | CommandDeps: []string{"${bloaty}"}, | 
|  | 37 | }) | 
|  | 38 |  | 
|  | 39 | // The bloaty merger script is used to combine the outputs from bloaty | 
|  | 40 | // into a single protobuf. | 
|  | 41 | bloatyMerger = pctx.AndroidStaticRule("bloatyMerger", | 
|  | 42 | blueprint.RuleParams{ | 
|  | 43 | Command:        "${bloatyMerger} ${out}.lst ${out}", | 
|  | 44 | CommandDeps:    []string{"${bloatyMerger}"}, | 
|  | 45 | Rspfile:        "${out}.lst", | 
|  | 46 | RspfileContent: "${in}", | 
|  | 47 | }) | 
|  | 48 | ) | 
|  | 49 |  | 
|  | 50 | func init() { | 
|  | 51 | pctx.VariableConfigMethod("hostPrebuiltTag", android.Config.PrebuiltOS) | 
|  | 52 | pctx.SourcePathVariable("bloaty", "prebuilts/build-tools/${hostPrebuiltTag}/bin/bloaty") | 
|  | 53 | pctx.HostBinToolVariable("bloatyMerger", "bloaty_merger") | 
| LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 54 | android.RegisterParallelSingletonType("file_metrics", fileSizesSingleton) | 
| Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 55 | fileSizeMeasurerKey = blueprint.NewProvider[measuredFiles]() | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Thiébaud Weksteen | e4dd14b | 2021-04-14 11:18:47 +0200 | [diff] [blame] | 58 | // measuredFiles contains the paths of the files measured by a module. | 
|  | 59 | type measuredFiles struct { | 
|  | 60 | paths []android.WritablePath | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | // MeasureSizeForPaths should be called by binary producers to measure the | 
|  | 64 | // sizes of artifacts. It must only be called once per module; it will panic | 
|  | 65 | // otherwise. | 
|  | 66 | func MeasureSizeForPaths(ctx android.ModuleContext, paths ...android.OptionalPath) { | 
|  | 67 | mf := measuredFiles{} | 
|  | 68 | for _, p := range paths { | 
|  | 69 | if !p.Valid() { | 
|  | 70 | continue | 
|  | 71 | } | 
|  | 72 | if p, ok := p.Path().(android.WritablePath); ok { | 
|  | 73 | mf.paths = append(mf.paths, p) | 
|  | 74 | } | 
|  | 75 | } | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 76 | android.SetProvider(ctx, fileSizeMeasurerKey, mf) | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | type sizesSingleton struct{} | 
|  | 80 |  | 
|  | 81 | func fileSizesSingleton() android.Singleton { | 
|  | 82 | return &sizesSingleton{} | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | func (singleton *sizesSingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
|  | 86 | var deps android.Paths | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 87 | ctx.VisitAllModules(func(m android.Module) { | 
| Colin Cross | 5a37718 | 2023-12-14 14:46:23 -0800 | [diff] [blame] | 88 | filePaths, ok := android.SingletonModuleProvider(ctx, m, fileSizeMeasurerKey) | 
|  | 89 | if !ok { | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 90 | return | 
|  | 91 | } | 
| Thiébaud Weksteen | e4dd14b | 2021-04-14 11:18:47 +0200 | [diff] [blame] | 92 | for _, path := range filePaths.paths { | 
|  | 93 | filePath := path.(android.ModuleOutPath) | 
|  | 94 | sizeFile := filePath.InSameDir(ctx, filePath.Base()+bloatyDescriptorExt) | 
|  | 95 | ctx.Build(pctx, android.BuildParams{ | 
|  | 96 | Rule:        bloaty, | 
|  | 97 | Description: "bloaty " + filePath.Rel(), | 
|  | 98 | Input:       filePath, | 
|  | 99 | Output:      sizeFile, | 
|  | 100 | }) | 
|  | 101 | deps = append(deps, sizeFile) | 
|  | 102 | } | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 103 | }) | 
|  | 104 |  | 
|  | 105 | ctx.Build(pctx, android.BuildParams{ | 
|  | 106 | Rule:   bloatyMerger, | 
|  | 107 | Inputs: android.SortedUniquePaths(deps), | 
| Thiébaud Weksteen | 5db3d98 | 2021-02-24 16:44:36 +0100 | [diff] [blame] | 108 | Output: android.PathForOutput(ctx, protoFilename), | 
| Thiébaud Weksteen | 6d48aad | 2021-02-10 14:06:12 +0100 | [diff] [blame] | 109 | }) | 
|  | 110 | } | 
| Thiébaud Weksteen | 5db3d98 | 2021-02-24 16:44:36 +0100 | [diff] [blame] | 111 |  | 
|  | 112 | func (singleton *sizesSingleton) MakeVars(ctx android.MakeVarsContext) { | 
|  | 113 | ctx.DistForGoalWithFilename("checkbuild", android.PathForOutput(ctx, protoFilename), protoFilename) | 
|  | 114 | } |