2021-02-10 14:06:12 +01:00
|
|
|
// Copyright 2021 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package bloaty implements a singleton that measures binary (e.g. ELF
|
|
|
|
// executable, shared library or Rust rlib) section sizes at build time.
|
|
|
|
package bloaty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
2021-03-29 11:26:18 +02:00
|
|
|
const bloatyDescriptorExt = ".bloaty.csv"
|
2021-04-06 14:45:41 +02:00
|
|
|
const protoFilename = "binary_sizes.pb.gz"
|
2021-02-10 14:06:12 +01:00
|
|
|
|
|
|
|
var (
|
2023-12-12 23:13:26 +01:00
|
|
|
fileSizeMeasurerKey blueprint.ProviderKey[measuredFiles]
|
2021-02-10 14:06:12 +01:00
|
|
|
pctx = android.NewPackageContext("android/soong/bloaty")
|
|
|
|
|
|
|
|
// bloaty is used to measure a binary section sizes.
|
|
|
|
bloaty = pctx.AndroidStaticRule("bloaty",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "${bloaty} -n 0 --csv ${in} > ${out}",
|
|
|
|
CommandDeps: []string{"${bloaty}"},
|
|
|
|
})
|
|
|
|
|
|
|
|
// The bloaty merger script is used to combine the outputs from bloaty
|
|
|
|
// into a single protobuf.
|
|
|
|
bloatyMerger = pctx.AndroidStaticRule("bloatyMerger",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "${bloatyMerger} ${out}.lst ${out}",
|
|
|
|
CommandDeps: []string{"${bloatyMerger}"},
|
|
|
|
Rspfile: "${out}.lst",
|
|
|
|
RspfileContent: "${in}",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pctx.VariableConfigMethod("hostPrebuiltTag", android.Config.PrebuiltOS)
|
|
|
|
pctx.SourcePathVariable("bloaty", "prebuilts/build-tools/${hostPrebuiltTag}/bin/bloaty")
|
|
|
|
pctx.HostBinToolVariable("bloatyMerger", "bloaty_merger")
|
2023-05-16 02:58:37 +02:00
|
|
|
android.RegisterParallelSingletonType("file_metrics", fileSizesSingleton)
|
2023-12-13 01:39:03 +01:00
|
|
|
fileSizeMeasurerKey = blueprint.NewProvider[measuredFiles]()
|
2021-02-10 14:06:12 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 11:18:47 +02:00
|
|
|
// measuredFiles contains the paths of the files measured by a module.
|
|
|
|
type measuredFiles struct {
|
|
|
|
paths []android.WritablePath
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeasureSizeForPaths should be called by binary producers to measure the
|
|
|
|
// sizes of artifacts. It must only be called once per module; it will panic
|
|
|
|
// otherwise.
|
|
|
|
func MeasureSizeForPaths(ctx android.ModuleContext, paths ...android.OptionalPath) {
|
|
|
|
mf := measuredFiles{}
|
|
|
|
for _, p := range paths {
|
|
|
|
if !p.Valid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p, ok := p.Path().(android.WritablePath); ok {
|
|
|
|
mf.paths = append(mf.paths, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.SetProvider(fileSizeMeasurerKey, mf)
|
2021-02-10 14:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type sizesSingleton struct{}
|
|
|
|
|
|
|
|
func fileSizesSingleton() android.Singleton {
|
|
|
|
return &sizesSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (singleton *sizesSingleton) GenerateBuildActions(ctx android.SingletonContext) {
|
|
|
|
var deps android.Paths
|
|
|
|
ctx.VisitAllModules(func(m android.Module) {
|
|
|
|
if !ctx.ModuleHasProvider(m, fileSizeMeasurerKey) {
|
|
|
|
return
|
|
|
|
}
|
2021-04-14 11:18:47 +02:00
|
|
|
filePaths := ctx.ModuleProvider(m, fileSizeMeasurerKey).(measuredFiles)
|
|
|
|
for _, path := range filePaths.paths {
|
|
|
|
filePath := path.(android.ModuleOutPath)
|
|
|
|
sizeFile := filePath.InSameDir(ctx, filePath.Base()+bloatyDescriptorExt)
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: bloaty,
|
|
|
|
Description: "bloaty " + filePath.Rel(),
|
|
|
|
Input: filePath,
|
|
|
|
Output: sizeFile,
|
|
|
|
})
|
|
|
|
deps = append(deps, sizeFile)
|
|
|
|
}
|
2021-02-10 14:06:12 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: bloatyMerger,
|
|
|
|
Inputs: android.SortedUniquePaths(deps),
|
2021-02-24 16:44:36 +01:00
|
|
|
Output: android.PathForOutput(ctx, protoFilename),
|
2021-02-10 14:06:12 +01:00
|
|
|
})
|
|
|
|
}
|
2021-02-24 16:44:36 +01:00
|
|
|
|
|
|
|
func (singleton *sizesSingleton) MakeVars(ctx android.MakeVarsContext) {
|
|
|
|
ctx.DistForGoalWithFilename("checkbuild", android.PathForOutput(ctx, protoFilename), protoFilename)
|
|
|
|
}
|