blob: 21bf4acf1bfcfda9d63f12fe73c3b84e40eadbfa [file] [log] [blame]
Thiébaud Weksteen6d48aad2021-02-10 14:06:12 +01001// 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.
17package bloaty
18
19import (
20 "android/soong/android"
21
22 "github.com/google/blueprint"
23)
24
25const bloatyDescriptorExt = "bloaty.csv"
Thiébaud Weksteen5db3d982021-02-24 16:44:36 +010026const protoFilename = "binary_sizes.pb"
Thiébaud Weksteen6d48aad2021-02-10 14:06:12 +010027
28var (
29 fileSizeMeasurerKey blueprint.ProviderKey
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
50func init() {
51 pctx.VariableConfigMethod("hostPrebuiltTag", android.Config.PrebuiltOS)
52 pctx.SourcePathVariable("bloaty", "prebuilts/build-tools/${hostPrebuiltTag}/bin/bloaty")
53 pctx.HostBinToolVariable("bloatyMerger", "bloaty_merger")
54 android.RegisterSingletonType("file_metrics", fileSizesSingleton)
55 fileSizeMeasurerKey = blueprint.NewProvider(android.ModuleOutPath{})
56}
57
58// MeasureSizeForPath should be called by binary producers (e.g. in builder.go).
59func MeasureSizeForPath(ctx android.ModuleContext, filePath android.WritablePath) {
60 ctx.SetProvider(fileSizeMeasurerKey, filePath)
61}
62
63type sizesSingleton struct{}
64
65func fileSizesSingleton() android.Singleton {
66 return &sizesSingleton{}
67}
68
69func (singleton *sizesSingleton) GenerateBuildActions(ctx android.SingletonContext) {
70 var deps android.Paths
71 // Visit all modules. If the size provider give us a binary path to measure,
72 // create the rule to measure it.
73 ctx.VisitAllModules(func(m android.Module) {
74 if !ctx.ModuleHasProvider(m, fileSizeMeasurerKey) {
75 return
76 }
77 filePath := ctx.ModuleProvider(m, fileSizeMeasurerKey).(android.ModuleOutPath)
78 sizeFile := filePath.ReplaceExtension(ctx, bloatyDescriptorExt)
79 ctx.Build(pctx, android.BuildParams{
80 Rule: bloaty,
81 Description: "bloaty " + filePath.Rel(),
82 Input: filePath,
83 Output: sizeFile,
84 })
85 deps = append(deps, sizeFile)
86 })
87
88 ctx.Build(pctx, android.BuildParams{
89 Rule: bloatyMerger,
90 Inputs: android.SortedUniquePaths(deps),
Thiébaud Weksteen5db3d982021-02-24 16:44:36 +010091 Output: android.PathForOutput(ctx, protoFilename),
Thiébaud Weksteen6d48aad2021-02-10 14:06:12 +010092 })
93}
Thiébaud Weksteen5db3d982021-02-24 16:44:36 +010094
95func (singleton *sizesSingleton) MakeVars(ctx android.MakeVarsContext) {
96 ctx.DistForGoalWithFilename("checkbuild", android.PathForOutput(ctx, protoFilename), protoFilename)
97}